Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

MouseListener on a Canvas Item

Thread view: 
Thierry St-Pierre - 07 Apr 2005 04:20 GMT
Hi

   What is the best way to deal with Mouse Events on Canvas Items?  I tried
make self contained objects with MouseListener drawn on the Canvas, but it
seems the MouseAdapter can't find them.  I tried to have my objects
extending Component or JButton in hope the compiler would create the missing
link, but no.  Since the number and location of items change each time, I
find it tedious (not mentioning stupid in an Object-Oriented environment) to
have the Canvas track all items.

Thanks,
Thierry
sanjay manohar - 07 Apr 2005 17:14 GMT
To have objects that respond to mouse events in the way you describe,
using a MouseListener:
1) They must be Components, as you correctly inferred, and therefore
ONLY A RECTANGULAR area will be clickable.
2) They must be added to the parent component. (This is so that java
knows the bounds of the component, and is able to automatically find
out which component has been clicked on).
3) You must register the mouse listener to each one. E.g. you could do
this in the constructor
4) Sounds like you are drawing on a Canvas, but in fact you should
paint each component on its own Graphics object (from the paint
method). If there is nothing else drawn on the canvas except for your
rectangular canvas items, then use a JPanel?
5) Note that you need to use different coordinate systems if your
components are extending Component.

e.g.
/*******************************/

JPanel container = new JPanel();
// This means I will specify the positions of
// each component explicitly
container.setLayout(null);
CanvasItem item1=new CanvasItem();
container.add(item);
// this controls both the location the item is painted,
// and the area that responds to mouse events.
item.setBounds(x,y,w,h);

class CanvasItem extends JComponent {
public CanvasItem(){
 addMouseListener(myMouseListener);
}
protected MouseListener myMouseListener = new MouseAdapter(){
 //add listener methods here
};
public void paint(Graphics g){
 //draw this item on its own graphics
}
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.