java graphics创建一个N by N板

时间:2015-04-19 21:40:11

标签: java graphics

我有这个问题我在作业中没有解决,“cmps200:java编程简介”。这是我写的,但它给出了一个空白面板:

import java.awt.*;
public class Board {
    public static void main(String[] args){
         int N= Integer.parseInt(args[0]);
        int x=0;
        int y=0;
        int m=(int)(300/5);
        DrawingPanel panel= new DrawingPanel(300, 300);
        Graphics g= panel.getGraphics();
        for (int i=1; i<=N; i++){
            for (int j=1; j<=N; j++){
                square();
                g.setColor(Color.RED);
                circle();
                g.setColor(Color.BLUE);
                circle();
                y+=m;
            }
        }
    }
    public static void square(){
        int N;
        int m= (int)(300/5);
        int x=0; int y=0;
        DrawingPanel panel= new DrawingPanel(300, 300);
        Graphics g= panel.getGraphics();
        g.setColor(Color.BLACK);
        g.drawRect(x, y, N/300, N/300);
    }
    public static void circle(){
        int x; int y; int m;
        Graphics g= panel.getGraphics();
        g.fillOval(x+y+(3/2)*m);
    }
}

1 个答案:

答案 0 :(得分:0)

正确传递参数:

public class Board {
    public static void main(String[] args){
        ...
        for (int i=1; i<=N; i++){
            for (int j=1; j<=N; j++){
                square(N);
                g.setColor(Color.RED);
                circle(x, y);
                g.setColor(Color.BLUE);
                circle(x, y);
                y+=m;
            }
        }
    }
    public static void square(int N){
        //deleted first line
        int m= (int)(300/5);
        int x=0; int y=0;
        DrawingPanel panel= new DrawingPanel(300, 300);
        Graphics g= panel.getGraphics();
        g.setColor(Color.BLACK);
        g.drawRect(x, y, N/300, N/300);
    }
    public static void circle(int x, int y){
        //deleted first line
        Graphics g= panel.getGraphics();
        g.fillOval(x+y+(3/2)*m);
    }
}