[Removed old UI classes.
bjorn@bringert.net**20040907121815] < > {
hunk ./lab4/BusMapFrame.java 1
-package lab4;
-
-// implementation of BusMapInf interface
-
-import java.awt.Color;
-import java.awt.Graphics;
-import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ArrayList;
-import javax.swing.JFrame;
-
-/**
- * An implementation of the interface BusMapInf. This implementation
- * puts up a window where the buslines and busstops can be displayed.
- * The window has coordinates 0-1000 with (0,0) in the lower left
- * corner.
- *
- * @author Kent Petersson
- * @version 1.0
- * @since 1.0
- * @see JFrame
- * @see BusMapInf
- */
-public class BusMapFrame extends JFrame implements BusMapInf {
-
- static int frameNr = 0;
-
- /**
- * Class to represent line segments
- * @author Kent Petersson
- * @version 1.0
- * @since 1.0
- */
- class Line {
- public Line(int fx, int fy, int tx, int ty, Color c) {
- fromX = fx; fromY = fy;
- toX = tx; toY = ty;
- lineColor = c;
- }
- int fromX, fromY, toX, toY;
- Color lineColor;
- }
-
-
- /**
- * Class to represent stops
- *
- * @author Kent Petersson
- * @version 1.0
- * @since 1.0
- */
- class Stop {
- public Stop(int px, int py, String name) {
- posX = px; posY = py;
- this.name = name;
- }
- int posX, posY;
- String name;
- }
-
- List lines = new ArrayList();
- List stops = new ArrayList();
- List shortest = new ArrayList();
-
- boolean isShortest = false;
-
- private int currColIx = 0;
- private Color[] colors
- = {Color.black, Color.blue, Color.green, Color.white,
- Color.yellow, Color.magenta, Color.orange};
- private Color currColor() {
- return colors[currColIx];
- }
-
- public BusMapFrame() {
- frameNr++;
- setTitle("BusMap Frame " + frameNr);
- setSize(600,625);
- getContentPane().setBackground(Color.white);
-
-
- addWindowListener(new WindowAdapter() {
-
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
-
- });
- }
-
- /**
- * Initiates the object
- */
- public void initMap() {
- setVisible(false);
- lines.clear(); stops.clear(); shortest.clear();
- isShortest = false; currColIx = 0;
- }
-
- /**
- * Makes the following edges be drawn thick and in red. Could be
- * used to distinguish the shortest path.
- */
- public void initShortestPath() {
- isShortest = true;
- }
-
- /**
- * Makes the following edges in a "new" color.
- */
- public void nextColor() {
- currColIx++;
- if (currColIx >= colors.length) currColIx = 1;
- }
-
- /**
- * Draw a line between the points (fromX,fromY) and (toX,toY).
- */
- public void drawEdge(int fromX, int fromY, int toX, int toY) {
- (isShortest?shortest:lines)
- .add(new Line(fromX,fromY,toX,toY,currColor()));
- }
-
- /**
- * Draw a stop (small bullet) at position (x,y) and print the
- * name of the stop immediately to the right of the stop.
- */
- public void drawStop(int x, int y, String name) {
- stops.add(new Stop(x,y,name));
- }
-
- /**
- * Shows the map.
- */
- public void finalMap() {
- this.setVisible(true);
- }
-
- /**
- * Translates between the external coordinates and the internal
- * coordintes in the X-direction (width).
- */
- private int transformX (int pos) {
- double width = this.getSize().getWidth()-40;
- return (int)((pos*width)/1000)+20;
- }
-
- /**
- * Translates between the external coordinates and the internal
- * coordintes in the Y-direction (height).
- */
- private int transformY (int pos) {
- double height = this.getSize().getHeight()-60;
- return (int)(height-(pos*height)/1000)+40;
- }
-
- private static final int bullSize = 2;
- public void paint(Graphics g) {
- super.paint(g);
- g.setColor(Color.lightGray);
- g.fillRect(20,40,
- (int)(this.getSize().getWidth()-40),
- (int)(this.getSize().getHeight()-60));
- Stop currStop;
- g.setColor(Color.black);
- for(Iterator i = stops.iterator(); i.hasNext();) {
- currStop = (Stop) i.next();
- g.fillOval(transformX(currStop.posX) - bullSize,
- transformY(currStop.posY) - bullSize,
- bullSize*2, bullSize*2);
- g.drawString(currStop.name,
- transformX(currStop.posX) + (int)(1.5*bullSize),
- transformY(currStop.posY) + (int)(1.9*bullSize));
- }
-
- Line currLine;
- for(Iterator i = lines.iterator(); i.hasNext();) {
- currLine = (Line) i.next();
- g.setColor(currLine.lineColor);
- g.drawLine(transformX(currLine.fromX), transformY(currLine.fromY),
- transformX(currLine.toX), transformY(currLine.toY));
- }
- g.setColor(Color.red);
- for(Iterator i = shortest.iterator(); i.hasNext();) {
- currLine = (Line) i.next();
- g.drawLine(transformX(currLine.fromX)+1, transformY(currLine.fromY),
- transformX(currLine.toX)+1, transformY(currLine.toY));
- g.drawLine(transformX(currLine.fromX), transformY(currLine.fromY)+1,
- transformX(currLine.toX), transformY(currLine.toY)+1);
- g.drawLine(transformX(currLine.fromX)-1, transformY(currLine.fromY),
- transformX(currLine.toX)-1, transformY(currLine.toY));
- g.drawLine(transformX(currLine.fromX), transformY(currLine.fromY)-1,
- transformX(currLine.toX), transformY(currLine.toY)-1);
- g.drawLine(transformX(currLine.fromX), transformY(currLine.fromY),
- transformX(currLine.toX), transformY(currLine.toY));
- }
- }
-
- /**
- * For testing purposes
- */
- public static void main(String[] args) {
- BusMapFrame bmf = new BusMapFrame();
- BusMapFrame x = new BusMapFrame();
- x.initMap();
- int x1, x2, y1, y2;
- for(int i=0; i<53; i++) {
- x1 = (int)(Math.random()*1000);
- y1 = (int)(Math.random()*1000);
- x2 = (int)(Math.random()*1000);
- y2 = (int)(Math.random()*1000);
- x.nextColor(); x.drawEdge(x1,y1,x2,y2);
- }
- x.finalMap();
- bmf.initMap();
- bmf.drawStop(0,0,"pos0");
- bmf.drawStop(100,100,"pos10");
- bmf.drawStop(200,200,"pos20");
- bmf.drawStop(300,300,"pos30");
- bmf.drawStop(400,400,"pos40");
- bmf.drawStop(500,500,"pos50");
- bmf.drawStop(600,600,"pos60");
- bmf.drawStop(700,700,"pos70");
- bmf.drawStop(800,800,"pos80");
- bmf.drawStop(900,900,"pos90");
- bmf.drawStop(1000,1000,"pos100");
- bmf.drawEdge(100,10,200,20);
- bmf.nextColor();
- bmf.drawEdge(200,200,400,400);
- bmf.nextColor();
- bmf.drawEdge(120, 190, 609,808);
- for(int i=0; i<333; i++) {
- x1 = (int)(Math.random()*1000);
- y1 = (int)(Math.random()*1000);
- x2 = (int)(Math.random()*1000);
- y2 = (int)(Math.random()*1000);
- bmf.nextColor(); bmf.drawEdge(x1,y1,x2,y2);
- }
- bmf.initShortestPath();
- bmf.drawEdge(100,200,800,400);
- bmf.finalMap();
- }
-}
rmfile ./lab4/BusMapFrame.java
hunk ./lab4/Lab4Frame.java 1
-package lab4;
-
-import java.awt.*;
-import java.awt.event.*;
-import javax.swing.*;
-
-/** The Lab3Frame class is intended to give an simple graphical
- * interface to the third laboration. The interface consists of two
- * comboboxes (the source and destionation), a button that indicates
- * when to calculate the shortest path and a text area, in which
- * information from the lab implemetation can be written.
- *
- * A typical laboration adds all nodes to the frame using the addStop
- * method and registers itself as an ActionListener using the
- * addActionListener method. When the button is pressed the action
- * listener is invoked, which should get the source and destination
- * using the getFrom and getTo methods. After having calculated the
- * shortest path, a textual representation of the path can be written
- * to the text area using the write or writeln methods and draw the
- * graph using the given BusMapFrame.
- *
- */
-
-public class Lab4Frame extends JFrame {
-
-
- protected JButton spath = new JButton("SPath");
-
- protected JComboBox from;
- protected JComboBox to;
-
- protected JTextArea tArea = new JTextArea("", 12, 30);
-
- protected int textPos = 0;
-
- /** Creates a new Lab3Fram
- *
- */
-
- public Lab4Frame() {
-
- String[] initialCBox = { };
-
- GridBagConstraints gbc = new GridBagConstraints();
-
- JPanel p2 = new JPanel(new GridBagLayout());
-
- gbc.insets = new Insets(2,2,2,2);
-
- gbc.gridx = 0; gbc.gridy = 0;
- JLabel fromLabel = new JLabel("from:");
- from = new JComboBox(initialCBox);
- p2.add(fromLabel,gbc); gbc.gridx = 1;
- p2.add(from, gbc);
-
- gbc.gridx = 0; gbc.gridy = 1;
- JLabel toLabel = new JLabel("to:");
- to = new JComboBox(initialCBox);
- p2.add(toLabel, gbc); gbc.gridx = 1;
- p2.add(to, gbc);
-
- Container c = getContentPane();
-
- JPanel p1 = new JPanel(new GridBagLayout());
-
- gbc.gridx = 0; gbc.gridy = 0;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- p1.add(p2, gbc);
-
- gbc.fill = GridBagConstraints.NONE;
- gbc.gridx = 0; gbc.gridy = 1;
- p1.add(spath, gbc);
-
- gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 0;
- gbc.gridheight = 2;
-
- tArea.setEditable(false);
-
- JScrollPane sp = new JScrollPane(tArea);
- p1.add(sp, gbc);
-
- c.add(p1);
- }
-
-
- /** Returns the object selected by the from combobox.
- *
- * @return the object selected by the from combobox
- */
-
- public Object getFrom() {
- return from.getSelectedItem();
- }
-
- /** Returns the object selected by the to combobox.
- *
- * @return the object selected by the to combobox
- */
-
-
- public Object getTo() {
- return to.getSelectedItem();
- }
-
- /** Writes the given string to the text area
- *
- * @param s the string to write in the text area
- */
-
-
- public void write(String s) {
- tArea.insert(s, textPos);
- textPos += s.length();
- }
-
- /** Writes the given string to the text area appending a newline
- *
- * @param s the string to write in the text area
- */
-
-
- public void writeln(String s) {
- write(s);
- write("\n");
- }
-
- /** Adds a stop to both comboboxes.
- *
- * @param o the object to add to both comboboxes.
- */
-
-
- public void addStop(Object o) {
- from.addItem(o);
- to.addItem(o);
- }
-
- /** Adds the given ActionListener to the button of the frame.
- *
- * @param l the listener to be added to the button.
- */
-
-
- public void addActionListener(ActionListener l) {
- spath.addActionListener(l);
- }
-
-}
rmfile ./lab4/Lab4Frame.java
}