首页 > 解决方案 > 变量没有做它应该做的事情?

问题描述

我正在从我的导师分配的 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);
      }
   }
}                

标签: java

解决方案


在你的两行

square(g , x+size*i, y, 25, Color.BLACK, true);

square(g , x+size*i, y, 25, Color.WHITE, false);

您作为被调用25的值或参数传递。所以你所有的方块都是 size 。也许您想将变量作为此参数的值传递,而不是 。喜欢squaresize2525size

square(g , x+size*i, y, size, Color.BLACK, true);

推荐阅读