我有一个问题没有解决,在我的作业"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);
}
}
正确传递参数:
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);
}
}