Costum gui elements

Hey guys,

So I’ve created my own menu in my “want-to-become-a-game”, and it works fine with keyboard input and it’s able to change state/screen. Next I’m wondering how I’d make it work with the mouse too? How do I make my buttons(Or whatever) able to know that it has been clicked, or rather how do I know what element that was clicked, so that I can tell it to do something? :slight_smile:

If you are using Java2D, just add a MouseListener and MouseMotionListener :slight_smile:

That’s what I’m using. :slight_smile:

But how do I know which element I’m hovering/clicking? Should I store the elements in some sort of list with their respective coords?


class Entity {
    Rectangle bounds = …
}

for(Entity e : entities) {
    if(e.bounds.contains(x,y)) {
        //x and y are within this component's bounds
    }
}

Humm, clever. :slight_smile:

So next up is I create an instance of each menu item, so that I know it’s dimensions and then place that on the canvas? (Currently I’m just printing the text directly with drawString(), though I want to change it to actually use my own letters from a sprite, eventually)

Okay, so I’m working on my Entity class and can’t really figure out what else I need. Also I’m not quite sure how to actually make a class that uses this to create menu buttons.

This is my entity class:


public abstract class Entity {
	private BufferedImage sprite;
	private Rectangle bounds;
	
	public abstract void update();
	public abstract void render();
}

I kind of want something like:

Entity button = new MenuButton("settings", posx, posy); 

So that I can store all the entities in a list of some sort.

But I’m not quite sure of how to turn that into a buffered image of the right size. Also I probably need a reference to the canvas so that the button can render itself?

It would have to be “public abstract void render(Graphics2D g)” and then you pass the Graphics2D of the Canvas’s BufferStrategy :slight_smile:

I just want to point out two additional practices to consider, for being able to track the source of an event. Maybe you are already aware of them.

One method is to make a unique MouseListener for each button. It’s not too bad, in terms of having to write so many Listeners, if you make each one an anonymous method directly attached to its “MenuButton” or whatever you are creating.

The second method is to assign an ActionCommand to your MenuButton.

myMenuButton.setActionCommand(identifyingString);

And then, in the common Listener being used for all the Events, extract the ActionCommand from the Event, and use that (perhaps in a Select Case structure) to choose what to do.

Testing for coordinates is a neat method, and one I’ve seen used in a code sample from my “Core Java” book. It should work for objects that have coordinates and a dimension. But if your object is being positioned via a LayoutStrategy, it might not be the easiest thing to implement.

if use java 7 :wink:

Well this wouldn’t be the first time I’ve gotten mixed up. What did I blow this time?

The command is a string, and you can only switch on strings in Java 7. You can always use a Map, which is probably more flexible in the long run anyway.

Alright, I’ll give it a go. I think I’ve finally figured out how I want to create the menu buttons. Just a few more steps and I have a menu that actually looks “good” and works with mouse input as well! Yay.

Thanks! :smiley: