java - 变量没有做它应该做的事情?
问题描述
我正在从我的导师分配的 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);
}
}
}
解决方案
在你的两行
square(g , x+size*i, y, 25, Color.BLACK, true);
和
square(g , x+size*i, y, 25, Color.WHITE, false);
您作为被调用25
的值或参数传递。所以你所有的方块都是 size 。也许您想将变量作为此参数的值传递,而不是 。喜欢square
size
25
25
size
square(g , x+size*i, y, size, Color.BLACK, true);
推荐阅读
- python - 获取 None.org.apache.spark.sql.execution.python.UserDefinedPythonFunction
- python - 将单元格值从 xlsx 导入到所有行的 python 变量
- mysql - 是否可以仅将 SMT(单消息转换)应用于来自指定主题的消息
- django - 我如何将模型字段与属于他的特定信息联系起来?
- c++ - 使用 FreeBSD 作为远程主机的 CLion 完全远程模式
- c# - 如何使用 Roslyn 在 C# 中忽略 Property Getter 和 Setter
- python - 无法添加到 lambda Python 3.6
- python - 我想预测新事件的更新次数?如何在python中做到这一点?
- typescript - 与 Strophe.js 断开连接
- python - 如何通过 odoo 10 中的 cron 作业计算天数?