Is it at all possible to add eventlisteners to custom objects like strings or rectangles that enclose strings? What I want to achieve can be demonstrated by this image, the red dot would move up/down as up/down is pressed and E selects the current item. Once an item is selected the appropriate action is taken.
Use images. I’m sure there’s a way to draw strings in different colors, probably using
g.setColor(COLOR.WHATEVER)
but I don’t like using straight up Java2D to do anything except for rendering my bufferedimage.
Actually I take that back. I used to render text menus in Java2D like this:
public void renderText(String text, int x, int y, int size, int style, int color) {
int r = (color & 0xff0000) >> 16;
int g = (color & 0xff00) >> 8;
int b = (color & 0xff);
Color c = new Color(r, g, b);
Font f = new Font("Verdana", style, size);
this.g.setColor(c);
this.g.setFont(f);
this.g.drawString(text, x, y);
}
It’s pretty self explanatory.
The drawing is fine, I can manage that atm, but the problem is adding actionevents to the strings.
Dont use action events. Just draw a circle and say when down arrow key is pressed, move circle draw location down next to the other string.
Easy method is having your Canvas/JPanel/whatever implement an event Listener and test for the condition.
E.g. mouse clicks for intersection with a rectangle around your text:
Rectangle buttonArea; //initialized elsewhere for readability
public void mousePressed(MouseEvent e) {
if(buttonArea.contains(e.getPoint())) {
//do stuff here, like flip to next screen, start game, etc.
}
}
Just use KeyListener for keys, and change which String the dot is drawn next to, and also test for ‘E’ being pressed, and have that do whatever action you need.