Graph, Drawing and my mouse. How do i do it?

I am currently attempting to write an application that allows me to draw a graph. I want the user to be able to click on the parts of the screen that they want to draw it (much like a paint application). I also want the users to be able to move the components of the graph as well as delete components. The application will be used to display traffic flow data so each vertex represents a junction and the link represent roads.

I have attempted to create a custom vertex component that extends Jbutton (to have the functionality of being able to click on and move it around etc). I have modified the paintComponent method to display the component how I like (just a simple 10 pixel dot, although it could change).

When I click the panel to draw it normally draws in the wrong place, always around 40 pixels higher up, when I resize the window they all disappear, but when i click to draw one they all reappear but now it draws it even higher up.

I抦 starting to get more and more baffled and I fear that I抦 going to go insane.

I feel that there is no need to upload my code because it抯 not really working.

My question really is what is the best way to tackle this problem , where do i find information and have i gone about it all wrong?

[1277 byte] By [Carpediema] at [2007-11-26 13:55:32]
# 1
1. The question would be better placed in a gui forum.2. A simple example that demonstrates your problem is your best bet. In creating the simple example you might find the problem, if not someone else here, might be able to quickly look at it and find the problem.
zadoka at 2007-7-8 1:34:38 > top of Java-index,Java Essentials,Java Programming...
# 2
Here is a quick screen shoot that i created just to give the idea of what i want to do. http://www.flickr.com/photos/75087136@N00/342602901/zadok thanks for your reply!!!
Carpediema at 2007-7-8 1:34:38 > top of Java-index,Java Essentials,Java Programming...
# 3

JButton probably isn't the right choice. You need to use MouseListener and MouseMotionListeners to track your mousing activities. You will also probably have to draw temporary stuff (like rubber-band lines) on the GlassPane. The GlassPane is associated with the basic window.

You may want to use a custom JComponent and do all the drawing yourself in paintComponent, or you may want to use, say a JPanel, and the various objects you insert could be a mixture of children (icons, boxes etc.) and direct painting (lines and arrows).

Mouse event positions are relative to the Component that is the source of the event. There are useful methods in SwingUtilities for converting mouse coordinates between nested components.

malcolmmca at 2007-7-8 1:34:38 > top of Java-index,Java Essentials,Java Programming...
# 4

Thank that is really helpfull, it makes more sense now. I'm going to give it all a try now.

Also when you say that i need to track the mouse activities is it that i need to manually check the position of the click against a list of all the components that have been drawn and if it finds one then thats where the user clicked? hope that makes sense.

Carpediema at 2007-7-8 1:34:38 > top of Java-index,Java Essentials,Java Programming...
# 5
> is it that i need to manually check the position of the click against a list of all the components No, you can just add a MouseListener to each component.
camickra at 2007-7-8 1:34:38 > top of Java-index,Java Essentials,Java Programming...