Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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 1447 times)
0 Members and 1 Guest are viewing this topic.
Offline zilik

Junior Newbie





« Posted 2012-02-27 21: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!
zilik
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2


Make it work; make it better.


« Reply #1 - Posted 2012-02-28 05: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 13: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: 322
Projects: 2
Exp: 4 years


I'm the King!


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

You add a MouseMotionListener to the component that you draw on:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
myComponent.addMouseMotionListener(new MyMotionListener());

private class MyMotionListener implements MouseMotionListener {
    public void mouseDragged(MouseEvent me) {
    }
   
    public void mouseMoved(MouseEvent me) {
        for(MyCircle c : circleList) {
            if(c.contains(me.getX(),me.getY())
                c.setHighlighted(true);
        }
    }
}


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

Offline zilik

Junior Newbie





« Reply #4 - Posted 2012-02-29 16: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 Member


Medals: 3



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

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

Junior Newbie





« Reply #6 - Posted 2012-02-29 17: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: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #7 - Posted 2012-02-29 19: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 20:43:23 »

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


link: http://noob.hu/2012/02/29/Merels_0.png

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: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #9 - Posted 2012-02-29 20: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:
1  
2  
3  
4  
5  
int gridX = me.getX() / GRID_WIDTH;
int gridY = me.getY() / GRID_HEIGHT;

if(circle[gridY][gridX].contains(me.getX(),me.getY()))
    //Collision!

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 (47 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

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
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!