aspose file tools*
The moose likes Java in General and the fly likes objects with listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "objects with listeners" Watch "objects with listeners" New topic
Author

objects with listeners

rob armstrong
Ranch Hand

Joined: Jul 25, 2005
Posts: 77
I want to make rectangle2D and have a few extra charteristics with is...
public class modrectangle extends Rectangle2D
{}
However I also want tooltips with it which is not part of the AWT group.
Also I want to have listeners for each side of the rectangle when hit by the mouse (mouseclicked) and (mouseentered). So do you write it like this?
public class modrectangle extends Rectangle2D implements MouseMotionListener
{}
or do i put the listener in the class that adds this new object into it?
thanks
roba
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Sounds like you want some sort of JComponent not a Rectangle2D. What are you trying to do?
rob armstrong
Ranch Hand

Joined: Jul 25, 2005
Posts: 77
Trying to make a rectangle shape car with several attributes. When you move the mouse on the specific car object it will show some info pertaining to that instance of car. I want to use a for statement to whip up 100's of cars though.
roba
rob armstrong
Ranch Hand

Joined: Jul 25, 2005
Posts: 77
Thinking on what you said should i extend JComponent so i get all those goodies i need than make a rectangle object in that class?
I suppose that is how they do it when sun designed a JButton so i guess that is how you do it right?
roba
rob armstrong
Ranch Hand

Joined: Jul 25, 2005
Posts: 77
Should can I have the extened JComonent draw itself like this?
class Window extends JFrame
{
public void paint(Graphics g)
{} // etc...
}
or can i do this?
class modrectangle extends JComponent
{
public void paint(Graphics g)
{} etc....
}
The reason why i want the object to paint itself is some modrectangle objects may be 2 rectangles that travel together (ie truck and trailer) so i want the object itself to display what is needed. Does this look right to anyone?
Thanks roba
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by rob armstrong:
Thinking on what you said should i extend JComponent so i get all those goodies i need than make a rectangle object in that class?
I suppose that is how they do it when sun designed a JButton so i guess that is how you do it right?
roba


As much as I dislike extending JComponent, yes. Swing locks you into using concrete inheritance at some point, so you might as well bite the bullet and make the GUI component a JComponent. I'd be careful to make sure no non-GUI code is in the GUI component though.

Originally posted by rob armstrong:
Trying to make a rectangle shape car with several attributes. When you move the mouse on the specific car object it will show some info pertaining to that instance of car. I want to use a for statement to whip up 100's of cars though.
roba


My initial inclination is that each car might be a JComponent that delegates drawing to some kind of "VehicleUI" that can draw an appropriate vehicle. For example, you might have a "SedanUI" to draw a sedan and a "PickupTruckUI" to draw a pickup truck. The component itself might have code that implements tooltips based upon some other model backing it. For example, you create a VehicleComponent that is backed by a Vehicle and the VehicleComponent delegates painting to it's UI based upon the type of Vehicle and has a tooltip that shows a description that can be retrieved from the Vehicle backing. You might even have something that draws a price or name too.

Originally posted by rob armstrong:
Should can I have the extened JComonent draw itself like this?
class Window extends JFrame
{
public void paint(Graphics g)
{} // etc...
}
or can i do this?
class modrectangle extends JComponent
{
public void paint(Graphics g)
{} etc....
}
The reason why i want the object to paint itself is some modrectangle objects may be 2 rectangles that travel together (ie truck and trailer) so i want the object itself to display what is needed. Does this look right to anyone?
Thanks roba


I think my other replies might help with this. Yes, you can have a JComponent that overrides the paint(Graphics) and does custom painting. Generally you want to do custom painting in paintComponent(Graphics) instead though. Sun's website has some tutorials that include examples of custom painting.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: objects with listeners
 
Similar Threads
Funny JFrame behaviour
Rectangle drawn when mouse is dragged
One class calling another class incharge of GUI
A Newb JMS question - trying to use observer pattern with MDB onMessage?
Listeners in controller class