我正在由导师分配的CSE 142进行作业,我试图复制代码,但是size变量并未更改程序绘制的正方形的大小,而是将每个正方形之间的距离更改为另一个广场。我不知道从哪里开始寻找解决此问题的方法,所以也许你们可以为您提供一些帮助
import java.awt.*;
public class CafeWall{
public static final int WIDTH = 800;
public static final int HEIGHT = 450;
public static void main (String[] args){
DrawingPanel panel = new DrawingPanel(WIDTH, HEIGHT);
Graphics g = panel.getGraphics();
panel.setBackground(Color.LIGHT_GRAY);
grid(g, 250, 200, 6, 25, 6, 10, Color.WHITE, Color.BLACK);
grid(g, 10, 150, 8, 25, 8, 0, Color.BLACK, Color.WHITE);
grid(g, 425, 180, 9, 20, 10, 10, Color.BLACK, Color.WHITE);
grid(g, 400, 20, 4, 35, 4, 35, Color.BLACK, Color.WHITE);
singleRow(g, 0, 0, 8, Color.BLACK, Color.WHITE, 25);
singleRow(g, 50, 70, 10, Color.BLACK, Color.WHITE, 25);
}
public static void grid(Graphics g, int x, int y, int row, int size,
int column, int offset,Color color1, Color color2){
for(int i = 0; i < row;i++){
int offsetTemp = 0;
offsetTemp = offset;
singleRow(g, x + offsetTemp, y+size*i, column, color1, color2,
size);
if(i % 2 != 0){
offsetTemp = offset;
singleRow(g, x, y+size*i, column, color1, color2, size);
}
}
}
public static void singleRow(Graphics g, int x, int y, int num, Color
color1, Color color2, int size){
for(int i = 0; i < num ; i++){
if (i % 2 == 0){
square(g , x+size*i, y, 25, Color.BLACK, true);
} else {
square(g , x+size*i, y, 25, Color.WHITE, false);
}
}
}
public static void square(Graphics g, int x, int y, int size, Color
color, boolean diagonals){
g.setColor(color);
g.fillRect(x, y, size, size);
if (diagonals){
g.setColor(Color.RED);
g.drawLine(x,y,x+size,y+size);
g.drawLine(x+size,y,x,y+size);
}
}
}
答案 0 :(得分:2)
在您的两行中
square(g , x+size*i, y, 25, Color.BLACK, true);
和
square(g , x+size*i, y, 25, Color.WHITE, false);
您正在传递25
作为名为square
的{{1}}的值或参数。因此,所有正方形的大小将为size
。也许您想传递变量25
作为此参数的值,而不是25
。像
size