Drawing a line between two points

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

+3
source share
3 answers

In swing:

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

IF you draw JPanel, you usually put this code in a method paintComponent:

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

To view all available methods in a class Graphics, see Javadocs .

+5
source

Take a look at Graphics.drawLine .

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

graphics.drawLine( p1.x, p1.y, p2.x, p2.y );
+2
source

For a JFrame, you must add a drawing method that starts when the JVM is ready to draw the JFrame inside the class that inherits the JFrame class. Then, inside this, you would call the drawing method “drawLine” as shown (make sure the “Graphics” class has been imported and replaced X1, Y1, X2, Y2 with integers of your choice.):

public void paint(Graphics g) {
    g.drawLine(X1,X2,Y1,Y2);
}
0
source

Source: https://habr.com/ru/post/1775705/


All Articles