home | hometown | racing |recent |resume |schedule |photos


g r a p h  a p p l e t

E d g e . j a v a

G r a p h . j a v a

G r a p h A p p l e t . j a v a

V e r t e x . j a v a

Edge.java

package graphapplet;
import java.util.*;
import java.awt.*;
//////////////////////////////////////////////////////////////////
// Edge Class - this class constructs and maintains an edge in a
// graph (Graph G) for the graph applet.  This Class idea was 
// inspired from a similar example at ===>
// http://www.cs.strath.ac.uk/teaching
//
// Vertex 1 and 2 are the nodes that the edge will connect.
// 
//
// Last Modified: 4/29/2000
//
// Author:  William Martin mail@WilliamMartin.com
// Original Author: http://www.cs.strath.ac.uk
//////////////////////////////////////////////////////////////////
public class Edge {
 private String id; 
 private Vertex v1, v2;
 private Color color;
 private Graph G;
 private Graphics g;
 private boolean mark;//has edge been visited
//////////////////////////////////////////////////////////////////
// Edge Method - This method uses the Applet class Graphics and
// uses it on this packages graph class.  Two vertexes are taken as
// paramenters containing the individual vertex information. this 
// method sets a new element (edge) into the graphs vector 
// containing edges. 
//
// Date Last Modified: 4/28/2000
//////////////////////////////////////////////////////////////////
public Edge(Graphics g, Graph G, Vertex v1, Vertex v2){
 this.g = g;
 this.G = G;
 this.v1 = v1;
 this.v2 = v2;
 id = "(" + v1.id() +","+ v2.id() + ")";
 v1.addAdjacentVertex(v2);
 v2.addAdjacentVertex(v1);
 mark = false;
 color = G.defaultColor();
 G.vectorOfEdges().addElement(this);
 paint();
}
//////////////////////////////////////////////////////////////////
// flash Method - this method provides a visual cue to which edge
// that the bfs search operation is traveling. This makes the
// edge flash 15 times.
//
// Last Modified: 4/28/2000
//////////////////////////////////////////////////////////////////
public void flash(){
  for (int i=0;i<15;i++){
      unPaint();
      G.pause(20);
      paint();
      G.pause(20);
  }
 }
//////////////////////////////////////////////////////////////////
// mark Method - this method returns tru or false.
// 1) true  -if edge has been visited
// 2) false -if edge has not been visited or has been reset
//
// Last Modified: 4/28/2000
//////////////////////////////////////////////////////////////////
 public boolean mark(){
  return mark;
 }
//////////////////////////////////////////////////////////////////
// paint Method- This method draws in the edge as a line.
//
// Last Modified: 4/25/2000
//////////////////////////////////////////////////////////////////
 public void paint() {
  g.setColor(color);
  g.drawLine(v1.xCoord(),v1.yCoord(),v2.xCoord(),v2.yCoord());
  v1.paint();
  v2.paint();
 } 
//////////////////////////////////////////////////////////////////
// reset Method - this method resets the edges back to their
// default color before beginning another bfs.
//
// Last Modified: 4/29/2000
//////////////////////////////////////////////////////////////////
 public void reset(){
  mark = false;
  color = G.defaultColor();
 }
//////////////////////////////////////////////////////////////////
// setColor Method - this method is used to set the color of the 
// edges.
//
// Last Modified:  4/19/2000
//////////////////////////////////////////////////////////////////
 public void setColor(Color color){
  this.color = color;
 }
//////////////////////////////////////////////////////////////////
// setMark Method - this method is used to set the edges mark 
// value to that which is specified in the calling object.
// 1) true = visited
// 2) flase = not visited or is reset 
//
// Last Modified:  4/19/2000
//////////////////////////////////////////////////////////////////
 public void setMark(boolean value){
  mark=value;
 }
//////////////////////////////////////////////////////////////////
// toString - this method is the ususl toString included with
// most classes.  In this case the id is returned
//
// Last Modified: 4/29/2000
//////////////////////////////////////////////////////////////////
 public String toString(){
  return id;
 }
//////////////////////////////////////////////////////////////////
// unPaint Method - this method unpaints the edge for the flash
// method.
//
// Last Modified: 4\29\2000
//////////////////////////////////////////////////////////////////
 public void unPaint(){
    g.setColor(Color.white);
    g.drawLine(v1.xCoord(),v1.yCoord(),v2.xCoord(),v2.yCoord());
  } 
//////////////////////////////////////////////////////////////////
// v1 Method - this method constructs a Vertex (1)
//
// Last Modified: 4/29/2000
//////////////////////////////////////////////////////////////////
 public Vertex v1(){return v1;}
//////////////////////////////////////////////////////////////////
// v2 Method - this method constructs a Vertex (2)
//
// Last Modified: 4/29/2000
//////////////////////////////////////////////////////////////////
 public Vertex v2(){return v2;}
}
[ TOP ]
Graph.java package graphapplet; import java.util.*; import java.applet.*; import java.awt.*; ///////////////////////////////////////////////////////////////// // Graph Class - This class is the core class for the graph applet. // this class contains the bfs search as well as the add vertices // and edges methods.this class was inspired by : // http://www.cs.strath.ac.uk/teaching/ug/classes/233/GraphDemo/GI.html // // Last Modified: 4/29/2000 // // Modified by: William Martin // Original Author: http://www.cs.strath.ac.uk ///////////////////////////////////////////////////////////////// public class Graph { private String id;//this graphs id private Vector vertices;//container for our vertices private Vector edges;// container for our edges private Color defaultColor = Color.red;//graph private Color searchColor = Color.yellow;//search private static int vertexNumber = 0;//how many verts ///////////////////////////////////////////////////////////////// // Graph Method - this method is a contructor for the graph class. // 1) give an id. // 2) set up a vector to contain vertices and edges // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public Graph(String s){ id = s; vertices = new Vector(); edges = new Vector(); } ///////////////////////////////////////////////////////////////// // absdiff Method - this method is a a abs(difference) function // essential for the findVertex Method. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// private int absdiff(int x, int y){ if (x<y) return y-x; else return x-y; } ///////////////////////////////////////////////////////////////// // addEdge Method - this method adds an edge into the graph. // RETURNS: an edge from vert.1 to vert.2. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public Edge addEdge(Graphics g, Vertex v1, Vertex v2){ Edge e = new Edge(g,this,v1,v2); return e; } ///////////////////////////////////////////////////////////////// // addNewElement Method - this method is essential to pass vertices // and edges into the container Vectors. These are treated as type // Object. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// private void addNewElement(Vector v, Object e){ if (!v.contains(e)) v.addElement(e); } ///////////////////////////////////////////////////////////////// // addVertex Method - this method adds a vertex into the graph. // INCREMENTS: vertexNumber // RETURNS: a new vertex @ coordinates x,y. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public Vertex addVertex(Graphics g, int x, int y){ vertexNumber++; Vertex v = new Vertex(g,this,vertexNumber,x,y); return v; } ///////////////////////////////////////////////////////////////// // bfs Method - this method is a breth first search algorythm // for this graph. // // while there is no more edges to explore // explore next edge and mark it as visited // while still more vertices // explore next vertex // if vertex is fresh // mark it visited and continue on // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void bfs(Vertex v) { Vertex v1, v2; Edge e; Vector edgesToExplore = new Vector(); edgesToExplore.addElement(v); while (!edgesToExplore.isEmpty()) { v1 = (Vertex) edgesToExplore.elementAt(0); edgesToExplore.removeElement(v1); v1.setMark(true); for (Enumeration E = v1.adjacentVertices(); E.hasMoreElements();) { v2 = (Vertex) E.nextElement(); if (!v2.mark()) { v2.setMark(true); e = findEdge(v1, v2); e.setMark(true); e.setColor(searchColor); e.flash(); pause(); edgesToExplore.addElement(v2); } } } } public Color defaultColor(){return defaultColor;} public Enumeration edges(){ return edges.elements();//so we can travers } ///////////////////////////////////////////////////////////////// // findEdge Method - this method checks for an edge and returns // it. If not found than null. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public Edge findEdge(Vertex v1, Vertex v2){ Edge e = null; boolean found = false; for (int i=0;i<edges.size() && !found;i++){ e = (Edge)edges.elementAt(i); found = (v1.equals(e.v1()) && v2.equals(e.v2())) || (v1.equals(e.v2()) && v2.equals(e.v1())); } if (!found) e=null; return e; } ///////////////////////////////////////////////////////////////// // findVertex Method - this method checks for a vertex and returns // it. If not found than null. This method is essential to find // vertices that are to be joined by a edge. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public Vertex findVertex(int x, int y){ Vertex v = null; boolean found = false; for (int i=0;i<vertices.size() && !found;i++){ v = (Vertex)vertices.elementAt(i); found = (absdiff(x,v.xCoord()) < 10 && absdiff(y,v.yCoord()) < 10); } if (!found) v=null; return v; } public String id(){return id;} ///////////////////////////////////////////////////////////////// // info Method - this method returns a string with information // on id and number of vertices. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public String info(){ String s = "Graph:"; s = s + " " + id; s = s + " n = " + vertices.size(); return s; } ///////////////////////////////////////////////////////////////// // paint Method - this method paints the vertices and edges in // the graph. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void paint(){ for (int i=0;i<vertices.size();i++) ((Vertex)vertices.elementAt(i)).paint(); for (int i=0;i<edges.size();i++) ((Edge)edges.elementAt(i)).paint(); } ///////////////////////////////////////////////////////////////// // pause Method - this method puts the process thread to sleep // momentarly. This method is essential for the visual bfs // search // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void pause(){ try{Thread.sleep(2);} catch (Exception e) {e.printStackTrace();} } ///////////////////////////////////////////////////////////////// // pause Method - this method puts the process thread to sleep // momentarly. The length of pause is defined by the incoming // parameter l (length). This method is essential for the visual // bfs search // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void pause(long l){ try{Thread.sleep(l);} catch (Exception e) {e.printStackTrace();} } ///////////////////////////////////////////////////////////////// // reset Method - this method resets each vertex and edge in the // graph. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void reset(){ for (int i=0;i<vertices.size();i++) ((Vertex)vertices.elementAt(i)).reset(); for (int i=0;i<edges.size();i++) ((Edge)edges.elementAt(i)).reset(); } public Color searchColor(){return searchColor;} public void setDefaultColor(Color color){ defaultColor = color;//vertexes and edges } public void setSearchColor(Color color){ searchColor = color;//for the visul bfs search } public Vector vectorOfEdges(){//container for our edges return edges; } public Vector vectorOfVertices(){//container for our vertices return vertices; } public Enumeration vertices(){ //with elements as Objects return vertices.elements();//useful for our bfs search } } [ TOP ]
GraphApplet.java package graphapplet; import java.applet.*; import java.awt.*; import java.awt.event.*; ///////////////////////////////////////////////////////////////// // GraphApplet Applet - this applet demonstrates, visually the // Graph, Edge, and Vertex classes. This applet takes user input // to construct a graph with vertexes and edges. The user can // then run a Breth First Search from any Vertice. This applet // was inspired by another similar applet at: // http://www.cs.strath.ac.uk/teaching/ug/classes/233/GraphDemo/GI.html // // Last Modified: 4/29/2000 // // Modified by: William Martin // Original Author: http://www.cs.strath.ac.uk ///////////////////////////////////////////////////////////////// public class GraphApplet extends Applet implements ActionListener, MouseListener{ Graph G; boolean addVertices, addEdges, bfs, info; TextField tf; Button graphChoice, graphChoice2, graphChoice3; private int x, y, ends; Vertex v1, v2; ////////////////////////////////////////////////////////////////// // actionPerformed Method - this method handles the action to be // taken when the mose is clicked under certain curcumstances: // 1) after the Add Edges button is clicked - Add Edge. // 2) after the Add Vertex button is clicked - Add Vertex. // 3) after the BFS button is clicked - do a bfs. // // Last Modified: 4/29/2000 ////////////////////////////////////////////////////////////////// public void actionPerformed (ActionEvent e) { if (e.getSource()== graphChoice2) setAddEdges(); else if (e.getSource()== graphChoice) setAddVertices(); else if (e.getSource()== graphChoice3){ textOut("Click a Vertex to start the search");//how to start setBfs(); } } ////////////////////////////////////////////////////////////////// // addEdge Method - this method finds the vertex at the point that // the mouse is clicked and either: // 1) marks it as the first vertex selected. // 2) marks it as the second vertex selected and sets ends to 2. // // if ends == 2 then connect the vertices. // // Last Modified: 4/29/2000 ////////////////////////////////////////////////////////////////// private void addEdge(Graphics g) { Vertex v = G.findVertex(x, y); if (v != null && ends == 1 && v != v1) { //disallow multi-edge! v2 = v; ends = 2; textOut("selected second vertex " + v2); } else if (v != null && ends == 0) { v1 = v; ends = 1; textOut("selected first vertex " + v1); } if (ends == 2) { if (G.findEdge(v1, v2) == null) { Edge E = G.addEdge(g, v1, v2); textOut("new edge " + E); } else textOut("edge already exists: " + v1 + " <-> " + v2); ends = 0; } } public boolean addEdges(){return addEdges;} ///////////////////////////////////////////////////////////////// // addVertex Method - uses the applet class to add a vertex to the // graph and then display the new id in the text window. // ends gets reset to = 0. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// private void addVertex(Graphics g){ ends = 0; Vertex v = G.addVertex(g,x,y); textOut("new vertex " + v); } public boolean addVertices(){return addVertices;} ///////////////////////////////////////////////////////////////// // bfs Method - finds the vertex that the mouse is clicking on // and starts the breath first search. The graph is reset and // painted before the visual search - just in case another search // was completed before leaving the edges colored // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// private void bfs(){ Vertex v = G.findVertex(x,y); if (v!=null){ G.reset(); G.paint(); G.bfs(v); } } public String getAppletInfo(){return G.id();} public boolean getBfs(){return bfs;} public boolean getInfo(){return info;} ///////////////////////////////////////////////////////////////// // init Method - this method sets up the applet. // 1) buttons are established(Add Vertices, add Edges, and BFS). // 2) mouse listener is started. // 3) a text area is places south. // 4) all flags (bfs = false, addEdges = false,and // addVertices = true{default starting mode}). // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void init (){ setLayout(new FlowLayout()); setBackground(Color.lightGray); G = new Graph("First"); graphChoice = new Button("Add Vertices"); add(graphChoice); graphChoice.addActionListener(this); graphChoice2 = new Button("Add Edges"); add(graphChoice2); graphChoice2.addActionListener(this); graphChoice3 = new Button("BFS"); add(graphChoice3); graphChoice3.addActionListener(this); addMouseListener(this); tf = new TextField(40); tf.setEditable(false); add("South",tf); addEdges = addVertices = false; addVertices = true; } public void mouseClicked(MouseEvent e){;} public void mouseEntered(MouseEvent e){;} public void mouseExited(MouseEvent e){;} ///////////////////////////////////////////////////////////////// // mousePressed Method - uses the MouseEvent method of the applet // class to: // 1) get the location of the mouse (at the pressed point). // 2) depending on the mode, direct traffic! // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void mousePressed(MouseEvent e){ Graphics g = getGraphics(); x = e.getX(); y = e.getY(); if (addVertices()) addVertex(g); else if (addEdges()) addEdge(g); else if (getInfo()) getInfo(); else if (getBfs()) bfs(); } public void mouseReleased(MouseEvent e){;} public void paint(Graphics g){G.paint();} ///////////////////////////////////////////////////////////////// // reset Method - this method simpily sets all flags to false! // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// private void reset(){ bfs = info = addVertices = addEdges = false; } ///////////////////////////////////////////////////////////////// // setAddEdges Method - this method sets all flags to false and // turns on the addEdges flag. Sets the mode to Add Edges mode. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void setAddEdges(){ reset(); addEdges = true; } ///////////////////////////////////////////////////////////////// // setAddVertices Method - this method sets all flags to false and // turns on the addVertices flag. Sets the mode to Add Vertices // mode. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void setAddVertices(){ reset(); addVertices = true; } ///////////////////////////////////////////////////////////////// // setbfs Method - this method sets all flags to false and // turns on the bfs flag. Sets the mode to BFS mode. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void setBfs(){ reset(); bfs = true; } ///////////////////////////////////////////////////////////////// // setInfo Method - this method sets all flags to false and // turns on the info flag. Sets the mode to info mode. // // Last Modified: 4/29/2000 ///////////////////////////////////////////////////////////////// public void setInfo(){ reset(); info = true; } public void start(){;} public void textOut(String s){tf.setText(s);} public void update(Graphics g){G.paint();} } [ TOP ]
Vertex.java package graphapplet; import java.util.*; import java.applet.*; import java.awt.*; ////////////////////////////////////////////////////////////////// // Vertex Class - this class constructs and maintains a vertex in a // graph (Graph G) for the graph applet. Inspirtation from ==> // http://www.cs.strath.ac.uk/teaching // // x, y coord is the location of vertex in the applet pane. // // // Last Modified: 4/29/2000 // // Author: William Martin mail@WilliamMartin.com // Original Author: http://www.cs.strath.ac.uk ////////////////////////////////////////////////////////////////// public class Vertex { private Graphics g; private Graph G; private int id; private int xCoord,yCoord;//coordinates of vertex private boolean mark;//true if vertex has been visited private Vector adjacentVertices; private Color color; ////////////////////////////////////////////////////////////////// // Vertex Method - This method uses the Applet class Graphics and // uses it on this packages graph class. Two coords (x,y) are // taken as paramenters containing the individual vertex // location. this method sets a new element (vertex) into the // graphs vector containing vertexes. // // Date Last Modified: 4/28/2000 ////////////////////////////////////////////////////////////////// public Vertex(Graphics g, Graph G, int id, int x, int y){ this.g = g; this.G = G; this.id = id; xCoord = x; yCoord = y; adjacentVertices = new Vector(); mark = false; color = G.defaultColor(); G.vectorOfVertices().addElement(this); paint(); } ////////////////////////////////////////////////////////////////// // addAdjacentVertex Method - This method adds a vertex element to // the Vector (container for our vertices). // // Date Last Modified: 4/28/2000 ////////////////////////////////////////////////////////////////// public void addAdjacentVertex(Vertex v){ adjacentVertices.addElement(v);} ////////////////////////////////////////////////////////////////// // adjacentVertices Method - This method returns the elements // the Verticies container (Vector). // // Date Last Modified: 4/28/2000 ////////////////////////////////////////////////////////////////// public Enumeration adjacentVertices(){ return adjacentVertices.elements(); } public int id(){ return id; } ////////////////////////////////////////////////////////////////// // info Method - this method returns a string that is used in the // text window for vertex position and id. // // Last Modified: 4/29/2000 ////////////////////////////////////////////////////////////////// public String info(){ String s = "V[" + id +"] -> "; Enumeration E = adjacentVertices(); while (E.hasMoreElements()){ Vertex v = (Vertex)E.nextElement(); s = s + "V[" + v.id() +"] "; } return s; } ////////////////////////////////////////////////////////////////// // isAdjacent Method - this method returns true if vertex v is // adjacent (in the container), and false if not. // // Last Modified: 4/29/2000 ////////////////////////////////////////////////////////////////// public boolean isAdjacent(Vertex v){ return adjacentVertices.contains(v); } ////////////////////////////////////////////////////////////////// // mark Method - this method returns true or false. // 1) true -if vertex has been visited // 2) false -if vertex has not been visited or has been reset // // Last Modified: 4/28/2000 ////////////////////////////////////////////////////////////////// public boolean mark(){return mark;} ////////////////////////////////////////////////////////////////// // paint Method- This method draws in the vertex as a oval or // circle of color that has been defined. // // Last Modified: 4/25/2000 ////////////////////////////////////////////////////////////////// public void paint(){ g.setColor(color); g.fillOval(xCoord-10,yCoord-10,20,20); } ////////////////////////////////////////////////////////////////// // reset Method - this method resets the vertices back to their // default color before beginning another bfs and sets the mark to // false (not visited). // // Last Modified: 4/29/2000 ////////////////////////////////////////////////////////////////// public void reset(){ mark = false; color = G.defaultColor(); } ////////////////////////////////////////////////////////////////// // setColor Method - this method is used to set the color of the // vertices. // // Last Modified: 4/19/2000 ////////////////////////////////////////////////////////////////// public void setColor(Color color){ this.color = color; } ////////////////////////////////////////////////////////////////// // setMark Method - this method is used to set the vertecies mark // value to that which is specified in the calling object. // 1) true = visited // 2) flase = not visited or is reset // // Last Modified: 4/19/2000 ////////////////////////////////////////////////////////////////// public void setMark(boolean value){ mark=value; } ////////////////////////////////////////////////////////////////// // toString - this method is the ususl toString included with // most classes. In this case the address of the vertex is // returned as well as its number. // // Last Modified: 4/29/2000 ////////////////////////////////////////////////////////////////// public String toString(){ String s = ""; s = s + "V[" + id +"] at "; s = s + "(" + xCoord +","+ yCoord +")"; return s; } ////////////////////////////////////////////////////////////////// // unPaint Method - this method unpaints the edge for the flash // method. // // Last Modified: 4\29\2000 ////////////////////////////////////////////////////////////////// public void unPaint(){ g.setColor(Color.white); g.fillOval(xCoord-5,yCoord-5,10,10); } ////////////////////////////////////////////////////////////////// // xCoord Method - this method specifies the x coordinate of (this) // vertex // // Last Modified: 4/29/2000 ////////////////////////////////////////////////////////////////// public int xCoord(){ return xCoord; } ////////////////////////////////////////////////////////////////// // yCoord Method - this method specifies the y coordinate of (this) // vertex // // Last Modified: 4/29/2000 ////////////////////////////////////////////////////////////////// public int yCoord(){ return yCoord; } } [ TOP ]

© Copyright 2001 William L. Martin All rights reserved.