Drawing multiple lines with Java Swing
Posted
by rize
on Stack Overflow
See other posts from Stack Overflow
or by rize
Published on 2010-03-27T23:39:47Z
Indexed on
2010/03/27
23:43 UTC
Read the original article
Hit count: 251
I'm learning drawing lines with Java Swing in order to draw a labyrinth. I can draw one line at a specified position and it shows just fine. But when I want to draw multiple lines, only the last one shows. My code:
public class LabyrinthGUI extends JFrame {
...
Line line1 = new Line(0, 0, 0, 50);
this.getContentPane().add(line1);
}
public class Line extends JPanel{
private int x1, y1, x2, y2;
public Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void paintComponent (Graphics g) {
g.drawLine(x1, y1, x2, y2);
}
I probably have to refresh something, to display all the lines drawn with for-loop, but don't know what.
© Stack Overflow or respective owner