2012-01-23 9 views
1

迷路とGUIを使った私の冒険は続き、現時点ではどのグラフも見ることができますG=(V,E) 頂点は部屋で、辺はコネクタ(扉や壁)しかし、長方形の寸法は小さすぎるので、私はそれらを拡大しようとしましたが、長方形はお互いに1つずつ進みます。迷路の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

緑色の矩形はroomsであり、redblueconnectorsです。ご覧のとおり、長方形が小さすぎるので、「60」のようなサイズが必要です。しかし、私は、fillRectanglex,x1,y,y1の値の正しい組み合わせを見つけることができないようで、四角形は重なり合わないでしょう。

誰かがこれをどのように修正できるか説明してください。

+1

の先頭に行くあなたの宿題は、我々はあなたが解決を支援しようとしているということですか..? ;] – Sorceror

+0

これは本当に私の宿題ですが、私はあなたにそれらを解決するように依頼しませんでした。ここで見ることができるように、私は過去数日間作業していたコードを投稿しました。そのコードは、しかし私はそれを解決するために何が間違っているのか診断するのを手伝うように頼んだので、あなたの助けに感謝します。 – ron

+2

教師として(私は大学でも教えています)、攻撃的な意味はありませんでした。難しいと思われる部分があっても、その問題に取り組んでいてうれしく思います。 – 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クラスにない場合は、それらを作る;!])私はあなたの実際のコードに基づいての補正を行った

EDIT

、新しいバージョンを確認してください。あなたはまた、レベル生成に間違えている

、ループは、NullPointerExceptionが、コードの私の最初のバージョンにアップスローされた理由です、あなただけ>1ので、一つの部屋が欠落している必要があり、>=1で終了しなければなりません。

また、このコードはストレートBasicShapesクラスのコンストラクタ

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

コードはコンパイルされますが、プログラムは実行時にクラッシュします。私は迷路(PRIMアルゴリズムのようなもの)を構築するための私のアルゴリズムが含まれているので、グラフクラスを投稿しないことを好む。 PMにコードを送ることはできますか? – ron

+1

これはStackOverflowのルールに少し違反しています。編集内容を見ましたか?私はそれがClassCastExceptionをスローするかもしれないことに気がついたが、私はそれを解決する..それが動作しない場合(バージョンを編集する)、エラーメッセージを投稿してください。 – Sorceror

+0

わかりました、10倍。 – ron

関連する問題