[ b a c k t o i n d e x ]
package robot_graph;
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. This Class idea was
// obtained from the website at
// http://www.cs.strath.ac.uk/teaching
//
// x, y coord is the location of vertex in the applet pane.
//
// Date created: 4/29/2000
// Modified: 8:10 PM 11/11/2000
// Last Modified: 10:58 AM 11/18/2000
//
// Modifications: a) paint was changed to draw a box instead of
// a circle to represent vertex.
// b) a landmark field added to draw certain
// scenerios. See draw() method for
// definitions.
//
// 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, landmark;
public 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. Added to this class is
// "landmark" which gives this node a scenero. Scenero types are
// defined in class DrawLandmark and in this class under the
// draw() method.
//
// Date Last Modified: 11/18/2000
//////////////////////////////////////////////////////////////////
public Vertex(Graphics g, Graph G, int id, int landmark,
int x, int y){
this.g = g;
this.G = G;
this.id = id;
this.landmark = landmark;
xCoord = x;
yCoord = y;
adjacentVertices = new Vector();
mark = false;
color = G.defaultColor();
G.vectorOfVertices().addElement(this);
paint();
}
//////////////////////////////////////////////////////////////////
// NON_GRAPHICS VERSION
// 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. Added to this class is
// "landmark" which gives this node a scenero. Scenero types are
// defined in class DrawLandmark and in this class under the
// draw() method.
//
// Date Last Modified: 11/18/2000
//////////////////////////////////////////////////////////////////
public Vertex(Graph G, int id, int landmark,
int x, int y){
this.G = G;
this.id = id;
this.landmark = landmark;
xCoord = x;
yCoord = y;
adjacentVertices = new Vector();
mark = false;
G.vectorOfVertices().addElement(this);
}
//////////////////////////////////////////////////////////////////
// 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);
}
public int landmark(){
return landmark;
}
//////////////////////////////////////////////////////////////////
// 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;}
//////////////////////////////////////////////////////////////////
// [VERSION 1 - depreciated]paint Method- This method draws in
// the vertex as a oval or circle of color that has been defined.
//
// Creation Date: 8:10 PM 11/11/2000
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Latest Revision: This method now calls DrawLandmark to draw the
// vertex as well as its scenero:
// private final static int CROSSROAD = 0;
// private final static int EAST_PATH = 1;
// private final static int SOUTH_PATH = 2;
// private final static int WEST_PATH = 3;
// private final static int NORTH_PATH = 4;
// private final static int SOUTH_EAST = 5;
// private final static int SOUTH_WEST = 6;
// private final static int NORTH_WEST = 7;
// private final static int NORTH_EAST = 8;
// private final static int EAST_WEST_SOUTH = 9;
// private final static int NORTH_SOUTH_WEST = 10;
// private final static int EAST_WEST_NORTH = 11;
// private final static int NORTH_SOUTH_EAST = 12;
// private final static int VIRT_NORTH = 13;
// private final static int VIRT_EAST = 14;
// private final static int VIRT_SOUTH = 15;
// private final static int VIRT_WEST = 16;
//
// Last Modified: 10:44 AM 11/18/2000
// by: William Martin mail@WilliamMartin.com
//////////////////////////////////////////////////////////////////
public void paint(){
DrawLandmark paint = new
DrawLandmark(g, landmark, xCoord, yCoord);
}
//////////////////////////////////////////////////////////////////
// [VERSION 1 - depreciated]paint Method- This method draws in
// the vertex as a oval or circle of color that has been defined.
//
// Creation Date: 8:10 PM 11/11/2000
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Latest Revision: This method now calls DrawLandmark to draw the
// vertex as well as its scenero:
// private final static int CROSSROAD = 0;
// private final static int EAST_PATH = 1;
// private final static int SOUTH_PATH = 2;
// private final static int WEST_PATH = 3;
// private final static int NORTH_PATH = 4;
// private final static int SOUTH_EAST = 5;
// private final static int SOUTH_WEST = 6;
// private final static int NORTH_WEST = 7;
// private final static int NORTH_EAST = 8;
// private final static int EAST_WEST_SOUTH = 9;
// private final static int NORTH_SOUTH_WEST = 10;
// private final static int EAST_WEST_NORTH = 11;
// private final static int NORTH_SOUTH_EAST = 12;
// private final static int VIRT_NORTH = 13;
// private final static int VIRT_EAST = 14;
// private final static int VIRT_SOUTH = 15;
// private final static int VIRT_WEST = 16;
//
// Last Modified: 10:44 AM 11/18/2000
// by: William Martin mail@WilliamMartin.com
//////////////////////////////////////////////////////////////////
public void paint(Graphics graphics){
DrawLandmark paint = new
DrawLandmark(graphics, landmark, xCoord, yCoord);
}
/**
* This method will return a string that
* includes the paths from this node.
* Creation date: (12/4/2000 7:55:41 PM)
* @return java.lang.String
*/
public String reportAdjacentPaths() {
String s = "Vertex #" + id + " | Visited Neighbors are: ";
Enumeration E = adjacentVertices();
while (E.hasMoreElements()) {
Vertex v = (Vertex) E.nextElement();
s = s + "Vertex #" + v.id() + ", ";
}
return s;
}
//////////////////////////////////////////////////////////////////
// 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);
DrawLandmark paint = new
DrawLandmark(g, landmark, xCoord, yCoord);
}
//////////////////////////////////////////////////////////////////
// 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;
}
}