📄 mapcanvas.java
字号:
/* Written by : Lee Jiayi
* Assisted by : Zhang Haoze and Koh Ling Xin
* Description of class: Class is used to contains mapCanvas as a means of
* being able to scroll mapCanvas when the physical size
* becomes bigger than the scrollPane
* Description of methods: Methods of this class are purely delegation method, ie
* it only forward the method to mapCanvas
*/
package GUI;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.event.MouseInputListener;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;
import tools.*;
import java.lang.Integer;
public class MapCanvas extends JPanel implements MouseInputListener, Observer {
//for use with setMode and stuff relating to mode
private static final int SELECT = 1;
private static final int DELETE = 2;
private static final int MOVE = 3;
private static final int CUT = 4;
private static final int COPY = 5;
private static final int PASTE = 6;
private static final int BUSSTOP1 = 7;
private static final int BUSSTOP2 = 8;
private static final int ROAD1 = 9;
private static final int ROAD2 = 10;
private static final int ROAD3 = 11;
private static final int ROAD4 = 12;
private static final int ROAD5 = 13;
private static final int ROAD6 = 14;
private static final int ROADJUNCTION = 15;
private static final int Depot = 16;
//private static final int TRAFFICLIGHT = 17;
private static final int BUSROUTE = 18;
//for use with convertGridToScreen
private final int LOC_TOPLEFT=0;
private final int LOC_TOPRIGHT=1;
private final int LOC_BOTTOMLEFT=2;
private final int LOC_BOTTOMRIGHT=3;
private final int LOC_CENTER=4;
//defines colors used in drawing some of the objects
private static final Color C_BACKGROUD = Color.WHITE;
private static final Color C_BUSSTOP = Color.RED;
private static final Color C_ROAD1 = Color.GRAY;
private static final Color C_ROAD2 = Color.DARK_GRAY;
private static final Color C_ROAD3 = Color.BLUE;
private static final Color C_ROAD4 = Color.GREEN;
private static final Color C_ROAD5 = Color.ORANGE;
private static final Color C_ROAD6 = Color.YELLOW;
private static final Color C_ROADJUNCTION = Color.BLACK;
private static final Color C_Depot = Color.GREEN;
private static final Color C_BUSROUTE = Color.CYAN;
private static final Color C_TEMP_BUSROUTE = Color.WHITE;
private static final Color C_GRIDLINE = Color.BLACK;
private static final Color C_SELECTED = Color.MAGENTA;
//IDs for tools
private final int ID_BUSSTOP1 = 1;
private final int ID_BUSSTOP2 = 2;
private final int ID_ROAD1 = 3;
private final int ID_ROAD2 = 4;
private final int ID_ROAD3 = 5;
private final int ID_ROAD4 = 6;
private final int ID_ROAD5 = 7;
private final int ID_ROAD6 = 8;
private final int ID_ROADJUNCTION= 9;
private final int ID_Depot = 10;
//private final int ID_TRAFFICLIGHT = 11;
private final int ID_BUSROUTE= 12;
private int drawingHeight; //height of canvas in pixel
private int gridSize; //size of 1 grid in pixels
private int drawingWidth; //width of canvas in pixel
//for use with mouse events
//there are 2 types of coordinates, screen and grid
//screen corresponding to the actual screen coordinates
//grid corresponding to the coordinates of the map grid, ie 0,0 to 29,29
private Point mouseOverPoint=new Point(); //point where the mouse is over(grid coordinates)
private Point pointClicked; //last point clicked(grid coordinates)
private Point storePoint; //first point that is clicked after a mode change(grid coordinates)
private int startIndex; //index to use to refer to grid(0 or 1)
private MapData mapData; //class where infomation of the map is stored
private int mapSizeY; //height of canvas in grid
private int mapSizeX; //width of canvas in grid
private int mode=1; //mode which the canvas is in
//below is for zooming purpose
private int offsetX; //X coordinates where the actual canvas is on the JPanel
private int offsetY; //Y coordinates where the actual canvas is on the JPanel
//for naming newly created roads and busRoute
private int busRouteNo=1;
private Point selectedPoint;
private BusRoute tempBusRoute;
private BusRoute displayedBusRoute;
private CmeGUI frame;
MapCanvas() {
super();
initialize(0,0,20);
}
MapCanvas(int width, int height) {
super();
initialize(width,height,20);
}
MapCanvas(int width,int height,int gridSize) {
super();
initialize(width,height,gridSize);
}
/* return screen coordinates corresponding to grid coordinates and location
* location is - LOC_TOPLEFT,LOC_TOPRIGHT,LOC_CENTER,LOC_BOTTOMLEFT,LOC_BOTTOMRIGHT
*/
private Point convertGridToScreen(Point k,int location) {
Point p1 = new Point();
//p1 holds center of grid
p1.setLocation(getOrigin().getX()+k.getX()*gridSize+0.5*gridSize,
getOrigin().getY()-k.getY()*gridSize-0.5*gridSize);
//offset p1 according to location
if (location==LOC_TOPLEFT) {
p1.setLocation(p1.getX()-gridSize/2,p1.getY()-gridSize/2);
//System.out.println(p1.getX() + " " +p1.getY());
} else if (location ==LOC_TOPRIGHT) {
p1.setLocation(p1.getX()+gridSize/2,p1.getY()-gridSize/2);
}
else if (location ==LOC_BOTTOMLEFT) {
p1.setLocation(p1.getX()-gridSize/2,p1.getY()+gridSize/2);
}
else if (location ==LOC_BOTTOMRIGHT) {
p1.setLocation(p1.getX()+gridSize/2,p1.getY()+gridSize/2);
}
//return p1
return p1;
}
/*
* return grid coordinates correspoinding to screen coordinates
*/private Point convertScreenToGrid(Point point) {
int x,y;
x=(int)point.getX()-offsetX;
y=(int)point.getY()-offsetY;
x=x/gridSize + startIndex;
y=mapSizeY - 1 - (y/gridSize - startIndex);
Point pt = new Point(x,y);
return pt;
}
/*
* function to add Depot to mapData
* error checking is done by MapData class
*/
private void addDepot() {
Depot b = new Depot(pointClicked);
if (!mapData.add(pointClicked,b)) {
//Depot not added
frame.setStatus("Depot not added");
} else {
//Depot added
frame.setStatus("Depot added");
//select Depot added
mapData.setSelected(pointClicked,ID_Depot);
}
}
/*
* function to add bus route to mapData
* bus routed added is already created properly via
* createBusRoute() function (ADDED BY JEFFREY ON 18/10)
*/
/* private void addBusRoute() {
//add tempBusRoute to mapData
mapData.addBusRoute(tempBusRoute);
//bus route added
frame.setStatus("Bus route No " + busRouteNo + " added");
//update bus route dialog
frame.updateBusRouteDialog();
//incremental bus route no
busRouteNo++;
tempBusRoute=null; //prepare for next bus route;
}*/
/*
* function to add bus stop 1 at pointClicked
* error checking is done in MapData
*/
private void addBusStop1() {
BusStop1 bs = new BusStop1(pointClicked);
if (!mapData.add(pointClicked,bs)) {
//bus stop not added
frame.setStatus("Busstop1 not added");
} else {
//bus stop added
frame.setStatus("Busstop1 added");
//select bus stop added
mapData.setSelected(pointClicked,ID_BUSSTOP1);
}
}
/*
* function to add bus stop 2 at pointClicked
* error checking is done in MapData
*/
private void addBusStop2() {
BusStop2 bs = new BusStop2(pointClicked);
if (!mapData.add(pointClicked,bs)) {
//bus stop not added
frame.setStatus("Busstop2 not added");
} else {
//bus stop added
frame.setStatus("Busstop2 added");
//select bus stop added
mapData.setSelected(pointClicked,ID_BUSSTOP2);
}
}
/*
* function to add Road 1 to mapData
* error checking is done in MapData
*/
private void addRoad1() {
Road1 r = new Road1(pointClicked);
if (!mapData.add(pointClicked,r)) {
//traffic light not added
frame.setStatus("Road 1 not added");
} else {
//traffic light added
frame.setStatus("Road 1 created");
//select traffic light added
mapData.setSelected(pointClicked,ID_ROAD1);
}
}
/*
* function to add Road 1 to mapData
* error checking is done in MapData
*/
private void addRoad2() {
Road2 r = new Road2(pointClicked);
if (!mapData.add(pointClicked,r)) {
//traffic light not added
frame.setStatus("Road 2 not added");
} else {
//traffic light added
frame.setStatus("Road 2 created");
//select traffic light added
mapData.setSelected(pointClicked,ID_ROAD2);
}
}
/*
* function to add Road 3 to mapData
* error checking is done in MapData
*/
private void addRoad3() {
Road3 r = new Road3(pointClicked);
if (!mapData.add(pointClicked,r)) {
//traffic light not added
frame.setStatus("Road 3 not added");
} else {
//traffic light added
frame.setStatus("Road 3 created");
//select traffic light added
mapData.setSelected(pointClicked,ID_ROAD3);
}
}
/*
* function to add Road 4 to mapData
* error checking is done in MapData
*/
private void addRoad4() {
Road4 r = new Road4(pointClicked);
if (!mapData.add(pointClicked,r)) {
//traffic light not added
frame.setStatus("Road 4 not added");
} else {
//traffic light added
frame.setStatus("Road 4 created");
//select traffic light added
mapData.setSelected(pointClicked,ID_ROAD4);
}
}
/*
* function to add Road 5 to mapData
* error checking is done in MapData
*/
private void addRoad5() {
Road5 r = new Road5(pointClicked);
if (!mapData.add(pointClicked,r)) {
//traffic light not added
frame.setStatus("Road 5 not added");
} else {
//traffic light added
frame.setStatus("Road 5 created");
//select traffic light added
mapData.setSelected(pointClicked,ID_ROAD5);
}
}
/*
* function to add Road 6 to mapData
* error checking is done in MapData
*/
private void addRoad6() {
Road6 r = new Road6(pointClicked);
if (!mapData.add(pointClicked,r)) {
//traffic light not added
frame.setStatus("Road 6 not added");
} else {
//traffic light added
frame.setStatus("Road 6 created");
//select traffic light added
mapData.setSelected(pointClicked,ID_ROAD6);
}
}
/*
* function to add Road Juntion to mapData
* error checking is done in MapData
*/
private void addRoadJunction() {
RoadJunction r = new RoadJunction(pointClicked);
if (!mapData.add(pointClicked,r)) {
//traffic light not added
frame.setStatus("Road Junction not added");
} else {
//traffic light added
frame.setStatus("Road Junction created");
//select traffic light added
mapData.setSelected(pointClicked,ID_ROADJUNCTION);
}
}
/*
* function to create bus route (ADDED BY JEFFREY ON 18/10)
*/
private void createBusRoute() {
//create tempBusRoute if it is null
if (tempBusRoute==null) {
tempBusRoute= new BusRoute();
tempBusRoute.setBusNum(busRouteNo);
}
//inform BusRoute whether pointClicked is on road
if (mapData.isOnRoad(pointClicked)) {
tempBusRoute.setisOnRoad();
}
//inform BusRoute whether pointClicked is a interchange
if (mapData.isDepot(pointClicked)) {
tempBusRoute.setIsDepot();
}
//add pointClicked to busRoute
if (tempBusRoute.addPoint(pointClicked)) {
//point is accepted
frame.setStatus("Point ("+(int)pointClicked.getX()+","+
(int)pointClicked.getY()+") added to bus route");
//repaint mapCanvas to reflect the added point
repaint();
} else {
//point is not accepted
frame.setStatus("Point not added to bus route");
}
//check if busRoute is complete
if (tempBusRoute.isComplete()) {
frame.setStatus("Bus route completed");
//busRoute is complete,add to mapData
mapData.addBusRoute(tempBusRoute);
frame.setStatus("Bus route No " + busRouteNo + " added");
//update bus route dialog
frame.updateBusRouteDialog();
//incremental bus route no
busRouteNo++;
tempBusRoute=null; //prepare for next bus route;
}
}
/*
* function to drawDepot at point pt
* a square of color C_Depot is drawn in the grid corresponding
*
*/
private void drawDepot(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_Depot);
int x1,y1,width,height;
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX()+1;
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY()+1;
width = gridSize-1;
height = gridSize-1;
g2.fillRect(x1,y1,width,height);
}
/*
* function to draw bus route based on temp bus route
* a white line is drawn from point to point tracing out
* the temp bus route
* also, the displayed bus route is drawn as well.
* a black line is drawn from point to point tracing out
* the displayed bus route
*/
private void drawBusRoute(Graphics g) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
Point x=null,y=null;
//set color of displayed bus route
g2.setColor(C_BUSROUTE);
//draw displayed bus route
if (displayedBusRoute!=null) {
//displayed bus route is a valid bus route
//thus busRoute.length must be >=2
int rectHeight,rectWidth;
for (int i=1;i<displayedBusRoute.getLength();i++) {
//draw line from point (i-1) to i
x = displayedBusRoute.getPoint(i);
y = displayedBusRoute.getPoint(i-1);
//set x=top/left and y=bottom/right
if (x.getX()>y.getX() || x.getY()<y.getY()) {
//y is top left
Point temp;
temp=x;x=y;y=temp;
}
//calculate height and width of line(rect)
if (x.getX()==y.getX()) {
//vertical line
rectHeight=gridSize*5/4;
rectWidth=gridSize/4;
} else {
//horizontal line
rectHeight=gridSize/4;
rectWidth=gridSize*5/4;
}
x = convertGridToScreen(x,LOC_CENTER);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -