Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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 1189 times)
0 Members and 1 Guest are viewing this topic.
Offline Regenuluz
« Posted 2012-01-30 21: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: 322
Projects: 2
Exp: 4 years


I'm the King!


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

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

Offline Regenuluz
« Reply #2 - Posted 2012-01-30 21: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: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #3 - Posted 2012-01-30 21: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 22: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 21: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: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #6 - Posted 2012-01-31 22: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-02-01 00: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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ReBirth
« Reply #8 - Posted 2012-02-01 01: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 02: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?

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie
« Reply #10 - Posted 2012-02-01 17: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 21: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.

xsi3rr4x (25 views)
2014-04-15 18:08:23

BurntPizza (20 views)
2014-04-15 03:46:01

UprightPath (36 views)
2014-04-14 17:39:50

UprightPath (18 views)
2014-04-14 17:35:47

Porlus (34 views)
2014-04-14 15:48:38

tom_mai78101 (60 views)
2014-04-10 04:04:31

BurntPizza (118 views)
2014-04-08 23:06:04

tom_mai78101 (218 views)
2014-04-05 13:34:39

trollwarrior1 (185 views)
2014-04-04 12:06:45

CJLetsGame (192 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!