📄 mapcanvas.java
字号:
pointClicked = convertScreenToGrid(arg0.getPoint());
//check which mode it is
switch (mode) {
case Depot:
//attempt to add Depot
addDepot();
break;
case ROAD1:
//attempt to add road
addRoad1();
break;
case ROAD2:
//attempt to add road
addRoad2();
break;
case ROAD3:
//attempt to add road
addRoad3();
break;
case ROAD4:
//attempt to add road
addRoad4();
break;
case ROAD5:
//attempt to add road
addRoad5();
break;
case ROAD6:
//attempt to add road
addRoad6();
break;
case ROADJUNCTION:
//attempt to add road junction
addRoadJunction();
break;
case BUSSTOP1:
//attempt to add bus stop
addBusStop1();
break;
case BUSSTOP2:
//attempt to add bus stop
addBusStop2();
case MOVE:
//attempt to move selected object to new location
moveSelectedObject();
break;
case SELECT:
//attempt to select object
selectPoint(arg0.getPoint());
break;
case BUSROUTE:
//attempt to create bus route
createBusRoute();
break;
}
}
/*
* function automatically called when mouse is dragged
* functionality not used
*/
public void mouseDragged(MouseEvent arg0) {
}
/*
* function automatically called when mouse enters the mapCanvas
* functionality not used
*/
public void mouseEntered(MouseEvent arg0) {
}
/*
* function automatically called when mouse is exit the mapCanvas
* functionality not used
*/
public void mouseExited(MouseEvent arg0) {
}
/*
* function automatically called when mouse is moved
* sets mouseOverPoint to be the coordinate which the mouse is
* over (grid coordinates)
*/
public void mouseMoved(MouseEvent arg0) {
//check if current point mouse is over is the same as previous point
if (!mouseOverPoint.equals(convertScreenToGrid(arg0.getPoint()))) {
//point is different, set current mouseOverPoint as new coordinate
mouseOverPoint.setLocation(convertScreenToGrid(arg0.getPoint()));
//update status
frame.setCoordinates(mouseOverPoint);
//call repaint() to draw temp line
repaint();
}
}
/*
* function automatically called when mouse is pressed
* functionality not used
*/
public void mousePressed(MouseEvent arg0) {
}
/*
* function automatically called when mouse is released
* functionality not used
*/
public void mouseReleased(MouseEvent arg0) {
}
/*
* function is called to move selected object to new point
*/
private void moveSelectedObject() {
if (mapData.move(pointClicked)) {
//object successfully moved
frame.setStatus("Object moved");
} else {
//fail to move object
frame.setStatus("Object could not be moved");
}
}
/*
* function automatically called when there is a need to paintComponent
* calls functions to draw: grid and respective objects
*/
protected void paintComponent(Graphics g) {
//call super function
super.paintComponent(g);
//create new graphics so as not to mess up with original graphics
Graphics g2 = g.create();
//draw the grid of the map
drawGrid(g2);
//check each point in the map for objects
for (int x=0;x<20;x++) {
for (int y=0;y<20;y++) {
//for each point in map from 0,0 to 19,19
Point tmpPt = new Point(x,y);
//tmpPt is a road
if (mapData.isRoad1(tmpPt)) {
//tmpPt is a road 1,draw it
drawRoad1(g,tmpPt);
}
else if (mapData.isRoad2(tmpPt)) {
//tmpPt is a road 2,draw it
drawRoad2(g,tmpPt);
}
else if (mapData.isRoad3(tmpPt)) {
//tmpPt is a road 3,draw it
drawRoad3(g,tmpPt);
}
else if (mapData.isRoad4(tmpPt)) {
//tmpPt is a road 4,draw it
drawRoad4(g,tmpPt);
}
else if (mapData.isRoad5(tmpPt)) {
//tmpPt is a road 5,draw it
drawRoad5(g,tmpPt);
}
else if (mapData.isRoad6(tmpPt)) {
//tmpPt is a road 6,draw it
drawRoad6(g,tmpPt);
}
else if (mapData.isRoadJunction(tmpPt)) {
//tmpPt is a road 6,draw it
drawRoadJunction(g,tmpPt);
}
//check if there's anything on top of the road
if (mapData.isTrafficLight(tmpPt)) {
//traffic light is on top, draw it
drawTrafficLight(g,tmpPt);
}
if (mapData.isBusStop1(tmpPt)) {
//bus stop is on top, draw it
drawBusStop1(g,tmpPt);
} else if (mapData.isBusStop2(tmpPt)) {
//bus interchange it on top, draw it
drawBusStop2(g,tmpPt);
}
//not a road then
if (mapData.isDepot(tmpPt)) {
//is a Depot, draw it
drawDepot(g,tmpPt);
}
}
}
if (mode==BUSROUTE) {
//is in bus route mode, draw bus route
drawBusRoute(g);
}
//draws selection box
drawSelected(g);
}
/*
* function is called to select the object on point pt
* it checks which area is clicked to determine if any overlay
* object is selected instead(busstop,trafficlight)
* also,if there is no object, it sets a point as selected instead
* when no object is selected, selectedPoint is set, else, it is null
*/
private void selectPoint(Point pt) {
Point pt1 = convertScreenToGrid(pt);
//some object will be selected, set selectedPoint as null
selectedPoint=null;
//area clicked is road region
//select the underlying road
if (mapData.setSelected(pt1,ID_ROAD1)) {
frame.setStatus("Road 1 selected");
} else if(mapData.setSelected(pt1,ID_ROAD2)){
frame.setStatus("Road 2 selected");
} else if(mapData.setSelected(pt1,ID_ROAD3)){
frame.setStatus("Road 3 selected");
} else if(mapData.setSelected(pt1,ID_ROAD4)){
frame.setStatus("Road 4 selected");
} else if(mapData.setSelected(pt1,ID_ROAD5)){
frame.setStatus("Road 5 selected");
} else if(mapData.setSelected(pt1,ID_ROAD6)){
frame.setStatus("Road 6 selected");
} else if(mapData.setSelected(pt1,ID_ROADJUNCTION)){
frame.setStatus("Road Junction selected");
}else if (mapData.setSelected(pt1,ID_BUSSTOP1)) {
frame.setStatus("Bus stop 1 selected");
}else if (mapData.setSelected(pt1,ID_BUSSTOP2)) {
frame.setStatus("Bus stop 2 selected");
}
//point is not on road, check is there any tool on point
else if (mapData.setSelected(pt1,ID_Depot)) {
//Depot exist,set selectedPoint as null
selectedPoint = null;
frame.setStatus("Depot selected");
} else {
//nothing on the road, selected the point
selectedPoint = pt1;
}
repaint();
}
/*
* function set canvasHeight
*/
public void setCanvasHeight(int canvasHeight) {
this.mapSizeY = canvasHeight;
}
/*
* function set canvasWidth
*/
public void setCanvasWidth(int canvasWidth) {
this.mapSizeX = canvasWidth;
}
/*
* functions attempts to cut selected object from mapData
* and output results in status
*/
public void cut() {
//check if an object was selected
if (selectedPoint!=null) {
frame.setStatus("No object was selected");
return;
}
//object was selected, proceed to cut
if (mapData.cut()) {
//cut success
frame.setStatus("Selected object cut");
} else {
//cut fail
frame.setStatus("Selected object could not be cut");
}
}
/*
* functions attempts to copy selected object from mapData
* and output results in status
*/
public void copy() {
//check if an object was selected
if (selectedPoint!=null) {
frame.setStatus("No object was selected");
return;
}
//object was selected, proceed to cut
if (mapData.copy()) {
//copy success
frame.setStatus("Selected object copied");
} else {
//copy fail
frame.setStatus("Selected object could not be copied");
}
}
/*
* functions attempts to paste selected object to mapData
* and output results in status
*/
public void paste() {
if (mapData.paste(pointClicked)) {
frame.setStatus("Selected object pasted");
} else {
frame.setStatus("Selected object could not be pasted");
}
}
/*
* functions attempts to delete selected object from mapData
* and output results in status
*/
public void delete() {
//check if an object was selected
if (selectedPoint!=null) {
frame.setStatus("No object was selected");
return;
}
//object was selected
if (mapData.removeSelected()) {
//delete sucess
frame.setStatus("Selected object deleted");
} else {
//delete fails
frame.setStatus("Selected object could not be deleted");
}
}
/*
* function sets mode of the mapCanvas which determine operation of
* mapCanvas
* also, it clears temp data used to display info to user(storepoint
* and tempBusRoute
*/
public void setMode(int mode) {
//clear storePoint as mode changes
storePoint = null;
//clear busRoute as mode changes
tempBusRoute = null;
displayedBusRoute=null;
//set the mode
this.mode=mode;
}
/*
* functions sets the main frame as reference to mapCanvas
* this is so that mapCanvas can call function in main frame to
* interact with other component in main frame
*/
public void setFrame(CmeGUI frame) {
this.frame = frame;
}
/*
* function called whenever data in mapData changes, this is
* mainly so that mapCanvas can repaint itself only when necessary
* instead of repaint itself every fixed interval
*/
public void update(Observable arg0, Object arg1) {
//update selected property in case selectedTool changes
int selectedToolID=0;
//check if selected tool is of class AreaTools
if (mapData.getSelected() instanceof AreaTools && mode!=BUSROUTE) {
//procees to check whether it is a road
AreaTools a = (AreaTools) mapData.getSelected();
if (a.getID()==ID_ROAD1) {
selectedToolID = ROAD1;
} else if (a.getID()==ID_ROAD2) {
selectedToolID = ROAD2;
} else if (a.getID()==ID_ROAD3) {
selectedToolID = ROAD3;
} else if (a.getID()==ID_ROAD4) {
selectedToolID = ROAD4;
} else if (a.getID()==ID_ROAD5) {
selectedToolID = ROAD5;
} else if (a.getID()==ID_ROAD6) {
selectedToolID = ROAD6;
} else if (a.getID()==ID_ROADJUNCTION) {
selectedToolID = ROADJUNCTION;
} else if (a.getID()==ID_BUSROUTE) {
selectedToolID = BUSROUTE;
}
//set the propertyValue of propertyPane to be
//name or bus no of selected tool
frame.setPropertyValue(a.getName());
//set property selected of propertyPane to ID of selected tool
frame.setPropertySelected(selectedToolID);
}
//repaint the whole mapCanvas to reflect changes in data
repaint();
}
/*
* function sets the name of selected tool if it has a name property
* this function is to be called by main frame for interactivity between
* mapCanvas and propertyPane
*/
public void setSelectedValue(String text) {
Tools a = mapData.getSelected();
//check if value is for busRoute
if (mode==BUSROUTE) {
print("busroute");
//value is for bus route
//set bus number to text
int busNo = Integer.parseInt(text);
displayedBusRoute.setBusNum(busNo);
//call busRouteDialog to update the bus route dialog
frame.updateBusRouteDialog();
} else if (a instanceof AreaTools) {
print("areatools");
//value is for AreaTools
//set name to text
AreaTools a2 = (AreaTools) a;
a2.setName(text);
}
}
/*
* functions sets the displayedBusRoute.
* this function should be called by main frame for interactivity between
* mapCanvas and busRouteDialog
*/
public void setDisplayedBusRoute(BusRoute busRoute) {
displayedBusRoute = busRoute;
//sets property page to display bus number of displayedBusRoute
frame.setPropertyValue(String.valueOf(displayedBusRoute.getBusNum()));
//repaints mapCanvas to reflect the change in displayedBusRoute
repaint();
}
/*
* function sets mapData to be used
*/
public void setMapData(MapData mapData) {
//set mapData
this.mapData=mapData;
//adds itself as observer of mapData
this.mapData.addObserver(this);
//clear selectedPoint
selectedPoint=null;
//clear pointClicked
pointClicked=null;
//clear displayedBusRoute
displayedBusRoute=null;
//repaint canvas
repaint();
}
private void print(String string) {
// TODO Auto-generated method stub
System.out.println(string);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -