0

我编写了一个 Tromino 拼图程序,它用 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();

        }
    } 
4

2 回答 2

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 命名约定。类名以大写字母开头。

于 2013-11-07T05:44:36.483 回答
0

尽管通过对话框显示您的程序是一个简单的过程,但使用 Java 的内置 Swing 框架显示它会更令人印象深刻(而且难度并不大)。

我不熟悉 Tromino 拼图,但是如果您有一个想要显示的元素网格,那么使用 Java Swing 框架来完成是一项相当简单的任务。您可以创建一个扩展JPanel的类并将其添加到JFrame。一些可能有帮助的教程:

希望这可以帮助!

于 2013-11-07T05:53:55.983 回答