使用drawPanel创建网格时遇到问题

时间:2013-10-31 23:25:18

标签: java graphics grid user-input

尝试制作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);
   }
}

2 个答案:

答案 0 :(得分:2)

Graphics g = panel.getGraphics();不是自定义绘画的完成方式。

Scanner stdin = new Scanner(System.in);不是您应该如何在GUI上下文中与用户进行交互

首先看看Creating a GUI with SwingPerforming Custom Painting

查看Graphics Java Docs

Graphics#drawRect需要4个参数,xy位置(左上角)以及rectenagle的widthheight,而{Graphics#drawLine 1}}取x1y1,这是起点,x2y2是终点。

所以你想绘制一条水平线,你需要使用更像g.drawLine(x, y, i * size, i);的东西或垂直线,更像是g.drawLine(x, y, i, i * size);

如果您正在尝试绘制网格,那么您将需要循环,一个水平和一个垂直。您还需要更新每个矩形的x / y,以便更正它们,因此您应该修改位置参数,而不是修改尺寸参数

答案 1 :(得分: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");
        }
    }
}
相关问题