title: Drawing a square by dragging the mouse
body: <p>I am writing a Java program that closely mimics Microsoft Paint. It can draw four different shapes: Lines, Ovals, Rectangles, and Squares. I am very close to finishing this but I am stuck on the logic for drawing squares.</p>
  
  <p>There are two Points involved while drawing these shapes. The first Point(point1) is when the user presses the mouse button and the second Point(point2) is while the user drags the mouse across the canvas. I believe drawing Rectangles and Squares should be quite similar but the part that has confused me is when drawing a square the sides are equal length so point2 isn't exactly where the mouse is.</p>
  
  <p>Here is the fillRect() method header for reference:</p>
  
  <pre><code>fillRect(x, y, width, height)
  </code></pre>
  
  <p>My functioning code for drawing rectangles is as follows:</p>
  
  <pre><code>g.fillRect((((point1.x &lt; point2.x) ? point1.x : point2.x)),
              ((point1.y &lt; point2.y) ? point1.y : point2.y), 
              Math.abs(point2.x - point1.x), 
              Math.abs(point2.y - point1.y));
  </code></pre>
  
  <p>I tried using the same code for drawing squares except changing the height parameter to be equal to the width parameter because squares have equal length sides:</p>
  
  <pre><code>g.fillRect((((point1.x &lt; point2.x) ? point1.x : point2.x)), 
              ((point1.y &lt; point2.y) ? point1.y : point2.y), 
              Math.abs(point2.x - point1.x), 
              Math.abs(point2.x - point1.x)); //same as width
  </code></pre>
  
  <p>I don't know what the problem is with making both width and height equal. It works when drawing downwards to the left or right but of course the shape does not expand if you pull straight down. Clicking and dragging up does not work; the square simply moves up with the mouse along the Y axis instead of expanding.</p>
  
  <p>Could anyone point me in the right direction regarding the logic for drawing square from two points?</p>
  
  <p>I can explain my code a bit better if need be.</p>