0

I am writing a 'sketchPad' type program in Java. My FREEHAND and LINE implementation works fine, but when I do a RECT or an OVAL if I draw from top left to bottom right it works fine, but the opposite way doesn't function. It will draw from my starting point then down to the bottom right again depending on the size I try to draw. I have learned a little bit about Affine Transform and thought that may be a way to go, but I need help implementing it properly, or doing it a better way.

Here is the effected portion of the code:

public void paint(){      
      addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
                  oldX = e.getX();
                  oldY = e.getY();
                  graphics2D.setPaint(toolBar.getCurrentColor());
                  repaint();
          }
      });



      addMouseListener(new MouseAdapter() {
          public void mouseReleased(MouseEvent e) {
              String currentPaintType = toolBar.getPaintType();
              if(currentPaintType != "FREEHAND"){
                  currentX = e.getX();
                  currentY = e.getY();
                  if(currentPaintType == "LINE"){
                      if (graphics2D != null){
                          graphics2D.drawLine(oldX, oldY, currentX, currentY);
                      }
                  }else if(currentPaintType == "OVAL"){
                      if (graphics2D != null){
                          graphics2D.drawOval(oldX, oldY,       getWidthHeight(oldX,currentX), getWidthHeight(oldY,currentY));
                      }
                  }else if(currentPaintType == "RECT"){
                      if (graphics2D != null){
                          graphics2D.drawRect(oldX, oldY, getWidthHeight(oldX,currentX), getWidthHeight(oldY,currentY));
                      }
                  }
                  repaint();
              }
          }
      });
  }

//Code to calculate the proper width and height based on X/Y points

  public int getWidthHeight(int old, int current){
      int i=0;
      if(old>current){
          i=old-current;
      }else{
          i=current-old;
      }

      return i;
  }
link270
  • 15
  • 5
  • 1
    That's the way the API works, what you need to do is calculate the start point so are always drawing down/right, for [example](http://stackoverflow.com/questions/22645172/java-draws-rectangle-one-way-not-both/22645343#22645343) – MadProgrammer May 02 '16 at 23:05
  • 1
    Also, currentPaintType == "LINE" is not how String comparison works, you should be using the String#equals method – MadProgrammer May 02 '16 at 23:09
  • Thank you for the quick answer! Sometimes all you need is for someone to point out the obvious when you miss it. I was able to follow your advice and get it functioning great! Also, Thanks for the tips on using String.equals(). – link270 May 02 '16 at 23:22

0 Answers0