Java 矩形重叠,同时试图扩大迷宫的图形用户界面
我在迷宫和GUI中的冒险还在继续,现在我可以看到任何图形Java 矩形重叠,同时试图扩大迷宫的图形用户界面,java,user-interface,swt,maze,Java,User Interface,Swt,Maze,我在迷宫和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.
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;
}
private void drawMaze(PaintEvent e){
图形迷宫=新图形();
迷宫生成码(25);
int i=0;
智力水平=25;
e、 gc.setAntialias(SWT.ON);
e、 总承包商背景(新颜色(如显示器,150、150、150));
e、 gc.设定线宽(12);
e、 gc.setBackground(例如display.getSystemColor(SWT.COLOR\u深绿色));
while(iroom2.getYcoordinate()){
//房间1位于房间2上方-相同X不同Y
x=room1.getXcoordinate();
y=room1.getYcoordinate()-10;
坐标=新坐标(x,y);
}
否则{
//房间2位于房间1的上方
x=room1.getXcoordinate();
y=room2.getYcoordinate()-10;
坐标=新坐标(x,y);
}
坐标设置标志(水平);
返回坐标;
}
else if(room1.getYcoordinate()==room2.getYcoordinate()){
//否则可能是相同的Y坐标-X在变化
if(room1.getXcoordinate()>room2.getXcoordinate()){
//1号房间在2号房间的右边
x=room1.getXcoordinate()-10;
y=room2.getYcoordinate();//相同的y,所以没有区别
//我们选谁
坐标=新坐标(x,y);
}
否则{
//2号房间在1号房间的右边
x=room2.getXcoordinate()-10;
y=room2.getYcoordinate();//相同的y,所以没有区别
//我们选谁
坐标=新坐标(x,y);
}
坐标设置符号(对角线);
返回坐标;
}
坐标=新坐标(0,0);
返回坐标;
}
输出:
绿色矩形是房间
,红色
和蓝色
是连接器
。正如你所看到的,矩形太小了,我需要一个像“60”这样的尺寸。但是,我似乎找不到fillRectangle
和x,x1,y,y1
的正确值组合,其中矩形不会是一个大于另一个
有人能解释一下我如何解决这个问题吗 您没有发布数据,因此我不能完全确定我的方法是否能正确呈现您想要的结果,但我确实简化了您的代码,并为您添加了一些常量,以便免费更改基本大小
private final int boxSize = 60;
private final int connectorSize = 20;
private final int topOffset = 50;
private final int leftOffset = 50;
private Color roomColor = null;
private Color wallColor = null;
private Color doorColor = null;
private void drawMaze(PaintEvent e) {
GC gc = e.gc;
int i = 0;
int level = 25;
Graph maze = new Graph();
maze.generateMaze(level);
gc.setAntialias(SWT.ON);
gc.setBackground(new Color(e.display, 150, 150, 150));
while (i < level) {
Connector connector = maze.getEdgeConnectorByIndex(i);
gc.setBackground(roomColor);
Room room1 = connector.getFirstRoom();
Room room2 = connector.getSecondRoom();
// left top corner X of room is offset from left side + coordinate mul size of box plus number of connectors between boxes already drawn before current room mul connector size
int roomX = leftOffset + room1.getXcoordinate() * boxSize + (room1.getXcoordinate() - 1) * connectorSize;
// left top corner Y of room is offset from top + coordinate mul size of box plus number of connectors between boxes already drawn above current room mul connector size
int roomY = topOffset + room1.getYcoordinate() * boxSize + (room1.getYcoordinate() - 1) * connectorSize;
gc.fillRectangle(roomX, roomY, boxSize, boxSize);
if (connector instanceof Door) gc.setBackground(doorColor);
if (connector instanceof Wall) gc.setBackground(wallColor);
int connectorX = 0;
int connectorY = 0;
int connectorWidth = 0;
int connectorHeight = 0;
// room have same X, second is above or under the first
if (room1.getXcoordinate() == room2.getXcoordinate()) {
connectorWidth = boxSize;
connectorHeight = connectorSize;
connectorX = roomX;
// check if it's under
if (room1.getYcoordinate() > room2.getYcoordinate()) connectorY = roomY - connectorSize;
else connectorY = roomY + boxSize;
}
// room have same Y, second is on right or left side of the first
else {
connectorWidth = connectorSize;
connectorHeight = boxSize;
connectorY = roomY;
// check if it's right side
if (room1.getXcoordinate() > room2.getXcoordinate()) connectorX = roomX - connectorSize;
else connectorX = roomX + boxSize;
}
gc.fillRectangle(connectorX, connectorY, connectorWidth, connectorHeight);
// draw the second room
gc.setBackground(roomColor);
roomX = leftOffset + room2.getXcoordinate() * boxSize + (room2.getXcoordinate() - 1) * connectorSize;
roomY = topOffset + room2.getYcoordinate() * boxSize + (room2.getYcoordinate() - 1) * connectorSize;
gc.fillRectangle(roomX, roomY, boxSize, boxSize);
i++;
}
}
您没有发布数据,因此我不能完全确定我的方法是否能正确呈现您想要的结果,但我确实简化了您的代码,并为您添加了一些常量,以便免费更改基本大小
private final int boxSize = 60;
private final int connectorSize = 20;
private final int topOffset = 50;
private final int leftOffset = 50;
private Color roomColor = null;
private Color wallColor = null;
private Color doorColor = null;
private void drawMaze(PaintEvent e) {
GC gc = e.gc;
int i = 0;
int level = 25;
Graph maze = new Graph();
maze.generateMaze(level);
gc.setAntialias(SWT.ON);
gc.setBackground(new Color(e.display, 150, 150, 150));
while (i < level) {
Connector connector = maze.getEdgeConnectorByIndex(i);
gc.setBackground(roomColor);
Room room1 = connector.getFirstRoom();
Room room2 = connector.getSecondRoom();
// left top corner X of room is offset from left side + coordinate mul size of box plus number of connectors between boxes already drawn before current room mul connector size
int roomX = leftOffset + room1.getXcoordinate() * boxSize + (room1.getXcoordinate() - 1) * connectorSize;
// left top corner Y of room is offset from top + coordinate mul size of box plus number of connectors between boxes already drawn above current room mul connector size
int roomY = topOffset + room1.getYcoordinate() * boxSize + (room1.getYcoordinate() - 1) * connectorSize;
gc.fillRectangle(roomX, roomY, boxSize, boxSize);
if (connector instanceof Door) gc.setBackground(doorColor);
if (connector instanceof Wall) gc.setBackground(wallColor);
int connectorX = 0;
int connectorY = 0;
int connectorWidth = 0;
int connectorHeight = 0;
// room have same X, second is above or under the first
if (room1.getXcoordinate() == room2.getXcoordinate()) {
connectorWidth = boxSize;
connectorHeight = connectorSize;
connectorX = roomX;
// check if it's under
if (room1.getYcoordinate() > room2.getYcoordinate()) connectorY = roomY - connectorSize;
else connectorY = roomY + boxSize;
}
// room have same Y, second is on right or left side of the first
else {
connectorWidth = connectorSize;
connectorHeight = boxSize;
connectorY = roomY;
// check if it's right side
if (room1.getXcoordinate() > room2.getXcoordinate()) connectorX = roomX - connectorSize;
else connectorX = roomX + boxSize;
}
gc.fillRectangle(connectorX, connectorY, connectorWidth, connectorHeight);
// draw the second room
gc.setBackground(roomColor);
roomX = leftOffset + room2.getXcoordinate() * boxSize + (room2.getXcoordinate() - 1) * connectorSize;
roomY = topOffset + room2.getYcoordinate() * boxSize + (room2.getYcoordinate() - 1) * connectorSize;
gc.fillRectangle(roomX, roomY, boxSize, boxSize);
i++;
}
}
是你的家庭作业,我们正试图帮助你解决…?;]这确实是我的家庭作业,但我没有要求你解决它们。正如你在这里看到的,我发布了我过去几天一直在编写的代码,我在代码上非常努力,但是我要求你帮助我诊断出它的问题,而不是解决它,因此,我非常感谢您的帮助。它没有冒犯性的意思;】,作为一名教师(我也在大学任教),我很高兴你正在解决这个问题,并试图解决它,即使有些部分看起来很难……这是我们试图帮助你解决的家庭作业吗?;]这确实是我的家庭作业,但我没有要求你解决它们。正如你在这里看到的,我发布了我过去几天一直在编写的代码,我在代码上非常努力,但是我要求你帮助我诊断它的问题,而不是