0

我有一个正方形的 GUI 网格,我提示用户输入 (2^n) 的大小。然后我要求坐标在网格中放置一个白色正方形或“洞”。我无法将用户输入的“洞”放入我的网格中。

正如您在我的代码(在 main 方法中)中看到的那样,有一次尝试,但它不起作用。知道如何将这个“洞”放入我的网格中吗?

*截至目前,我不断收到此错误:方法声明无效;需要返回类型...... *但是当我的方法 myPan 以完全相同的方式工作且没有错误时,为什么我会得到这个?

  import java.util.*;
import java.awt.*;
import javax.swing.*;
class trominoWarZ{
        private int currentNum;
        private int[][] grid;
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("What size board do you want?");
        double pw = input.nextDouble();

        System.out.println("Coordinates of missing square?");
        int x = input.nextInt();
        int y = input.nextInt(); 

        myHole hole = new myHole(x,y);     //WANT TO SET x,y coord to WHITE 

        super.drawHole(g,x,y);      

        myPan panel = new myPan(pw);      //this is how it reads size of board

        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 double hc;

    public static void drawHole(Graphics g, int x, int y) {    
    g.setColor(Color.WHITE);
    g.fillRect(g,x,y);

    }
    public myPan(double p){
        pow = p;
    }

    public myHole(int h){
        hc = h;                     //I will have the var hc to use for the hole
        drawHole(hc);
    }    
    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
//             System.out.println("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++){
                //System.out.print("*");
                g.setColor(Color.BLACK);
                g.drawRect((int)nowX, (int)nowY, (int)wid, (int)hi);
                nowX = nowX + wid;
            }
            nowY = nowY + hi;
            //System.out.print("\n");
        }

        //grid or whatever = g.fill
    }
    }



//     public static void tromino(int size, int x, int y) {
//             int actualsize = 1;
//             while (actualsize < size) actualsize*=2;
// }
4

1 回答 1

0

你的第一个问题是

g.setColor(Color.WHITE);
g.fillRect(x,y);

您的 g.fillRect(x,y) 无法处理给定的 val 您类型。

于 2013-11-10T08:52:58.390 回答