我写过一个Tromino Puzzle程序,除了用户指定的“空洞”(空方块)外,用tromino填充用户指定大小的棋盘。
我现在需要做的是在GUI窗口中显示。用解决方案展示董事会和trominos。我完全迷失了如何让我的代码在GUI窗口中显示它。 我该怎么做?
注意:我在代码中使用了一个巨大的DrawingPanel类
这是我的代码:
import java.util.*;
public class trominoZ {
private int[][] grid;
private int currentNum;
// Pre-condition: size must be a perfect power of 2 and 0<=x<size, 0<=y<size
// Post-condition: creates an empty tromino object with dimensions size x size.
public void tromino(int size, int x, int y) {
int actualsize = 1;
while (actualsize < size) actualsize*=2;
// Make sure the grid size is a perfect power of 2.
grid = new int[actualsize][actualsize];
currentNum = 1;
// Fill in the grid with all empty squares.
for (int i=0; i<actualsize; i++) {
for (int j=0; j<actualsize; j++) {
grid[i][j] = 0;
}
}
// This represents the original hole in the tromino.
grid[x][y] = -1;
}
// Wrapper call for recursive method.
public void tile() {
tileRec(grid.length, 0, 0);
}
private void tileRec(int size, int topx, int topy) {
// No recursive case needed here, just fill in your one tromino...
if (size == 2) {
// Fill in the one necessary tromino. The hole is identified by a
// non-zero number, so don't fill in that one square.
for (int i=0; i<size; i++)
for (int j=0; j<size; j++)
if (grid[topx+i][topy+j] == 0)
grid[topx+i][topy+j] = currentNum;
// Advance to the next tromino.
currentNum++;
}
// Recursive case...
else {
// Find coordinates of missing hole
int savex=topx, savey=topy;
for (int x=topx; x<topx+size; x++)
for (int y=topy; y<topy+size; y++)
if (grid[x][y] != 0) {
savex = x;
savey = y;
}
// Hole in upper left quadrant.
if (savex < topx + size/2 && savey < topy + size/2) {
// Recursively tile upper left quadrant.
tileRec(size/2, topx, topy);
// Fill in middle tromino
grid[topx+size/2][topy+size/2-1] = currentNum;
grid[topx+size/2][topy+size/2] = currentNum;
grid[topx+size/2-1][topy+size/2] = currentNum;
// Advance to the next tromino
currentNum++;
// Now we can make our three other recursive calls.
tileRec(size/2, topx, topy+size/2);
tileRec(size/2, topx+size/2, topy);
tileRec(size/2, topx+size/2, topy+size/2);
}
// Hole in upper right quadrant
else if (savex < topx + size/2 && savey >= topy + size/2) {
// Recursively tile upper right quadrant.
tileRec(size/2, topx, topy+size/2);
// Fill in middle tromino
grid[topx+size/2][topy+size/2-1] = currentNum;
grid[topx+size/2][topy+size/2] = currentNum;
grid[topx+size/2-1][topy+size/2-1] = currentNum;
// Advance to the next tromino
currentNum++;
// Now we can make our three other recursive calls.
tileRec(size/2, topx, topy);
tileRec(size/2, topx+size/2, topy);
tileRec(size/2, topx+size/2, topy+size/2);
}
// Hole in bottom left quadrant
else if (savex >= topx + size/2 && savey < topy + size/2) {
// Recursively tile bottom left quadrant.
tileRec(size/2, topx+size/2, topy);
// Fill in middle tromino
grid[topx+size/2-1][topy+size/2] = currentNum;
grid[topx+size/2][topy+size/2] = currentNum;
grid[topx+size/2-1][topy+size/2-1] = currentNum;
// Advance to the next tromino
currentNum++;
// Now we can make our three other recursive calls.
tileRec(size/2, topx, topy);
tileRec(size/2, topx, topy+size/2);
tileRec(size/2, topx+size/2, topy+size/2);
}
else {
// Recursively tile bottom right quadrant.
tileRec(size/2, topx+size/2, topy+size/2);
// Fill in middle tromino
grid[topx+size/2-1][topy+size/2] = currentNum;
grid[topx+size/2][topy+size/2-1] = currentNum;
grid[topx+size/2-1][topy+size/2-1] = currentNum;
// Advance to the next tromino
currentNum++;
// Now we can make our three other recursive calls.
tileRec(size/2, topx+size/2, topy);
tileRec(size/2, topx, topy+size/2);
tileRec(size/2, topx, topy);
}
} // end large if-else
} // end tileRec
// Prints out the current object.
public void print() {
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[i].length; j++)
System.out.print(grid[i][j] + "\t");
System.out.println();
}
}
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
// Get user input...
System.out.println("How big do you want your Tromino grid?");
System.out.println("Please enter a perfect power of 2.");
int size = stdin.nextInt();
System.out.println("Where do you want the hole?");
System.out.println("Answer with an x and y coordinate separated by spaces.");
int x = stdin.nextInt();
int y = stdin.nextInt();
// Create our object and tile it!
tromino thisguy = new tromino(size, x, y);
thisguy.tile();
// Print out the trominoed grid.
System.out.println("Here's your final grid:\n");
thisguy.print();
}
}
答案 0 :(得分:0)
不要让print()
实际打印,而是让它返回一个字符串
public String print() {
String printString = "";
for (int i=0; i<grid.length; i++) {
for (int j=0; j<grid[i].length; j++)
printString += grid[i][j] + "\t");
}
printString += "\n";
// I moved the nextline. I figured this may what you want instead
}
}
然后在对话框中显示消息
public static void main(String[] args){
tromino thisguy = new tromino(size, x, y);
// this will print the result in a popup box
JOptionPane.showMessageDialog(thisguy.print(), "This is your final grid");
// You need a String as the first argument that's why I changed the
// return type of print() to String instead of void
// and don't print but instead create a string
}
另外,请考虑Java命名约定。班级名称以大写字母开头。
答案 1 :(得分:0)
虽然通过对话框显示程序是一个简单的过程,但使用Java内置的Swing框架显示它会更令人印象深刻(并且不会太难)。
我不熟悉Tromino难题,但是如果你想要显示一个元素网格,那么使用Java Swing框架这是一个相当简单的任务。您可以创建一个扩展JPanel的类,并将其添加到JFrame。一些可能有用的教程:
希望这有帮助!