Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Troubles with mouse events in Java 2D

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answered in cross-post here:

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


    Bookmark Topic Watch Topic
  • New Topic