Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
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  
  java.awt.Shape and mouse event  (Read 1863 times)
0 Members and 1 Guest are viewing this topic.
Offline zilik

Junior Newbie

« Posted 2012-02-27 20:00:55 »

Hey guys!

I've got a little problem and I'd like to seek your advice on it. I have a few circles (Ellipse2D.Float) drawn to a JPanel. Those circles should change their size/color when the mouse hovers over them or the player clicks on them, but I can not figure out how to implement it.

All the circles have their fixed location while the panel's dimension is also fixed at 600*600.

I've tried two things so far, my first idea was to use MouseInfo's getPointerInfo() to check if any circle contains the current location of the cursor, but I don't really like that solution. I also tried to create a custom event, but I had never done it before and I couldn't make it to work.

How would you implement it?

Thank you in advance!
Offline CaptainJester

JGO Knight

Medals: 12
Projects: 2
Exp: 14 years

Make it work; make it better.

« Reply #1 - Posted 2012-02-28 04:27:11 »

Use MouseMotionListener. If you are using a game loop update the mouse position in the MouseMotionListener methods. In your game loop check the mouse position against your Shapes.

Offline zilik

Junior Newbie

« Reply #2 - Posted 2012-02-28 12:10:14 »

Thank you for your answer!

The solution you mentioned is exactly the same what I did, but I didn't really liked it. For example if I have 200 circles I have to loop through them in every game loop, which doesn't seems like a correct solution for me. I was thinking about something like MouseListener's mouseEntered() method used on a JPanel where the cursor's location triggers the event by hovering over the panel, but I could't make it work as I wanted.

Any advice on it?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel

Medals: 389
Projects: 3
Exp: 5 years

I'm the King!

« Reply #3 - Posted 2012-02-29 04:23:22 »

You add a MouseMotionListener to the component that you draw on:
myComponent.addMouseMotionListener(new MyMotionListener());

private class MyMotionListener implements MouseMotionListener {
    public void mouseDragged(MouseEvent me) {
    public void mouseMoved(MouseEvent me) {
        for(MyCircle c : circleList) {

mouseMoved(MouseEvent) is called every single time the mouse is moved.

Offline zilik

Junior Newbie

« Reply #4 - Posted 2012-02-29 15:31:17 »

Thanks guys!

Seems like I got to stay with that solution.

If I do this brute-force check every time, should I divide the panel into smaller rectangles, like 3 600*200 pieces, check the cursor position against them first and then do the check on the circles that are in those pieces? Or it would be pointless?
Offline Waterwolf

Junior Devvie

Medals: 3

« Reply #5 - Posted 2012-02-29 16:02:23 »

Are you having performance problems with the current solution?
Offline zilik

Junior Newbie

« Reply #6 - Posted 2012-02-29 16:22:04 »

No, I just don't like it, and I couldn't make any better.

Imagine a GUI with some buttons on it, where you would like to check if the user clicks on any of them (or highlight them if the cursor hovers over them). Would you do it this way? Just doesn't seems right.  Huh
Offline ra4king

JGO Kernel

Medals: 389
Projects: 3
Exp: 5 years

I'm the King!

« Reply #7 - Posted 2012-02-29 18:44:24 »

Are those circles evenly divided? Could you provide an image of what you want?

Offline zilik

Junior Newbie

« Reply #8 - Posted 2012-02-29 19:43:23 »

Are those circles evenly divided?
Kind of. I'm working on a Mill/Mills/Merels game.


I'm sorry if I was unclear. I'd like those circles (shapes) to act like, lets say a JToggleButton which has 3 states based on mouse interact (idle, mouse over and pressed). Despite the fact that I have only 24 circles here, I'd like to work out a solution which can be applied to more of them, without the need of looping through an array/list with hundreds of elements every time. A circle is defined as a class.

I'm kind of new to game developement (as you might have noticed Tongue), I do not know things in concepts, that is why I wrote every time that I don't like the solution.
Offline ra4king

JGO Kernel

Medals: 389
Projects: 3
Exp: 5 years

I'm the King!

« Reply #9 - Posted 2012-02-29 19:54:12 »

Image is broken.

However if I understand correctly, if the images are evenly spaced, then all you need to do is basically convert that X and Y position to a grid X and Y position then test containment:
int gridX = me.getX() / GRID_WIDTH;
int gridY = me.getY() / GRID_HEIGHT;


Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Jesse (11 views)
2015-07-29 04:35:27

Riven (33 views)
2015-07-27 16:38:00

Riven (17 views)
2015-07-27 15:35:20

Riven (20 views)
2015-07-27 12:26:13

Riven (10 views)
2015-07-27 12:23:39

BurntPizza (28 views)
2015-07-25 00:14:37

BurntPizza (39 views)
2015-07-24 22:06:39

BurntPizza (22 views)
2015-07-24 06:06:53

NoxInc (26 views)
2015-07-22 22:16:53

NoxInc (17 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!