• Buttons

    To create a button we use the mouse with any of these classes: GameObject, GameImage, Animation, Sprite or Body.

    To know if the mouse had cliked in some object that we're using as a button, we should know if the mouse is over the object, to do that we use the method

    boolean mouse.isOverObject(object);

    Now that we know how to verify if the mouse is over an object, we should know if any button was pressed, to do that we could use the following methods:

     
    					public boolean isLeftButtonPressed(),
    					public boolean isMiddleButtonPressed(),
    					public boolean isRightButtonPressed().
    				

    These methods together could make any object act like a button.


    Example: Creating a button.

     
    					package Botao001;
    
    					import jplay.Animation;
    					import jplay.GameImage;
    					import jplay.Mouse;
    					import jplay.Window;
    
    					/**
    					 * @author Gefersom Cardoso Lima
    					 * Federal Fluminense University
    					 * Computer Science
    					 */
    
    					public class Botao001
    					{
    						//Creates a button.
    						public static void main(String[] args)
    						{
    								Window janela = new Window(800,600);
    								Mouse mouse = janela.getMouse();
    
    								GameImage backGround = new GameImage("fundo.png");
    							   
    								Animation botao = new Animation("botao.png",12);
    
    								botao.x = 350;
    								botao.y = 330;
    								botao.setTotalDuration(1200);
    								botao.setLoop(false);
    								botao.stop();
    
    								boolean executando = true;
    								while(executando)
    								{
    										backGround.draw();
    										botao.draw();
    										janela.update();
    										
    										if (mouse.isOverObject(botao) && mouse.isLeftButtonPressed())
    										{
    											botao.stop();
    											botao.play();
    										}
    
    										botao.update();
    								}
    								janela.exit();
    						}
    					}
    
    				

    UFF - Universidade Federal Fluminense - Institudo de Computação - Ciência da Computação