Search code examples
javadrawingawt

drawing a line between two points


Hi I have 2 points (X1,Y1) and (X2,Y2) how can I draw a line between them? thanks


Solution

  • In Swing:

    Graphics g;
    g.drawLine(X1, Y1, X2, Y2);
    

    IF you are drawing on a JPanel, you will usually put this code in the paintComponent method:

    @Override
    protected void paintComponent(Graphics g) {
        g.drawLine(X1, Y1, X2, Y2);
    }
    

    To see all available methods on the Graphics class, see the Javadocs.