Custom actionListener on a Rectangle2D.Double

C

Chris Lawson

Hi,

I am trying to add an actionListener to a Rectangle2D.Double class.
What i wish to accomplish is to have small square shapes on the screen
that can call a method when clicked. I don't want any writing in them,
when i tried doing this with a button object the buttons wouldn't show
up on the screen unless they had text asigned to them.

Is there a way to add an actionListener to the Rectangle2D.Double
class. I'm guessing that i will need to make a new class and extend
some other class. Not to sure though.

Any help would be greatly appreciated.

Kind Regards
 
A

Andrew Thompson

Chris said:
Hi,

I am trying to add an actionListener to a Rectangle2D.Double class.
What i wish to accomplish is to have small square shapes on the screen
that can call a method when clicked.

One way to do this is to attach a MouseListener
to a Panel, when you detect a MouseEvent,
get it's x and y and check that against the bounds
of your rectangle.

HTH
 
A

Andrew Hobbs

Chris Lawson said:
Hi,

I am trying to add an actionListener to a Rectangle2D.Double class.
What i wish to accomplish is to have small square shapes on the screen
that can call a method when clicked. I don't want any writing in them,
when i tried doing this with a button object the buttons wouldn't show
up on the screen unless they had text asigned to them.

Is there a way to add an actionListener to the Rectangle2D.Double
class. I'm guessing that i will need to make a new class and extend
some other class. Not to sure though.

actionListeners are really only intended to work with AWT or Swing
components (derived from Component or JComponent). However deriving your
rectangles from Component or using JPanels etc is a bit of an overkill with
a lot of probably unnecessary baggage. As Andrew Thompson indicated putting
the actionListener on the Panel and then working out which rectangle was
selected is the way to go. If you want more information on this type of
approach then you should do a web search for sprite and sprite animation
etc.with java.

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top