0

2^n サイズのグリッドを作成しようとして、ユーザーに「n」を尋ねています。2^n の部分はコーディングしていませんが、これも少し混乱を招きます。しかし、現在、ユーザーからの入力を受け取ると、ボードが正しく表示されません。私の drawLine は、ボード全体を通る対角線です。

ボードを正しく表示するにはどうすればよいですか?

これが私のコードです:

import java.awt.*;
import java.util.*;
public class DrawingPanelTest2{

   public static void main(String args[]){

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

        //create a drawing panel of width=400px and height=400px
        DrawingPanel panel = new DrawingPanel(400, 400);
        //set the background of the panel to CYAN
        panel.setBackground(Color.LIGHT_GRAY);
        //create a graphic object for the panel 
        Graphics g = panel.getGraphics();

        //draw square
        drawFigure_1(g,0,0);

   }

   public static void drawFigure_1(Graphics g,int x, int y) {
        Scanner stdin = new Scanner(System.in);

        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(); 
        //set your drawing color to red
        g.setColor(Color.BLACK);
        for (int i = 1; i <= size; i++) {
            //draw a rectangle, (x,y) is the top-left cordiante of the rectangle, and ((i*z), (i*z)) 
            //are the width and height of the rectangle
            g.drawRect(x, y, i * size, i * size);
            g.drawLine(x, y, i *size, i *size);
        }
        g.setColor(Color.BLACK);
   }
}
4

2 に答える 2

1

次のようなことを試してください:

import java.util.*;
import java.awt.*;
import javax.swing.*;
class myjava{
    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 myPan(double p){
        pow = p;
    }
    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{
            System.out.println("not");
            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");
        }
    }
}
于 2013-11-01T00:37:35.933 に答える