I found similar problems on the internet but the solutions provided didn’t work.
I want to clear the JPanel. To do that I call repaint()(from the clear() method) with a flag set to false to avoid calling my drawing method (drawLines()). Drawn lines are still on the Panel.
I tried to repaint same lines again with the background color. This also didn’t work.
public class WektPanel extends JPanel{
boolean check = false;
Color c = Color.BLUE;
boolean oval = false;
public WektPanel() {
setBackground(c);
setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(check)
drawLines(3, g);
}
void clear(){
check=false;
repaint();
}
void draw(){
check=true;
repaint();
}
void drawLines(int stroke, Graphics g){
g.drawLine(0,0,getWidth(),getHeight());
g.drawLine(0,getHeight(),getWidth(),0);
for(int i=1; i<stroke;i++){
g.drawLine(0+i,0,getWidth(),getHeight()-i);
g.drawLine(0,0+i,getWidth()-i,getHeight());
g.drawLine(0,getHeight()-i,getWidth()-i,0);
g.drawLine(0+i,getHeight(),getWidth(),0+i);
}
}
}
You’re overriding
paint(), but callingsuper.paintComponent(g). You should overridepaintComponent(g).Edit: I couldn’t follow your
checklogic. This worked for me.