1

我有我的 tromino 程序。但是,现在需要将其转换为图形(我留到最后)。我对如何做到这一点感到很困惑,我知道一些关于图形的基础知识,但看看我的程序,似乎我可能需要做很多修改才能让它工作。

有没有一种简单的方法可以做到这一点?基本上我需要将我现在作为数字拥有的 trominos 转换为棋盘(其大小由用户指定)。并且缺陷方块是任何颜色,表明它是缺陷方块的位置。这是代码:

    import java.util.*;

    public class tromino {

        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 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();

        }
    }

更新的图形,虽然我仍然对如何完成我的代码感到困惑?

import java.util.*;
import java.awt.*;
import javax.swing.*;
class DrawingPanelTest2{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double pw = input.nextDouble();
        myPan panel = new myPan(pw);      //calling class myPan of JPanel (put stuff inside frame)
        JFrame application = new JFrame();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);           
        application.setSize(400, 400);
        application.setVisible(true); 
    }
}

class myPan extends JPanel{
    public double pow;      //pow variable of type double
    public tri play[];      //constructor intializes tromino
    public background back; //type background variable back
    public myPan(double p){ //method calling p
        pow = p;            //pow = p (method calling pow now)
        back = new background(p);   //back = call to  method background w/var p
        play = new tri[10]; //this is where to create all of the triominoes
                            //example way (rough idea)

    }
    public void paintComponent(Graphics g){  //new method of type void
        super.paintComponent(g);             //refers to parent's method paintCompenent
        back.paintBackground(g);             //call method paintBackground
        for(int i = 0; i < play.length; i++){ //paint all of the triominos
            play[i].paintTri(g);              //paint the trominoes
        }
    }
}

class background{
    public double pow;
    int across;                    //across (row)   
    int up;                        //column    
    public boolean filled[][];
    public background(double p){
        pow = p;
        filled = new boolean[up][across]; //this is if it is filled with a tri
                                          //need to figure out a way to set
                                          //the filled ones to "true"
    }
    public void paintBackground(Graphics g){   //constructor header w/no return type? passes param g
      paintComponent(g);
      }
        public void paintComponent(Graphics g){ // mutator method w/void return type modifying object g internal state
//         super.paintComponent(g);          //refers to parent's method paintCompenent(call var g)
        double num = Math.pow(2,pow);     //exp of 2
        double across;                    //across (row)   
        double up;                        //column
        if(pow % 2 == 0){ //is a square
            across = Math.pow(num,0.5);   
            up = across;
        }
        else{                       //not a square
            double x = Math.floor(pow/2); //largest integer less than or = to exp/2
            double y = x + 1;             
            across = Math.pow(2,x);       //row = 2^(expo)
            up = Math.pow(2,y);           //column = 2^(power of 2)
        }
        System.out.println(across);
        System.out.println(up);
        //
        //
        double wid = 400/across; //width of one
        double hi = 400/up; //height of one
        double nowX = 0;
        double nowY = 0;
        for(int i = 0; i < up; i++){ //top to bottom
            nowX = 0;
            for(int j = 0; j < across; j++){
                g.setColor(Color.BLACK);
                g.drawRect((int)nowX, (int)nowY, (int)wid, (int)hi);
                if(filled[i][j] == true){ //<--how to check this using my "filled" array?
                    g.setColor(Color.GRAY);
                    g.fillRect((int)nowX, (int)nowY, (int)wid, (int)hi);
                }
                else{//the square is not ok
                    g.setColor(Color.RED);
                    g.fillRect((int)nowX, (int)nowY, (int)wid, (int)hi);
                }
                nowX = nowX + wid;
            }
            nowY = nowY + hi;
        }
    }
    }


class tri{
    public tri(int x, int y, int width, int height){

        //initilize variables

//         int x = 0;
//         int y = 0;
//         int width = 0;
//         int height = 0;
    }
    public void paintTri(Graphics g){
        //draw it here
    }
}
4

1 回答 1

1

这是一个基本的大纲。希望这会有所帮助。

import java.util.*;
import java.awt.*;
import javax.swing.*;
class DrawingPanelTest2{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double pw = input.nextDouble();
        myPan panel = new myPan(pw);
        JFrame application = new JFrame();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);           
        application.setSize(400, 400);
        application.setVisible(true); 
    }
}

class myPan extends JPanel{
    public double pow;
    public tri play[];
    public background back;
    public myPan(double p){
        pow = p;
        back = new background(p);
        play = new tri[10]; //this is where you create all of your triominoes
                            //you can do it a different way, but this is an
                            //example
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        back.paintBackground(g);
        for(int i = 0; i < play.length; i++){ //paint all of the triominos
            play[i].paintTri(g);
        }
    }
}

class background{
    public double pow;
    public boolean filled[][];
    public background(double p){
        pow = p;
        filled = new boolean[up][across]; //this is if it is filled with a tri
                                          //you'll need to figure out a way to set
                                          //the filled ones to "true"
    }
    public paintBackground(Graphics g){
        public void paintComponent(Graphics g){
        super.paintComponent(g);
        double num = Math.pow(2,pow);
        double across;
        double up;
        if(pow % 2 == 0){ //is a square
            across = Math.pow(num,0.5);
            up = across;
        }
        else{
            double x = Math.floor(pow/2);
            double y = x + 1;
            across = Math.pow(2,x);
            up = Math.pow(2,y);
        }
        System.out.println(across);
        System.out.println(up);
        //
        //
        double wid = 400/across; //width of one
        double hi = 400/up; //height of one
        double nowX = 0;
        double nowY = 0;
        for(int i = 0; i < up; i++){ //top to bottom
            nowX = 0;
            for(int j = 0; j < across; j++){
                g.setColor(Color.BLACK);
                g.drawRect((int)nowX, (int)nowY, (int)wid, (int)hi);
                if(...the square is OK...){ //<--you'll need a way to check this using the "filled" array
                    g.setColor(Color.WHITE);
                    g.fillRect((int)nowX, (int)nowY, (int)wid, (int)hi);
                }
                else{//the square is not ok
                    g.setColor(Color.RED);
                    g.fillRect((int)nowX, (int)nowY, (int)wid, (int)hi);
                }
                nowX = nowX + wid;
            }
            nowY = nowY + hi;
        }
    }
    }
}

class tri{
    public tri(int x, int y, int width, int height){
        //initialize all of your values
    }
    public paintTri(Graphics g){
        super.paintComponent(g);
        //draw it here
    }
}
于 2013-11-01T11:52:58.273 回答