// Gary Yngve // 11/20/2005 import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import javax.swing.*; import java.util.*; public class GraphUI { private JTextArea textBox; private JScrollPane scrollPane; private DrawingPanel dp; private Graphics2D gr; private Image img; private static final int TEXTHEIGHT = 100; private int imgWidth; private int imgHeight; // loads the campus map and creates the window public GraphUI() { Toolkit tk = Toolkit.getDefaultToolkit(); dp = new DrawingPanel(); img = tk.getImage("smallmap.jpg"); MediaTracker mt = new MediaTracker(dp.getFrame()); mt.addImage(img,0); try { mt.waitForID(0); } catch (InterruptedException e) { throw new RuntimeException(e.toString()); } imgWidth = img.getWidth(null); imgHeight = img.getHeight(null); dp.fixSize(imgWidth,imgHeight+TEXTHEIGHT); gr = (Graphics2D) dp.getGraphics(); drawBackground(); // Added by Kate Deibel to make the text area more useful this.textBox = new JTextArea(); this.scrollPane = new JScrollPane(this.textBox, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); this.scrollPane.setBounds(0,imgHeight,imgWidth,TEXTHEIGHT); this.dp.getPanel().add(scrollPane); this.textBox.setLineWrap(false); this.textBox.setEditable(true); this.textBox.setMargin(new Insets(1,5,1,5)); this.textBox.setFont(new Font(null,Font.BOLD,15)); } // sleeps for the number of milliseconds requested -- // accuracy is low for below 100 ms public void sleep(int milliseconds) { try { // nappy time Thread.sleep(milliseconds); } catch (Exception e) { // some silly checked exception System.out.println("Dunno what this is for."); } } // draws the background image (erasing whatever was on top) public void drawBackground() { gr.drawImage(img,0,0,null); } // draws a vertex given a Color and a radius (min is 1) public void drawVertex(Vertex vertex, Color color, int radius) { int x = vertex.xcoord; int y = vertex.ycoord; gr.setColor(color); gr.fillOval(x-radius,y-radius,2*radius,2*radius); } // draws an edge given a Color and thickness (min is 0) public void drawEdge(Edge edge, Color color, int thickness) { int x1,y1,x2,y2; x1 = edge.vertex1.xcoord; y1 = edge.vertex1.ycoord; x2 = edge.vertex2.xcoord; y2 = edge.vertex2.ycoord; gr.setColor(color); for(int i = -thickness/2; i <= (thickness+1)/2; i++) for(int j = -thickness/2; j <= (thickness+1)/2; j++) gr.drawLine(x1+i,y1+j,x2+i,y2+j); } public void drawVertexString(Vertex vertex) { gr.setFont(new Font(null,Font.BOLD,10)); gr.setColor(Color.BLACK); gr.drawString(vertex.label,vertex.xcoord+5,vertex.ycoord-10); } public void drawEdgeString(Edge edge) { gr.setFont(new Font(null,Font.BOLD,10)); gr.setColor(Color.BLACK); int x = (edge.vertex1.xcoord+edge.vertex2.xcoord)/2; int y = (edge.vertex1.ycoord+edge.vertex2.ycoord)/2; gr.drawString(edge.label,x+5,y-10); } // sets the textbox to empty public void blankTextBox() { this.textBox.setText(""); } // writes a message on a new line of the textbox public void writeMessage(String s) { if(this.textBox.getText().length() > 0) this.textBox.append("\n"); this.textBox.append(s); this.textBox.setCaretPosition(this.textBox.getDocument().getLength()); } // a sample main to show how the drawing works public static void main(String[] args) { BuildGraph.buildGraph(); GraphUI gui = new GraphUI(); gui.drawBackground(); for(Iterator i = BuildGraph.vertices.iterator(); i.hasNext(); ) { Vertex v = (Vertex)i.next(); gui.drawVertex(v,Color.RED,4); } for(Iterator i = BuildGraph.edges.iterator(); i.hasNext(); ) { Edge e = (Edge)i.next(); gui.drawEdge(e,Color.YELLOW,1); } Edge edge = (Edge)BuildGraph.edges.get(7); gui.drawEdge(edge,Color.BLUE,3); gui.drawEdge(edge,Color.WHITE,1); gui.blankTextBox(); gui.writeMessage("Yo, yo, wazzuuuup?"); gui.writeMessage(edge.toString() + ", weight = " + (int)edge.weight + " : " + edge.vertex1.toString() + " <--> " + edge.vertex2.toString()); } }