Class GraphPanel

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----java.awt.Panel
                           |
                           +----GraphPanel

class GraphPanel
extends Panel
implements Runnable
Run the animation as a separate thread.


Variable Index

 o arcColor1
 o arcColor2
 o arcColor3
 o edgeColor
 o edges
 o fixedColor
 o graph
 o nedges
 o nnodes
 o nodeColor
 o nodes
 o offgraphics
 o offscreen
 o offscreensize
 o pick
 o pickfixed
 o random
 o relaxer
 o selectColor
 o stress
 o stressColor

Constructor Index

 o GraphPanel(Graph)

Method Index

 o addEdge(String, String, int)
Add the edge in between 2 nodes.
 o addNode(String)
Add the new node in a graph, use random() function to create x and y coordinates.
 o findNode(String)
Find if the node is present in a graph.
 o mouseDown(Event, int, int)
This Mouse event is activated when the mouse button is pressed, takes x and y coordinates when the mouseDown event occurred.
 o mouseDrag(Event, int, int)
Handles mouse movements made with the mouse button pressed down
 o mouseUp(Event, int, int)
Invoked when the mouse button is released.
 o paintNode(Graphics, Node, FontMetrics)
Draw the graph node.
 o relax()
To avoid race condition between each thread and to preserve data integrity of the shared object, the method, relax is synchronized This method settles the nodes of the polygon e.g.
 o run()
The thread behavior is implemented here.
 o start()
start the new thread.
 o stop()
stop the thread.
 o update(Graphics)
Update method is invoked by repaint method.

Variables

 o graph
 Graph graph
 o nnodes
 int nnodes
 o nodes
 Node nodes[]
 o nedges
 int nedges
 o edges
 Edge edges[]
 o relaxer
 Thread relaxer
 o stress
 boolean stress
 o random
 boolean random
 o pick
 Node pick
 o pickfixed
 boolean pickfixed
 o offscreen
 Image offscreen
 o offscreensize
 Dimension offscreensize
 o offgraphics
 Graphics offgraphics
 o fixedColor
 final Color fixedColor
 o selectColor
 final Color selectColor
 o edgeColor
 final Color edgeColor
 o nodeColor
 final Color nodeColor
 o stressColor
 final Color stressColor
 o arcColor1
 final Color arcColor1
 o arcColor2
 final Color arcColor2
 o arcColor3
 final Color arcColor3

Constructors

 o GraphPanel
 GraphPanel(Graph graph)

Methods

 o findNode
 int findNode(String lbl)
Find if the node is present in a graph. If yes, return the index, else add node to a graph.

Parameters:
lbl - The label of the node.
Returns:
The index of the node.
See Also:
addNode
 o addNode
 int addNode(String lbl)
Add the new node in a graph, use random() function to create x and y coordinates.

Parameters:
lbl - he label of the node.
Returns:
The node which is just added.
 o addEdge
 void addEdge(String from,
              String to,
              int len)
Add the edge in between 2 nodes.

Parameters:
from - From which node.
to - To which node.
len - Length of the edge.
See Also:
findNode
 o run
 public void run()
The thread behavior is implemented here. Run method contains an infinite loop. In each iteration of the loop, the thread sleeps for 100 milliseconds.

 o relax
 synchronized void relax()
To avoid race condition between each thread and to preserve data integrity of the shared object, the method, relax is synchronized This method settles the nodes of the polygon e.g. if the polygon has 6 nodes, then it will change the position of the nodes such that, after the polygon settles down, polygon will have a shape of hexagon.

 o paintNode
 public void paintNode(Graphics g,
                       Node n,
                       FontMetrics fm)
Draw the graph node. Called by the update method.

Parameters:
g - The graphics object.
n - The n'th graph node.
fm - The FontMetrics.
See Also:
update
 o update
 public synchronized void update(Graphics g)
Update method is invoked by repaint method. The update method erases any drawing that was previously done on the applet. It creates the new drawing in the off-screen buffer and then display the off-screen image onto the window.

Parameters:
g - The graphics object.
Overrides:
update in class Component
 o mouseDown
 public synchronized boolean mouseDown(Event evt,
                                       int x,
                                       int y)
This Mouse event is activated when the mouse button is pressed, takes x and y coordinates when the mouseDown event occurred. It picks the node which is nearest to the mouse coordinates where mouse is pressed.

Parameters:
evt - The event.
x - The x coordinate of the mouse button pressed.
y - The y coordinate of the mouse buttom pressed.
Returns:
true.
Overrides:
mouseDown in class Component
 o mouseDrag
 public synchronized boolean mouseDrag(Event evt,
                                       int x,
                                       int y)
Handles mouse movements made with the mouse button pressed down

Parameters:
evt - The event.
x - The x coordinate of the new mouse position.
y - The y coordinate of the new mouse position.
Returns:
true.
Overrides:
mouseDrag in class Component
 o mouseUp
 public synchronized boolean mouseUp(Event evt,
                                     int x,
                                     int y)
Invoked when the mouse button is released.

Parameters:
evt - The event.
x - The x coordinate of the mouse button when released.
y - The y coordinate of the mouse button when released.
Returns:
true.
Overrides:
mouseUp in class Component
 o start
 public void start()
start the new thread.

 o stop
 public void stop()
stop the thread.