迷路と GUI の冒険は続きます。現時点G=(V,E)
では、頂点が部屋で、エッジがコネクタ (ドアまたは壁) であるグラフを確認できますが、長方形の寸法が小さすぎるため、それらを拡大しますが、長方形は互いに重なり合っています。
次のコードを考えると:
private void drawMaze(PaintEvent e) {
Graph maze = new Graph();
maze.generateMaze(25);
int i = 0;
int level = 25;
e.gc.setAntialias(SWT.ON);
e.gc.setBackground(new Color(e.display, 150, 150, 150));
e.gc.setLineWidth(12);
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_GREEN));
while (i < level) {
Connector connector = maze.getEdgeConnectorByIndex(i);
if (connector instanceof Door) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_GREEN));
Room room1 = ((Door)connector).getFirstRoom();
Room room2 = ((Door)connector).getSecondRoom();
int x = room1.getXcoordinate()+10;
int y = room1.getYcoordinate()+10;
int x1 = room2.getXcoordinate()+10;
int y1 = room2.getYcoordinate()+10;
e.gc.fillRectangle(x*30,y*30,20,20);
e.gc.fillRectangle(x1*30,y1*30,20,20);
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));
Room r1 = new Room(30*x,30*y);
Room r2 = new Room(30*x1,30*y1);
Coordinate c = this.checkWhereConnectorLocated(r1,r2);
if (c.getSign() == DIAGONAL)
e.gc.fillRectangle(c.getXCoordinate(),c.getYCoordinate(),10,20);
else
e.gc.fillRectangle(c.getXCoordinate(),c.getYCoordinate(),20,10);
}
if (connector instanceof Wall) {
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_GREEN));
Room room1 = ((Wall)connector).getFirstRoom();
Room room2 = ((Wall)connector).getSecondRoom();
int x = room1.getXcoordinate()+10;
int y = room1.getYcoordinate()+10;
int x1 = room2.getXcoordinate()+10;
int y1 = room2.getYcoordinate()+10;
e.gc.fillRectangle(x*30,y*30,20,20);
e.gc.fillRectangle(x1*30,y1*30,20,20);
e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_DARK_RED));
Room r1 = new Room(30*x,30*y);
Room r2 = new Room(30*x1,30*y1);
Coordinate c = this.checkWhereConnectorLocated(r1,r2);
if (c.getSign() == DIAGONAL)
e.gc.fillRectangle(c.getXCoordinate(),c.getYCoordinate(),10,20);
else
e.gc.fillRectangle(c.getXCoordinate(),c.getYCoordinate(),20,10);
}
i++;
}
}
// void org.eclipse.swt.graphics.GC.fillRectangle(int x, int y, int width, int
// height)
private Coordinate checkWhereConnectorLocated(Room room1,Room room2) {
int x = 0; int y = 0;
Coordinate coordinate ;
if (room1.getXcoordinate() == room2.getXcoordinate()) {
// same X coordinate
if (room1.getYcoordinate() > room2.getYcoordinate()) {
// ROOM1 is located above ROOM2 - same X different Y
x = room1.getXcoordinate();
y = room1.getYcoordinate()-10;
coordinate = new Coordinate(x,y);
}
else {
// ROOM2 is located above ROOM1
x = room1.getXcoordinate();
y = room2.getYcoordinate()-10;
coordinate = new Coordinate(x,y);
}
coordinate.setSign(HORIZONTAL);
return coordinate;
}
else if (room1.getYcoordinate() == room2.getYcoordinate()) {
// else maybe same Y coordinate - the X is changing
if (room1.getXcoordinate() > room2.getXcoordinate()) {
// ROOM1 is on the right of ROOM2 l
x = room1.getXcoordinate() - 10;
y = room2.getYcoordinate(); // same Y so there is no difference
// whom Y's we choose
coordinate = new Coordinate(x,y);
}
else {
// ROOM2 is on the right of ROOM1
x = room2.getXcoordinate() - 10;
y = room2.getYcoordinate(); // same Y so there is no difference
// whom Y's we choose
coordinate = new Coordinate(x,y);
}
coordinate.setSign(DIAGONAL);
return coordinate;
}
coordinate = new Coordinate(0,0);
return coordinate;
}
出力:
緑色の四角形はrooms
およびred
およびblue
ですconnectors
。ご覧のとおり、長方形が小さすぎるため、「60」のようなサイズが必要です。ただし、長方形が上下に重ならない とfillRectangle
の値の正しい組み合わせを見つけることができないようです。x,x1,y,y1
誰かがこれを修正する方法を説明できますか?