Drawing on the canvas
I'm doing a graph theory project for my senior project that is required for graduation and i'm doing pretty well in learning swing as a beginner, but i need some help. I need to learn how to be able to draw objects on a canvas object. These are the parameters:
1) I need to be able to draw nodes (in the case of my project, circles) onto the canvas
2) The nodes must be able to be placed at whatever location the user wants, in case the user wants to move the node to another location.
3) The edges (lines) that connect the nodes must stay with that node (i.e. the xn,yn endpoint of the edge must correlate to the center of the node being moved)
For the nodes, i plan on having a Graphics object associated to each node so i won't have too much to keep track of. If anyone has any suggestions or any alternate ideas, that would be great
[870 byte] By [
kynphleea] at [2007-10-3 5:04:26]

The Java 2D tutorial is a good place to start. Also, when you install the JDK, it comes with a Java2D demo (in the demos directory) and all the source code. Check it out and look at some basic Java2D sample code, it should get you started if not most of the way there. Post any errors/problems you have along the way and we can help. Good luck!
cool.....i appreciate the help.....i looked through the demos, and they're helpful.....but there's only an applet version for one of the demos (C:\Program Files\Java\jdk1.5.0_06\demo\applets\DrawTest) and that's the only way that i've seen the Graphics class utilized......although I have to make an applet version, i also want to know how to use the same methods in an application i.e. how do i turn an applet into an application?
Check out the GraphLayout folder in the jdk1.5.0_06\demo\applets folder. It has four examples that might give you some ideas about class organization, graphics and event code. Also, you can find more model code in the jdk1.5.0_06\demo\plugin\jfc\Java2D folder. The application (vis-a-vis applet) is Java2DDemo.java in the src folder.
There are plenty of resources along these lines available on the internet, easily accessible via Google.
If you want to learn java I would recommend that you start with a simple application and gradually build it up in steps. To get started you could make an app with a JFrame and:
1 —create a graphic component by extending JPanel and overriding the paintComponent method
2 — draw one or more nodes and edges as you like
3 — now you have to decide how you want the user to interact with your app — one approach could be to add a node with the mouse.
So you could add a MouseListener to your graphic component and draw a new node for a mousePressed event.
4 — another choice: how will you store the nodes that have been added so that each one will be rendered each time paintComponent is called? You could keep the nodes in an ArrayList or, if you would rather avoid collections, in an array.
Keep putting things together like this and pretty soon you're done.
The tutorial has lessons such as [url=http://java.sun.com/docs/books/tutorial/uiswing/14painting/index.html]Performing Custom Painting[/url] and [url=http://java.sun.com/docs/books/tutorial/uiswing/14painting/practice.html]Implementing a Custom Component[/url] that might be useful to you.