Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing a line between two points

Tags:

java

awt

drawing

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

like image 884
user472221 Avatar asked Jan 22 '23 01:01

user472221


2 Answers

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.

like image 155
jjnguy Avatar answered Jan 23 '23 16:01

jjnguy


Take a look at the Graphics.drawLine method.

You'll basically need to override some widget (like JPanel) or get a Canvas and in the paint method you do something like:

graphics.drawLine( p1.x, p1.y, p2.x, p2.y );
like image 20
javamonkey79 Avatar answered Jan 23 '23 16:01

javamonkey79