0
\$\begingroup\$

I'm making a Menu for my game and I made the MenuState(class) and made a MouseManager(class, implemented MouseListener and MouseMotionListener) and they are different class in different folder. Here is my problem, usually people make their buttons my drawing rectangles and adds font and makes that their button. But I didn't do that. I made a button using Photoshop and I put it in my sprite sheet, cropped it and made it my button. now I want to make it that if I click on my 'play' button picture it sends to my GameState and if I click on my 'exit' button picture it closes my window

MyMenuState(class)

 package com.vescorspel.game.MyFirstGame.states;

import java.awt.Color;
import java.awt.Graphics;

import com.vescorspel.game.MyFirstGame.Handler;
import com.vescorspel.game.MyFirstGame.gfx.Assets;

public class MenuState extends State{
// Resizing my button from 64 pixels to 256 pixels
public static final int ButtonWidth = 256;
public static final int ButtonHeight = 256;

// coordinates where my buttons are placed in pixels ;)
public static final int PlayButtonX = 500;
public static final int PlayButtonY = 100;
public static final int ExitButtonX = 500;
public static final int ExitButtonY = 400;

public MenuState(Handler handler){
	super(handler);
}

// My tick method
@Override
public void tick() {
	//This If statement tells my game if I LeftClicked anywhere in the MenuScreen, then it send me to my GameState(GameScreen)
	if(handler.getMouseManager().isLeftPressed())
		State.setState(handler.getGame().gameState);;
}


//My render where i render the button pictures in my MenuScreen
@Override
public void render(Graphics g) {
	//PlayButton from my Assets class(this is an image)
    g.drawImage(Assets.PlayButton, PlayButtonX, PlayButtonY, ButtonWidth, ButtonHeight, null );
    //ExitButton from my Assets class(this is an image)
    g.drawImage(Assets.ExitButton, ExitButtonX, ExitButtonY, ButtonWidth, ButtonHeight, null);
    //these 2 commands tells my game to make a red cube and follow my mouse in the MenuScreen
    g.setColor(Color.RED);
    g.fillRect(handler.getMouseManager().getMouseX(), handler.getMouseManager().getMouseY(), 8, 8);
}
}

MouseManager(class)

package com.vescorspel.game.MyFirstGame.input;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class MouseManager implements MouseListener, MouseMotionListener{
	private boolean leftPressed;
	private int mouseX, mouseY;

	public MouseManager(){
	}

	//Getters

	public boolean isLeftPressed(){
	    return leftPressed;
	}

	public int getMouseX(){
	    return mouseX;
	}

	public int getMouseY(){
	    return mouseY;
	}

	// Implemented methods
    
	@Override
	public void mousePressed(MouseEvent e) {
		if(e.getButton() == MouseEvent.BUTTON1)
			leftPressed = true;
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	    if(e.getButton() == MouseEvent.BUTTON1)
	        leftPressed = false;
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	    mouseX = e.getX();
	    mouseY = e.getY();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
	    // TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	    // TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
	    // TODO Auto-generated method stub
	}

}

\$\endgroup\$
  • \$\begingroup\$ notice me ppl... \$\endgroup\$ – SreeLegend Nov 6 '16 at 13:09
  • \$\begingroup\$ WHERE DID EVERYBODY GO \$\endgroup\$ – SreeLegend Nov 6 '16 at 13:09
1
\$\begingroup\$

All you need to do is verify that the mouse is inside the image, if you want to do this properly you should have an array of your own button classes which have methods to check collisions. Checking the actual collision of these buttons is trivial, just verify that the mouse coords are within the range of coordinates covered by the sprite, do this by validating that the mouse coordinates are greater that the x and y positions, but not greater than the x and y positions plus the width and height respectively, like this:

if ((mouseX > PlayButtonX && mouseX < (PlayButtonX + ButtonWidth)) && (mouseY > PlayButtonY && mouseY < (PlayButtonY + ButtonHeight)) {
     //play button has been clicked
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ If you start needing more buttons/GUI elements, you'd probably want to look into a full GUI system. This is probably the best answer for something one time and small that won't need to be reused a lot. \$\endgroup\$ – Coburn Nov 10 '16 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.