title: Drawing rectangle and grid
body: <p>I would be very thankful if someone could help me.</p>
  
  <p>My program has 2 classes. One draws a rectangle and a grid, the other is a viewer.
  The problem is, when I try to change the position for the greed+rectangle, i.e. update X and Y constants in the Viewer class, the proportions change. The grid  fits in only when X=0 and Y=0.</p>
  
  <p>I'm using the same arrays of coordinates to draw the grid and the rectangle, but somehow the proportions are wrong, which surprises me.</p>
  
  <p>Thank you.</p>
  
  <p>My code:</p>
  
  <pre><code>import java.applet.Applet;
  import java.awt.Graphics;
  
  import java.awt.Color;
  import java.awt.Graphics2D;
  import java.awt.geom.Line2D;
  import java.awt.geom.Rectangle2D;
  import java.math.BigDecimal;
  
  
  public class Viewer extends Applet {
    public static final double R_WDTH = 12;//rectangle width
    public static final double X = 10;//the grid fits in only when X=Y=0
    public static final double Y = 10;
    public static final int ROWS = 25;
    public static final int COLUMNS = 12;
  
    RectangleAndGrid cup = new RectangleAndGrid(X, Y, R_WDTH, ROWS, COLUMNS);
  
    public void paint(Graphics g) {
      cup.draw((Graphics2D)g);
    }
  }
  
  
  public class RectangleAndGrid {
    private double x, y, blockWidth;
    private int rows, columns;
    private double coordX[][], coordY[][];
    private Rectangle2D.Double cupSpace;
  
    public RectangleAndGrid(double cX, double cY, double givenBlockWidth, int r, int c){
      x = cX;
      y = cY;
      blockWidth = givenBlockWidth;
      rows = r;
      columns = c;
  
      BigDecimal currX = BigDecimal.valueOf(x);
      BigDecimal currY = BigDecimal.valueOf(y);
      coordX = new double[rows + 1][columns + 1];
      coordY = new double[rows + 1][columns + 1];
      for(int i = 0; i &lt;= rows; i++){
        for(int j = 0; j &lt;= columns; j++){
          coordX[i][j] = currX.doubleValue();
          coordY[i][j] = currY.doubleValue();
          currX = currX.add(BigDecimal.valueOf(blockWidth));
        }
        currX = BigDecimal.valueOf(x);
        currY = currY.add(BigDecimal.valueOf(blockWidth));
      }
  
      cupSpace = new Rectangle2D.Double(coordX[0][0], coordY[0][0], coordX[rows][columns], coordY[rows][columns]);
    }
  
    public void draw(Graphics2D g2d){
      g2d.setColor(Color.CYAN);
      g2d.fill(cupSpace);
      g2d.setColor(Color.BLACK);
      g2d.draw(cupSpace);
  
      Line2D.Double line = new Line2D.Double();
      for(int i = 0; i &lt;= rows; i++){
        line.setLine(coordX[i][0], coordY[i][0], coordX[i][columns], coordY[i][columns]);
        g2d.draw(line);
      }
      for(int i = 0; i &lt;= columns; i++){
        line.setLine(coordX[0][i], coordY[0][i], coordX[rows][i], coordY[rows][i]);
        g2d.draw(line);
      }
    }
  }
  </code></pre>