2016-07-15 8 views
0

このクラスでは、部屋をランダムに作成し、角括弧(部屋を表す)をコンソールに表示します。しかし、私はGUIのJPanelにそのようなものを追加する方法を知りたがっていました。ルームジェネレータクラスは次のとおりです。JPanelにコンソールに出力するクラスを追加する方法

public class Rooms { 
    static final int width = 15, height = 10; 
    static final int rooms = 19; 

    static boolean[][] room = new boolean[width][height]; 

    static int neighborCount(int x, int y) { 
     int n = 0; 
     if (x > 0 && room[x-1][y]) n++; 
     if (y > 0 && room[x][y-1]) n++; 
     if (x < width-1 && room[x+1][y]) n++; 
     if (y < height-1 && room[x][y+1]) n++; 
     return n; 
    } 

    public void Rooms() { 
     room[width/2][height/2] = true; 
     Random r = new Random(); 
     int x, y, nc; 
     for (int i = 0; i < rooms; i++) { 
      while (true) { 
       x = r.nextInt(width); 
       y = r.nextInt(height); 
       nc = neighborCount(x, y); 
       if (!room[x][y] && nc == 1) break; 
      } 
      room[x][y] = true; 
     } 
     for (y = 0; y < height; y++) { 
      for (x = 0; x < width; x++) 
       System.out.print(room[x][y] ? "[]" : " "); 
       System.out.print(); 
     } 
    } 
} 

ありがとうございます!

答えて

0

おそらくjava.awt.Graphicsを使いたいと思うでしょう。それは線、長方形、円のような原始的な図形を描くことを可能にするThisはあなたを始めるかもしれません。セクション2では、JPanelでの描画方法についても説明します。

関連する問題