Skip to content
Advertisement

How do I print a shape in a loop? [closed]

I am trying to print shapes in a loop, but when I run the program nothing shows up. I am using shapes from a custom class I made earlier. I use mouse clicks to get each end of the mouse (in a different section) and that is working.

@Override 
public void paintComponent(Graphics g){
    super.paintComponent(g);
    for (int i = 0; i<howMany;i++){
        if (shapes[i] instanceof Line){ 
            Line l = (Line) shapes[i];
            g.drawLine((int)l.start.getX(),(int) l.start.getY(),(int) l.end.getX(),(int) l.end.getY());
            repaint();
        }
    }
}

Advertisement

Answer

Remove repaint request and double check that shapes is not empty and shapes[i] is actually a Line

public void paintComponent(Graphics g){
    super.paintComponent(g);
    for (int i = 0; i<howMany;i++){
        if (shapes[i] instanceof Line){ 
            Line l = (Line) shapes[i];
            g.drawLine((int)l.start.getX(),(int) l.start.getY(),(int) l.end.getX(),(int) l.end.getY());
           // repaint(); remove that 
        }
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement