📄 mapcanvas.java
字号:
g2.fillRect((int)x.getX()-gridSize/8,(int)x.getY()-gridSize/8,
rectWidth,rectHeight);
}
}
//set color of temp bus route
g2.setColor(C_TEMP_BUSROUTE);
//draw temp bus route
//check if tempBusRoute is null
if (tempBusRoute!=null) {
//tempBusRoute is not null
if (tempBusRoute.getLength()==0) {
//draw nothing as no points in bus route
return;
} else if (tempBusRoute.getLength()==1) {
//draw a "dot" on the start Point
x = tempBusRoute.getPoint(0);
x = convertGridToScreen(x,LOC_CENTER);
g2.fillRect((int)x.getX()-gridSize/8,(int)x.getY()-gridSize/8,
gridSize/4,gridSize/4);
} else {
//busRoute.length >=2
int rectHeight,rectWidth;
for (int i=1;i<tempBusRoute.getLength();i++) {
//draw line from point (i-1) to i
x = tempBusRoute.getPoint(i);
y = tempBusRoute.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);
g2.fillRect((int)x.getX()-gridSize/8,(int)x.getY()-gridSize/8,
rectWidth,rectHeight);
}
}
}
}
/*
* function to draw bus stop 1 at point pt
* a rect is drawn in the topright corner of the square
* occupying 1/4 grid in width and 2/3 grid in height
*/
private void drawBusStop1(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_BUSSTOP);
int x1,y1,width,height;
x1=(int)convertGridToScreen(pt,LOC_BOTTOMLEFT).getX()+1;
y1=(int)convertGridToScreen(pt,LOC_BOTTOMLEFT).getY()+1;
g2.drawString("B.S1", x1, y1-2);
}
/*
* function to draw bus stop 2 at point pt
*/
private void drawBusStop2(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_BUSSTOP);
int x1,y1,width,height;
x1=(int)convertGridToScreen(pt,LOC_BOTTOMLEFT).getX()+1;
y1=(int)convertGridToScreen(pt,LOC_BOTTOMLEFT).getY()+1;
g2.drawString("B.S2", x1, y1-2);
}
/*
* function to drawRoad1 at point pt
* a square of color C_ROAD1 is drawn in the grid corresponding
*/
private void drawRoad1(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_ROAD1);
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);
g2.drawString("NS_Road", x1, y1-2);
Graphics g3 = g.create();
g3.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX()+gridSize/4;
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY()+gridSize*3/7;
width = gridSize*1/2;
height = gridSize/7;
g3.fillRect(x1,y1,width,height);
}
/*
* function to drawRoad2 at point pt
* a square of color C_ROAD2 is drawn in the grid corresponding
*/
private void drawRoad2(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_ROAD2);
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);
g2.drawString("WE_Road", x1, y1-2);
Graphics g3 = g.create();
g3.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX()+gridSize*3/7;
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY()+gridSize/4;
width = gridSize/7;
height = gridSize/2;
g3.fillRect(x1,y1,width,height);
}
/*
* function to drawRoad3 at point pt
* a square of color C_ROAD3 is drawn in the grid corresponding
*/
private void drawRoad3(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_ROAD3);
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);
g2.drawString("B.S1", x1, y1-2);
Graphics g3 = g.create();
g3.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX()+gridSize*3/7;
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY()+gridSize*2/3;
width = gridSize/7;
height = gridSize/3;
g3.fillRect(x1,y1,width,height);
Graphics g4 = g.create();
g4.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX();
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY()+gridSize*3/7;
width = gridSize/3;
height = gridSize/7;
g4.fillRect(x1,y1,width,height);
}
/*
* function to drawRoad4 at point pt
* a square of color C_ROAD4 is drawn in the grid corresponding
*/
private void drawRoad4(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_ROAD4);
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);
Graphics g3 = g.create();
g3.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX()+gridSize*3/7;
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY();
width = gridSize/7;
height = gridSize/3;
g3.fillRect(x1,y1,width,height);
Graphics g4 = g.create();
g4.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX();
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY()+gridSize*3/7;
width = gridSize/3;
height = gridSize/7;
g4.fillRect(x1,y1,width,height);
}
/*
* function to drawRoad5 at point pt
* a square of color C_ROAD5 is drawn in the grid corresponding
*/
private void drawRoad5(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_ROAD5);
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);
Graphics g3 = g.create();
g3.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX()+gridSize*3/7;
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY()+gridSize*2/3;
width = gridSize/7;
height = gridSize/3;
g3.fillRect(x1,y1,width,height);
Graphics g4 = g.create();
g4.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX()+gridSize*2/3;
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY()+gridSize*3/7;
width = gridSize/3;
height = gridSize/7;
g4.fillRect(x1,y1,width,height);
}
/*
* function to drawRoad6 at point pt
* a square of color C_ROAD6 is drawn in the grid corresponding
*
*/
private void drawRoad6(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_ROAD6);
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);
Graphics g3 = g.create();
g3.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX()+gridSize*3/7;
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY();
width = gridSize/7;
height = gridSize/3;
g3.fillRect(x1,y1,width,height);
Graphics g4 = g.create();
g4.setColor(Color.WHITE);
x1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getX()+gridSize*2/3;
y1=(int) convertGridToScreen(pt,LOC_TOPLEFT).getY()+gridSize*3/7;
width = gridSize/3;
height = gridSize/7;
g4.fillRect(x1,y1,width,height);
}
/*
* function to drawRoadJunction at point pt
* a square of color C_ROADJUNCTION is drawn in the grid corresponding
*/
private void drawRoadJunction(Graphics g,Point pt) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_ROADJUNCTION);
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 Grid of map
*/
private void drawGrid(Graphics g) {
//create new graphics object so as not to mess with original graphics
Graphics g2= g.create();
//calculate offset to center canvas if canvas is smaller then
//scrollPane
offsetX = (getWidth()-drawingWidth)/2;
if (offsetX<0) {
offsetX=0;
}
offsetY = (getHeight()-drawingHeight)/2;
if (offsetY<0) {
offsetX=0;
}
g2.translate(offsetX,offsetY);
g2.setColor(C_BACKGROUD);
//draw grid background
g2.fillRect(0,0,drawingWidth,drawingHeight);
//draw gridline
g2.setColor(C_GRIDLINE);
//draw vertical line
for (int x=1;x<mapSizeX;x++) {
g2.drawLine(x*gridSize,0,x*gridSize,drawingHeight);
}
//draw horizontal line
for (int y=1;y<mapSizeY;y++) {
g2.drawLine(0,y*gridSize,drawingWidth,y*gridSize);
}
}
/*
* function to draw selected at point pt
* a hollow square of color C_SELECTED is drawn in the grid corresponding
* to the location of the selected point
*/
//draw a box around selected item
private void drawSelected(Graphics g) {
//create new graphics object so as not to mess with original graphics
Graphics g2 = g.create();
g2.setColor(C_SELECTED);
//check if it's in bus route mode
if (mode==BUSROUTE) {
//don't draw selected
return;
}
int height,width,x1,y1;
Point x,y;
Tools t = mapData.getSelected();
//check to see is point is selected
if (selectedPoint!=null) {
//draw box at selected point instead
x = convertGridToScreen(selectedPoint,LOC_TOPLEFT);
width = gridSize;
height = gridSize;
//check if there is a selected object
} else if (t!=null) {
//check if it's pt or area tools
if (t instanceof AreaTools) {
//area tool, get top left and bottom right coordinate
AreaTools t2 = (AreaTools) t;
Point pt1 =t2.getLocation1();
Point pt2 =t2.getLocation2();
if (pt1.getY()>pt2.getY() || pt1.getX()<pt2.getX()) {
//pt1 is at top left
x = convertGridToScreen(pt1,LOC_TOPLEFT);
y = convertGridToScreen(pt2,LOC_BOTTOMRIGHT);
} else {
//pt2 is at top left
x = convertGridToScreen(pt2,LOC_TOPLEFT);
y = convertGridToScreen(pt1,LOC_BOTTOMRIGHT);
}
height = (int) (y.getY()-x.getY());
width = (int) (y.getX()-x.getX());
} else {
//pt tool, get top left and draw a magenta box surrounding the object
PtTools t2 = (PtTools) t;
x = convertGridToScreen(t2.getLocation(),LOC_TOPLEFT);
width = gridSize;
height = gridSize;
}
} else return;
x1 = (int) x.getX();
y1 = (int) x.getY();
g2.drawRect(x1,y1,width,height);
}
/*
* function to draw traffic light at point pt
*
*/
private void drawTrafficLight(Graphics g,Point pt) {
Graphics g2 = g.create();
int x1,y1,width,height;
//width = height = gridSize*2/9;
x1=(int)convertGridToScreen(pt,LOC_BOTTOMLEFT).getX()+1;
y1=(int)convertGridToScreen(pt,LOC_BOTTOMLEFT).getY()+1;
g2.setColor(Color.GREEN);
g2.drawString("T.L", x1, y1-2);
}
/*
* returns gridSize of mapCanvas
*/
public int getGridSize() {
return gridSize;
}
/*
* returns mapHeight of mapCanvas
*/
public int getMapSizeY() {
return mapSizeY;
}
/*
* returns mapWidth of mapCanvas
*/
public int getMapSizeX() {
return mapSizeX;
}
/*
* function to return where the bottom left coordinates of where
* the drawing takes place. Can be visualize as the bottom left coordinate
* of point (0,0) [grid coordinate]. Coordinates returned in in screen
* coordinates
*/
private Point getOrigin() {
return new Point(offsetX,offsetY+drawingHeight);
}
/*
* return start index of grid coordinates
* start index corresponding to the what number is the
* first reference point, ie, (0,0) or (1,1)
*/
public int getStartIndex() {
return startIndex;
}
/*
* function to initialize the mapCanvas
*/
private void initialize(int mapSizeX,int mapSizeY,int gridSize) {
//set members of mapCanvas
this.mapSizeY=mapSizeY;
this.mapSizeX=mapSizeX;
this.gridSize=gridSize;
drawingHeight=mapSizeY*gridSize;
drawingWidth=mapSizeX*gridSize;
offsetX=0;
offsetY=0;
startIndex=0;
//add mouse listerner
addMouseListener(this);
addMouseMotionListener(this);
//set preferred size of mapCanvas
this.setPreferredSize(new Dimension(drawingWidth,drawingHeight));
}
/*
* function called automatically when user clicks on the mouse
* depending on current mode of operation, corresponding function
* is called
*/
public void mouseClicked(MouseEvent arg0) {
//set pointClicked to the coordinates of grid
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -