[ b a c k t o i n d e x ]
package robot_graph;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
/**
* TESTER FOR GRAPH TAB: The real one dosent use
* string, thread, or any listeners.
*
* Graph Tab is a client side panel tab that displays
* a graph. Te graph is onstructed from landmark data
* from the server.
*
* Creation date: (11/30/2000 8:50:23 PM)
* @author: William L.Martin - bonewshakerbike@hotmail.com
*/
public class GraphTab extends JPanel implements
ActionListener,
Runnable {
RobotGraph robotgraph; //RobotGraph extends Graph.
private final int RES = 1; //pixels/this = cm
private final int OFFSET = 0; //to display into the window
JButton btn = new JButton("Make Graph");//Only temporary to
//simulate incoming
//data.
/**
* GraphTab constructor.
*/
public GraphTab() {
robotgraph = new RobotGraph();
//robotgraph.construct(13, OFFSET, OFFSET);
//temp stuff
btn.setHorizontalTextPosition(JButton.LEFT);
btn.addActionListener(this);
this.add(btn);
}
/**
* This method will be replaced by either a
* buffer listener or a thread that continously
* updates the graph.
* Creation date: (11/30/2000 10:06:45 PM)
* @param e java.awt.event.ActionEvent
*/
public void actionPerformed(ActionEvent e) {
Graphics graphics = getGraphics();
if ((JButton) e.getSource() == btn) {
Thread bill = new Thread(this, "bill"); //may put
bill.start(); //this in
//constructor
}
}
/**
* This method takes a integer array, breaks it up
* and inserts them into the graph. We could use a size
* rem 3 error checker here in the future.
* Creation date: (12/4/2000 1:02:52 PM)
*/
public void insert(int array[]) {
int arrayLength = array.length;
for (int i = 2; i < arrayLength; i++) { //start i at
//2 because the first
//2 ints is messages
//to be stripped off.
int scenero = array[i];
i++;
int x = (array[i]/RES)+OFFSET;
i++;
int y = (array[i]/RES)+OFFSET;
robotgraph.construct(scenero, x, y);
}
}
/**
* This method draws the graph .
*
* @param Graphics graphics
*/
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
for (int i = 0; i < robotgraph.vertices.size(); i++) {
Vertex v = (Vertex) robotgraph.vertices.elementAt(i);
int landmark = v.landmark();
int xi = v.xCoord;
int yi = v.yCoord;
DrawLandmark d = new DrawLandmark(graphics, landmark, xi, yi);
}
for (int i = 0; i < robotgraph.edges.size(); i++) {
Edge edge = (Edge) robotgraph.edges.elementAt(i);
Vertex v1 = edge.v1;
Vertex v2 = edge.v2;
int v1x = v1.xCoord;
int v1y = v1.yCoord;
int v2x = v2.xCoord;
int v2y = v2.yCoord;
graphics.drawLine(v1x, v1y, v2x, v2y);
graphics.drawString (((edge.getLength(v1, v2)*RES) + "cm"),
((v1.xCoord() + v2.xCoord()) / 2),
((v1.yCoord() + v2.yCoord()) / 2));
}
}
/**
* This is the threads run method in which inserts
* the data into the graph and redraws. Next we may
* make this continously read from a buffer, insert,
* then re-pant.
* Creation date: (11/30/2000 10:34:51 PM)
*/
public void run() {
updateData (new int[] {1, 345, 5, 380, 190, 5, 190, 190, 5, 0, 190});
this.updateUI();
}
/**
* Other code requires the call to insert with
* updateData.
* Creation date: (12/10/2000 4:41:44 PM)
* @param message int[]
*/
public void updateData(int[] message) {
insert(message);
}
}