Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Costum gui elements  (Read 599 times)
0 Members and 2 Guests are viewing this topic.
Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« on: 2012-01-30 15: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
*****

Posts: 3153
Medals: 196


I'm the King!


« Reply #1 on: 2012-01-30 15:36:35 »

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

Offline Regenuluz

Jr. Member
**

Posts: 73
Medals: 1



« Reply #2 on: 2012-01-30 15: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! Go get 'em!
Offline ra4king

JGO Kernel
*****

Posts: 3153
Medals: 196


I'm the King!


« Reply #3 on: 2012-01-30 15: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

Jr. Member
**

Posts: 73
Medals: 1



« Reply #4 on: 2012-01-30 16: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

Jr. Member
**

Posts: 73
Medals: 1



« Reply #5 on: 2012-01-31 15: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
*****

Posts: 3153
Medals: 196


I'm the King!


« Reply #6 on: 2012-01-31 16: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

Sr. Member
**

Posts: 493
Medals: 24



« Reply #7 on: 2012-01-31 18: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.

"Life is short, art long, opportunity fleeting, experience treacherous, judgment difficult." Smiley
Offline ReBirth

JGO Wizard
****

Posts: 1272
Medals: 19



« Reply #8 on: 2012-01-31 19: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

Sr. Member
**

Posts: 493
Medals: 24



« Reply #9 on: 2012-01-31 20: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?

"Life is short, art long, opportunity fleeting, experience treacherous, judgment difficult." Smiley
Games published by our own members! Go get 'em!
Offline sproingie

JGO Strike Force
***

Posts: 894
Medals: 55



« Reply #10 on: 2012-02-01 11: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

Jr. Member
**

Posts: 73
Medals: 1



« Reply #11 on: 2012-02-01 15: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]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.093 seconds with 19 queries.