📄 mapdata.java
字号:
//added hasChanged and notifyPbservers to everytime selected is changed
//end add by JY
{
if(matrixElem[(int)p1.getY()][(int)p1.getX()].size() == 0)
return false;
else
for( int i = 0; i < matrixElem[(int)p1.getY()][(int)p1.getX()].size(); i++)
if( ((Tools)matrixElem[(int)p1.getY()][(int)p1.getX()].get( i )).getID() == c ){
selectedPoint = p1;
selectedTool = matrixElem[(int)p1.getY()][(int)p1.getX()].get( i );
selectedToolID = c;
//added by JY
setChanged();
notifyObservers();
//end added by JY
return true;
}
return false;
}
// remove selected Tool
public boolean removeSelected()
{
if(selectedToolID == 0)
return false;
// selectedID is other tools
else{
matrixElem[(int)selectedPoint.getY()][(int)selectedPoint.getX()].remove(selectedTool);
selectedTool = null;
selectedToolID = 0;
selectedPoint = null;
//added by JY
setChanged();
notifyObservers();
//end added by JY
return true;
}
}
public boolean cut()
{
if ( selectedToolID == 0 )
return false;
else if (!(selectedToolID == 12) )
{
sta.push(selectedTool);
removeSelected();
cutobject=true;
return true;
}
else
return false;
}
public boolean copy()
{
if ( selectedToolID == 0 )
return false;
else if (!(selectedToolID == 12) )
{
sta.push(selectedTool);
cutobject=true;
return true;
}
else
return false;
}
public boolean paste(Point p)
{
if (cutobject==true)
{
/*commented by JY
selectedTool = sta.pop();
//added by JY
((PtTools) selectedTool).setLocation(p);
//end added by JY
add(p, (Tools)selectedTool);
cutobject = false;
return true;
end commented by JY*/
//added by JY
PtTools tempTool = (PtTools) sta.peek();
Point prev = new Point(tempTool.getLocation());
((PtTools) tempTool).setLocation(p);
//attempt to paste to mapData
if (add(p,tempTool)) {
//paste success
//set selectedTool as sta
selectedTool=sta.pop();
cutobject=false;
return true;
} else {
//paste fail
//revert tempTool to original location
tempTool.setLocation(prev);
return false;
}
//end added by JY
}
else return false;
}
public boolean move(Point p)
{
/* commented by JY
cut();
selectedTool = sta.pop();
return paste(p);
end comment by JY */
if (cut()) {
//cut is successful,must be point tool
if (paste(p)) {
//paste successful
return true;
} else {
//paste unsuccessful
PtTools a = (PtTools) sta.peek();
paste(a.getLocation());
return false;
}
} else {
//cut failed
return false;
}
}
// Special BusRoute Methods
/* Methods dealing with bus route
*/
/* Get number of bus routes
*/
public int busRouteSize() {
return busRoute.size();
}
/* Get the bus route at the specified position, index starts from 0
*/
public BusRoute getOneBusRoute(int index) {
return (BusRoute)busRoute.get(index);
}
/* Remove the bus route at the specified position, index starts from 0
*/
public BusRoute removeBusRoute(int index) {
return (BusRoute)busRoute.remove(index);
}
public LinkedList getBusRoute() {
return busRoute;
}
/* public boolean checkRoad(Road r){
Point p1 = r.getStart();
Point p2 = r.getEnd();
int x1 = (int)p1.getX();
int y1 = (int)p1.getY();
//changed by JY
int x2 = (int)p2.getX();
int y2 = (int)p2.getY();
//end changed by JY
if( !r.getDirect()) {// if road is hori
for(int j=x1;j<=x2;j++){
Point k = new Point(j,y1);
if(isRiver(k) || isTree(k) || isDepot (k) || isBusStop(k) || isInterchange(k)|| isOnBusRoute(k))
return false;
}
return true;
}
else { // if road is vertical
for(int j=y1;j<=y2;j++){
Point k = new Point(x1,j);
if(isRiver(k) || isTree(k) || isDepot (k) || isBusStop(k) || isInterchange(k)|| isOnBusRoute(k))
return false;
}
return true;
}
}
*/
public NodeData[][] getMap() {
int id1,id2,id3,id4,id5,id6,id7,id8,id9,id10,id11;
NodeData[][] nodeDataAr = new NodeData[22][22];
id1=1;id2=1;id3=1;id3=1;id4=1;id5=1;id6=1;id7=1;id8=1;id9=1;id10=1;id11=1;
for( int j = 0; j < 22; j++ ) {
for( int k = 0; k < 22; k++ ) {
Point temP = new Point(k-1,j-1);
if(j == 0||k == 0|| j == 21||k == 21) {
nodeDataAr[j][k] = new NodeData("boundary","boundary",7,-1);
}
else if(matrixElem[j][k].size() != 0) {
if(((Tools)matrixElem[j][k].getFirst()).getID() == 8) { //road6
nodeDataAr[j][k] = new NodeData(8,id8);
id8++;
}
else if(((Tools)matrixElem[j][k].getFirst()).getID() == 9) {//junction
nodeDataAr[j][k] = new NodeData(9,id9);
id9++;
}
else if(((Tools)matrixElem[j][k].getFirst()).getID() == 5) {//road3
nodeDataAr[j][k] = new NodeData(5,id5);
id5++;
}
else if(((Tools)matrixElem[j][k].getFirst()).getID() == 6) { // road4
nodeDataAr[j][k] = new NodeData(6,id6);
id6++;
}
else if(((Tools)matrixElem[j][k].getFirst()).getID() == 7) { // road5
nodeDataAr[j][k] = new NodeData(7,id7);
id7++;
}
else if(((Tools)matrixElem[j][k].getFirst()).getID() == 4) { // road2
nodeDataAr[j][k] = new NodeData(4,id4);
id4++;
}
else if(((Tools)matrixElem[j][k].getFirst()).getID() == 3) { // road1
nodeDataAr[j][k] = new NodeData(3,id3);
id3++;
}
else if(((Tools)matrixElem[j][k].getFirst()).getID() == 2) { // busstop2
nodeDataAr[j][k] = new NodeData(2,id2);
id2++;
}
else if(((Tools)matrixElem[j][k].getFirst()).getID() == 1) { // busstop1
nodeDataAr[j][k] = new NodeData(1,id1);
id1++;
}
else if(((Tools)matrixElem[j][k].getFirst()).getID() == 10) { // depot
nodeDataAr[j][k] = new NodeData(10,id10);
id10++;
}
}
else
nodeDataAr[j][k] = new NodeData(null,null,0,-1); // empty
}
}
return nodeDataAr;
}
/* added by JY*/
public Tools getSelected() {
return (Tools)selectedTool;
}
//end added by JY
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -