-4

I am trying to draw rectangles in Java like this picture:

enter image description here

I visualize the coordinates as I do at math but I come up with the rectangles turned upside down which is like this:

enter image description here

I know Im missing just a few things.What should I do? (Colors will be edited)

 public class BlockTower
{
public static void main(String[] args)
{

    Rectangle rect1 = new Rectangle(20, 70, 40, 30);
    rect1.draw();
    rect1.setColor(Color.BLUE);
    rect1.fill();

    Rectangle rect2 = new Rectangle(60, 70, 40, 30);
    rect2.draw();
    rect2.setColor(Color.MAGENTA);
    rect2.fill();

    Rectangle rect3 = new Rectangle(100, 70, 40, 30);
    rect3.draw();
    rect3.setColor(Color.CYAN);
    rect3.fill();

    Rectangle rect4 = new Rectangle(40, 100, 40, 30);
    rect4.draw();
    rect4.setColor(Color.RED);
    rect4.fill();

    Rectangle rect5 = new Rectangle(80, 100, 40, 30);
    rect5.draw();
    rect5.setColor(Color.PINK);
    rect5.fill();

    Rectangle rect6 = new Rectangle(60, 130, 40, 30);
    rect6.draw();
    rect6.setColor(Color.BLUE);
    rect6.fill();



    //TODO finish the draft to display the six blocks
  }
}
deadfish
  • 11,996
  • 12
  • 87
  • 136
SametSahin
  • 571
  • 3
  • 7
  • 24

1 Answers1

1

Coordinates in Swing start from Top Left. That means you have to recalculate your y-coordinates. So the bottom of your panel is actually at the current height.

enter image description here

If you've calculated something to be at coordinates (x,y) it now has to be at coordinates (x, height - y) instead.

QBrute
  • 4,405
  • 6
  • 34
  • 40
  • Now I get it thanks.I need to ask something.Whats wrong with my question? I am not comfortable asking questions here.When I post a new question people starts complaining and downvoting. – SametSahin May 03 '17 at 22:03
  • 1
    @S.Sahin Part of the problem with this question in particular, is the fact that the documentation and available tutorials would have answered your question, or at least provided you with the enough information to ascertain the cause of your problem. There's also little or no information about the APIs you are using, it's impossible from the code to determine if you're using a third party API, Swing, AWT, GWT or some other framework – MadProgrammer May 03 '17 at 22:53