Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Clickable Graphics  RSS feed

 
Derek Williams
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am making a simple memory game with a GUI. I am progressing along, but I am not sure how to handle the cards on the screen. Should I use graphics objects and add mouse listeners to detect which ones I have clicked so I can display a picture file icon or is there a more efficient way that bundles what I need to do in one class?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I use graphics objects and add mouse listeners to detect which ones I have clicked so I can display a picture file icon or is there a more efficient way that bundles what I need to do in one class?
It's possible to put everything in a single class but it can become unwieldy. It is easy enough to factor your code into classes by function: gui controls, graphic component and mouse code. The two general approaches to what I think you are describing are:
1 — graphics approach in which you draw everything in a graphic component that, generally, extends JPanel. You add the mouseListener/mouseMotionListeners to this component and find your way around to the graphic elements in them.
2 — component approach in which you use components, usually JLabels with ImageIcons to hold the images, and move these around with your mouse code using a null layout on the parent container and absolute positioning (setBounds, setLocation, etc) for the components.
Either way works well and seems to be a matter of personal preference. Efficiency amd ease seem to come with practice.
 
Derek Williams
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I hear what you are saying. If I go the Label approach with an image icon and I have 24 cards I am displaying do I need 24 action listeners for when they are clicked to make them visible? Do I then need another 24 to hide them if a match is not found? I am making an array of label objects and displaying them, but I am not sure how to code in the array loop to reference the action listeners to support 24 objects. I am assuming I need 24 action listeners and I can put code to decide to make them visible or not depending on other factors.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think JLabel will take an ActionListener. You could add a MouseListener to the parent panel and run through the labels (getComponents) in a loop, getBounds for each label and test if the mouse point is inside the bounds with rectangle.contains(point). Depends on how you have the labels laid out in the parent, if they are stationary or moving, etc. Sometimes you can have a fixed number of JLabels in place in the parent container and switch the ImageIcons (images) around in them for card changes. If you want to add listeners to each label you will need to make arrangements for them to be focusable so they can generate MouseEvents. The getFocusable method may do this well enough. If this seems too vague then I'd try a small test app with a few labels to see what will suit you.
 
Derek Williams
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I went the route of using JButtons and changing the icon pictures during the game I think that might work.

I am still not sure how the action listeners would work. Would I still need 24(one for each button) or can I pass arguments to one listener. I think I would need a listener for each of the 24.

If you already answered this and I missed it I apologize. Thanks for your help.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Easier way to do it use the findComponentAt() in the Container class.
Add a mouse listener so as to get the x and y co-ordinates.
Then call the findComponentAt(int x,int y)
Once you have identified the component on which the user clicked, you can process it according to your requirement.
No need to add multiple listeners.
Best of luck.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I went the route of using JButtons and changing the icon pictures during the game I think that might work.
Okay.
I am still not sure how the action listeners would work. Would I still need 24(one for each button) or can I pass arguments to one listener. I think I would need a listener for each of the 24.
I think you could do it either way.
If you used a single ActionListener and add it to all the buttons then you could find out which button the user clicked like this:

The 24 listeners can work just as well. Then you won't have to find out which button sent the message.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!