Search code examples
javaswingdrawing2d

Line form corner to corner


I got a little problem, I think I made a mistake. Can you help me please? Question: I want to use specifix integers for drawing line and filling rectangle. I tried to do it, but line, using same coordinates as rectangle, is not from corner to another corner.

void draw(Graphics g) {

    int x = getWidth();//1920
    int y = getHeight();//1080
    int x0 = 10; int y0 = 10;


    g.setColor(Color.RED);
    g.fillRect(x0, y0, x0 + 1300, y0 + 800);    


    g.setColor(Color.CYAN);
    g.drawLine(x0, y0, x0 + 1300, y0 + 800);


}

image how it looks:

https://i.sstatic.net/MD9Rq.jpg Thank you!


Solution

  • I tried to do it, but line, using same coordinates as rectangle

    But both method don't use coordinates:

    g.fillRect(x0, y0, x0 + 1300, y0 + 800);    
    

    The fillRect draws a rectangle from a starting point using a width and height.

    g.drawLine(x0, y0, x0 + 1300, y0 + 800);
    

    The drawLine draws a line between two points.

    When drawing the rectangle the code should be:

    //g.fillRect(x0, y0, x0 + 1300, y0 + 800);    
    g.fillRect(x0, y0, 1300, 800);