alessandroAmedei
alessandroAmedei

Reputation: 63

Strange repositioning using repaint();

I have a JSlider in a JPanel that return me a value of R-G-B . I create it, in the Costructor of JPanel. I draw in same Panel (using paintComponent) a little circle, and I change his color using the Slider. I want that the color change in contemporany of slider shift. So, i use the method repaint.. Next to Panel there is another Panel, with two button.. If I use method repaint in first panel , the buttons of second panel duplicated in the topLeft of First Panel. Why? Thank's you.

First Panel:

public class OptionsPanel extends JPanel {


static JSlider RBG = new JSlider(0,255);
OptionsPanel(){

this.setVisible(false);
this.setSize(350,1000);
this.setLayout(null);
this.setBackground(new Color(200,200,0));
Main.f1.add(this); 



 RBG.setVisible(true);
 RBG.setSize(255,50);
 RBG.setLocation(30,240);


 this.add(RBG);



LotL lotl = new LotL();
Button save = new Button("Save");

save.setVisible(true);
save.setSize(100,40);
save.setLayout(null);
save.setLocation(60,300);
save.addActionListener(lotl); 
save.setBackground(Color.yellow);
save.identificatore=3;
this.add(save);







  }


  boolean draw=false;

  @Override
 public void paintComponent(Graphics g){



 g.drawOval(50,100,70,70);

 g.setColor(new Color(RBG.getValue(),180,200));
 g.fillOval(50,100,70,70);


 repaint();




}
}

Second Panel:

public class FirstPanel extends JPanel{
FirstPanel(){

this.setVisible(true);
this.setSize(1000,1000);
this.setLayout(null);
this.setBackground(new Color(255,200,180)); 
Main.f1.add(this);

Button start = new Button("Start Game!");


Button options = new Button("Options");
LotL LotL = new LotL();  



start.setVisible(true);
start.setSize(200,80);
start.setLayout(null);
start.setLocation(400,450);
start.addActionListener(LotL); 
start.setBackground(Color.green);
start.identificatore=1;
this.add(start);

options.setVisible(true);
options.setSize(200,70);
options.setLayout(null);
options.setLocation(400,550);
options.addActionListener(LotL); 
options.setBackground(Color.green);
options.identificatore=2;
this.add(options);

}


}

Upvotes: 0

Views: 64

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

You've broken the paint chain...

@Override
public void paintComponent(Graphics g){

    g.drawOval(50,100,70,70);

    g.setColor(new Color(RBG.getValue(),180,200));
    g.fillOval(50,100,70,70);


    repaint();

}

Graphics is a shared resource, which gets passed to ALL the components that are painted during a given paint cycle.

One of the jobs of paintComponent is to prepare the Graphics context for painting, but filling with the components background color.

You MUST call super.paintComponent before performing any custom painting.

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawOval(50,100,70,70);

    g.setColor(new Color(RBG.getValue(),180,200));
    g.fillOval(50,100,70,70);
}

Also, there is never any need for paintComponent to be public, no one should ever be calling directly and NEVER modify the state of a component from within any paint method which may trigger a repaint, you will get yourself into a infinite loop which will eventually consume your CPU and make you computer unusable.

Take a look at Painting in AWT and Swing and Performing Custom Painting for more details

You should also avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Upvotes: 2

Related Questions