Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Costum gui elements  (Read 1309 times)
0 Members and 1 Guest are viewing this topic.
Offline Regenuluz
« Posted 2012-01-30 20:25:35 »

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? Smiley
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-01-30 20:36:35 »

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

Offline Regenuluz
« Reply #2 - Posted 2012-01-30 20:40:03 »

That's what I'm using. Smiley

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-01-30 20:56:38 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
class Entity {
    Rectangle bounds = …
}

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

Offline Regenuluz
« Reply #4 - Posted 2012-01-30 21:10:02 »

Humm, clever. Smiley

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)
Offline Regenuluz
« Reply #5 - Posted 2012-01-31 20:12:46 »

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:
1  
2  
3  
4  
5  
6  
7  
public abstract class Entity {
   private BufferedImage sprite;
   private Rectangle bounds;
   
   public abstract void update();
   public abstract void render();
}


I kind of want something like:
1  
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?

Offline ra4king

JGO Kernel


Medals: 353
Projects: 3
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-01-31 21:58:22 »

It would have to be "public abstract void render(Graphics2D g)" and then you pass the Graphics2D of the Canvas's BufferStrategy Smiley

Offline philfrei
« Reply #7 - Posted 2012-01-31 23:46:54 »

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.

"It's after the end of the world! Don't you know that yet?"
Offline ReBirth
« Reply #8 - Posted 2012-02-01 00:02:52 »

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.
if use java 7 Wink

Offline philfrei
« Reply #9 - Posted 2012-02-01 01:34:42 »

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.
if use java 7 Wink

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

"It's after the end of the world! Don't you know that yet?"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #10 - Posted 2012-02-01 16:35:18 »

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.
Offline Regenuluz
« Reply #11 - Posted 2012-02-01 20:55:59 »

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! Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

DarkCart (24 views)
2014-10-31 21:44:48

DarkCart (30 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (31 views)
2014-10-27 03:27:51

DarkCart (45 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (31 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!