2012-01-23 3 views
1

미로와 GUI가있는 내 모험이 계속되고 현재 어떤 그래프도 볼 수 있습니다 G=(V,E) Vertices가 방이고 가장자리는 커넥터 (문 또는 벽)입니다. , 그러나 직사각형의 크기가 너무 작아서 나는 그것을 확대하려고 시도했지만 직사각형은 서로 위에 하나씩 나아 간다.미로의 GUI를 확대하려고 시도 할 때 사각형이 겹치다

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; 

} 

출력 :

다음 코드를 감안할 때

enter image description here

녹색 사각형이 roomsredblue있는 connectors이다. 보시다시피, 직사각형이 너무 작아서 "60"과 같은 크기가 필요합니다. 그러나 fillRectanglex,x1,y,y1에 대한 올바른 조합의 값을 찾을 수없는 것 같습니다. 여기서 직사각형은 서로 겹치지 않습니다.

누군가이 문제를 해결할 수있는 방법을 설명해 주시겠습니까?

+1

당신이 숙제 우리는 당신이 해결하려고하는 것입니다 BasicShapes 클래스 생성자의 시작 부분에 간다 ..? ;] – Sorceror

+0

이것은 실제로 내 숙제이지만, 나는 그들을 풀어달라고하지 않았다. 여기서 볼 수 있듯이, 나는 지난 며칠간 작업해온 코드를 게시했다. 그 코드, 그러나 내가 당신에게 내가 잘못한 것을 진단하도록 도와달라고 부탁했다. 그것을 해결하지 않기 위해, 나는 당신의 도움에 감사 할 것이다. – ron

+2

그것은 불쾌한 의미가 없었습니다.] 선생님 (저는 또한 University에서 가르칩니다)으로서, 열심히하는 부분이 있어도 문제를 해결하고 해결하려고 노력한 것을 기쁘게 생각합니다. – Sorceror

답변

1

당신은 데이터를 게시하지 않았기 때문에 내 접근 방식이 원하는 결과를 올바르게 렌더링 할 수 있다고 확신하지는 않지만 코드를 단순화하고 기본 크기의 자유로운 변경을 위해 몇 가지 상수를 추가했습니다.

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++; 
    } 
} 

Ancestor을 올바른 클래스로 바꿉니다. (당신이 어떤 공통 조상 클래스가 없거나 그 방법이 Connector 클래스에없는 경우, 그들을 만들;!])

편집 나는 실제 코드를 기반으로의 보정을했습니다

, 새로운 버전을 확인하십시오. 또한 레벨 세대에서 실수 한

은, 루프는 NullPointerException이이 코드의 첫 버전에 던져했다 그 이유는, 당신은 단지 >1 그렇게 한 방에 누락이, >=1에 완료해야합니다.

또한이 코드는 바로

roomColor = display.getSystemColor(SWT.COLOR_DARK_GREEN); 
wallColor = display.getSystemColor(SWT.COLOR_RED); 
doorColor = display.getSystemColor(SWT.COLOR_BLUE); 
+0

코드가 컴파일되지만 런타임에 프로그램이 중단됩니다. 나는 PRIM 알고리즘과 같은 미로를 구축하기위한 알고리즘을 포함하기 때문에 Graph 클래스를 게시하지 않는 것을 선호한다. PM에 코드를 보낼 수 있습니까? – ron

+1

StackOverflow 규칙을 약간 위반합니다. 편집 한 내용을 보았습니까? 나는 그것이 ClassCastException을 던질지도 모른다는 것을 깨달았지만, 그것을 해결했다. (심지어 편집 버전), 작동하지 않습니다 오류 메시지를 게시하십시오. – Sorceror

+0

알겠습니다, 10 배. – ron

관련 문제