Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
690 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package Lab3Help; | ||
|
||
/** A BLineStop represents a bus stop visited by a line. Each line has | ||
* an associated BLineTable, containing an array of BLineStops. Each | ||
* BLineStop contains the name of the stop and the amount of time | ||
* needed for the transition from the stop before to the stop after. | ||
* | ||
*/ | ||
|
||
public class BLineStop { | ||
|
||
protected String stop; | ||
protected int time; | ||
|
||
/** Creates a new BLineStop with the given name and the given | ||
* transition time. | ||
* | ||
* @param stop the name of the stop | ||
* @param time the transition time from the stop before to this | ||
* stop. | ||
*/ | ||
|
||
public BLineStop(String stop, int time) { | ||
this.stop = stop; | ||
this.time = time; | ||
} | ||
|
||
/** Returns the time needed from the stop before to this stop. | ||
* | ||
* @return the time needed from the stop before to this stop. | ||
* | ||
*/ | ||
|
||
public int getTime() { | ||
return time; | ||
} | ||
|
||
/** Returns the name of this stop. | ||
* | ||
* @return the name of this stop. | ||
* | ||
*/ | ||
|
||
public String getName() { | ||
return stop; | ||
} | ||
} | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package Lab3Help; | ||
|
||
/** The BLineTable class represents line information for the line with the | ||
* given line id. Each table contains an array of BLineStop. | ||
* | ||
* @see BLineStop | ||
* | ||
*/ | ||
|
||
public class BLineTable { | ||
|
||
protected int id; | ||
protected BLineStop[] stops; | ||
|
||
/** Creates a new BLineTable with the given id and the given stop | ||
* information. | ||
* | ||
* @param stops the stops of this line | ||
* @param id the id of this line | ||
* | ||
*/ | ||
|
||
public BLineTable(BLineStop[] stops, int id) { | ||
this.stops = stops; | ||
this.id = id; | ||
} | ||
|
||
/** Returns the line id of this line table | ||
* | ||
* @return the line id of this line table | ||
*/ | ||
|
||
public int getLineNo() { | ||
return id; | ||
} | ||
|
||
/** Returns the stops information this line table | ||
* | ||
* @return the stop information of this line table | ||
*/ | ||
|
||
public BLineStop[] getStops() { | ||
return stops; | ||
} | ||
} | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package Lab3Help; | ||
|
||
/** The BStop class represents a bus stop. It contains the name of the | ||
* bus stop and the x and y coordinates of it. | ||
* | ||
*/ | ||
|
||
public class BStop { | ||
|
||
protected String name; | ||
protected int x,y; | ||
|
||
/** Creates a new bus stop with the given name at the given coordinates. | ||
* | ||
* @param name the name of the bus stop | ||
* @param x the x coordinate of the bus stop | ||
* @param y the y coordinate of the bus stop | ||
* | ||
*/ | ||
|
||
public BStop(String name, int x, int y) { | ||
this.name = name; | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
/** Returns the x coordinate of the bus stop | ||
* | ||
* @return the x coordinate of the bus stop | ||
*/ | ||
|
||
public int getX() { return x; } | ||
|
||
/** Returns the y coordinate of the bus stop | ||
* | ||
* @return the y coordinate of the bus stop | ||
*/ | ||
|
||
public int getY() { return y; } | ||
|
||
/** Returns the name of the bus stop | ||
* | ||
* @return the name of the bus stop | ||
*/ | ||
|
||
public String getName() { return name; } | ||
|
||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
package Lab3Help; | ||
|
||
// 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.Vector; | ||
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 <a href="mailto:kentp@cs.chalmers.se">Kent Petersson</a> | ||
* @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 <a href="mailto:kentp@cs.chalmers.se">Kent Petersson</a> | ||
* @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 <a href="mailto:kentp@cs.chalmers.se">Kent Petersson</a> | ||
* @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; | ||
} | ||
|
||
Vector lines = new Vector(); | ||
Vector stops = new Vector(); | ||
Vector shortest = new Vector(); | ||
|
||
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(); | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.