Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Troubles with mouse events in Java 2D  RSS feed

 
Taina Coimbra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm new to the forums, so please be nice and tell me if I've made this topic on the wrong place or something like that!

I have some programming experience in C++ and Actionscript 3, but these days I got a sudden wish to learn java and here I am!

First, I'll tell you what my application should do, and then I'll tell what it isn't doing:
I need an application that draws some circles on the screen and, when two circles are clicked (one after another), a line is plotted between them. Simple, right?

I'm using swing to make an interface and Java2D to handle the 'draw circles'-stuff. I must say there was a hard path to take 'till this worked, but at the moment my application does this: reads an text file with the circles coordinates and plots them on the screen.

I'm having trouble with the Mouse Listenning methods. In other languages I have worked, actionscript mainly, I was able to add mouse event listeners to almost anything. Unfortunately, it seems I can't add a mouse event listener to a shape object (I'm using an Ellipse2D). After a day searching for solutions, I got nothing. The best I could do was to add a mouse listener to the frame on which the circles are drawn, and try to identify the clicked ellipse with the "contains" method. Something like: if(ellipse.contains(event.getPoint())){ do something }
But that isn't working either. I'll show you a simple picture of what's happening:



If I click in (a) or in (b), it works: these points are inside the ellipse. However, in (c) (which, in my vision, is inside the ellipse) the above statement returns false!

I'm sorry if I wasn't clear enough, but thanks for lending me some of your time on this issue! It's a really simple application, but this mouse event things are giving me a hard time. In AS3, I could treat anything like a button (yeah, that's not a right thing to do, but when you are in need there are some things that should be done), and though some button methods worked for me on java (with ActionListener), I wasn't able to use them on the shapes. I thought it could be done with something like circle.addActionListener(methodToCall()), but it's not like that.

I'm willing to change all my code: maybe I'm 'thinking wrong' and using shapes to draw isn't the right way to address the problem. However, it seems to me that this is the right approach, since I basically need some circles and some lines on the screen.

I thank you for any advices.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answered in cross-post here: http://forums.sun.com/thread.jspa?threadID=5449509&tstart=0

OP, please read the FAQ, and in particular the one about cross-posting without notification. It will explain the issues involved.

BeForthrightWhenCrossPostingToOtherSites

Luck.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!