wood burning stoves 2.0
The moose likes Other JSE/JEE APIs and the fly likes Java 2D Shape classes (design question) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java 2D Shape classes (design question)" Watch "Java 2D Shape classes (design question)" New topic

Java 2D Shape classes (design question)

Ed Tidwell

Joined: Oct 22, 2002
Posts: 18
Hello everyone. Been riding the fence on a design decision and I was hoping someone could stear me in the right direction.
First has anyone worked with the 2DLine objects much? I've got a class that extends from the Rectangle shape but their is no easy way to extend a Line object. It basically has two static factory methods which kind of puts a cramp in the direction I was going with my Rectangle class.
I want a clean way via a mediator to handle events on objects drawn in a JPanel. They are loaded in via a file and drawn into a JPanel at the moment. My demo works fine and I'm happy with it but I'm worried about how it will scale. Right now I'm using the JPanel's paint(Graphic) to draw the images into.
I parse a file that contains a uniqueID, color and icon name. I basically need to allow them to click on a rectangle, then a line and make an entry into a JPanel(BorderLayout.South).add(myJTable).
What is the best way to handle the events?
Do I have this objects extend JComponent? I see a lot of examples that do. If I do that I can override the tooltip text messages for free and do some neat stuff.
Do I implement the objects as Shape and have the Graphics 'canvas' passed in and the object draw into it?
Can't decide but my deadline for the beta is before Thanksgiving so any help would be appreciated.
Been reading and looking at examples and can't find ANYTHING that is like what I want to do. I basically want to do something JUST LIKE TogetherJ with the mouse clicks on UML shapes updating via mediator other associated component views.
Thanks for reading. Look forward to your comments on this thread.
Ed Tidwell

Joined: Oct 22, 2002
Posts: 18
Just an FYI for anyone who may have this question.
I picked JComponent for several reasons. I get tooltips free. It offers double buffering. Mouse events are easy to register. Last but not least. I can set a Panel to the (null) layout manager and just do a panel.add(jcomponentShape).
I can add a border to do neat stuff. I can also just add a JLabel to to give the object a name.
Works out very well.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Thanks for reporting your decision - how many components are you going to be managing on a single display?
Ed Tidwell

Joined: Oct 22, 2002
Posts: 18
I don't see there ever being more than 50 items but on a rare occasion it could get that high. 100 items are the max.
I've basically built to ArrayList lookup tables. One for the rectangles and another for the lines connecting the rectangles.
For now I have all my rectangle objects as JPanels and set their x, y, width and height properties as well as color. Set the parent layout manager to be (null) for X Y layout and it works very well. It didn't even require me to do any code in the paint thread at all. Works pretty fast as well.
I don't need the user to move anything. Only click the appropiate rectangle and assign it a location.
Thanks for asking.
Ed Tidwell

Joined: Oct 22, 2002
Posts: 18
Just so some poor soul doesn't read this and spend a waste time.
I have rectangles and lines. The lines are drawn in the paintComponent thread of the parent panel.
Child rectangles are given a color, tooltip, JLabel name BorderLayout.Center and then parentPanel.add(child);
The lines that connect the rectangles get the center point from the JPanel rectangle for point1 and then the adjoining rectangle which is point2.
Anyway it works very well and I'm having a lot of fun with it.
I agree. Here's the link: http://aspose.com/file-tools
subject: Java 2D Shape classes (design question)
It's not a secret anymore!