Witam. Chciałbym aby przed odświeżeniem ekranu każdy działający wątek rysowania narysował co ma i dopiero odświeżył. Czytałem że powinienem użyć podwójnego buforowania, lecz niestety nie potrafię tego zaimplementować. Czy mógłby ktoś przerobić mi poniższy kod aby było to możliwe?Z góry dziękuję za pomoc
pozdrawiam
public class Klasa extends JFrame {
public Pane p;;
public Klasa(){
setSize(200,200);
p= new Pane();
add(BorderLayout.CENTER,p);
dodajP(p,"Dodaj",new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
Runnable r = new W(p);
Thread t = new Thread(r);
t.start();
}
} );
setVisible(true);
Runnable b = new W(p);
Thread t = new Thread(b);
t.start();
}
public void dodajP(Pane k,String title,ActionListener listener){
JButton b = new JButton(title);
b.addActionListener(listener);
k.add(BorderLayout.SOUTH,b);
}
public static void main(String args[])
{
Klasa t = new Klasa();
}
}
class Pane extends JPanel
{
int x =0;
int y=0;
String name;
public Pane()
{
setSize(100,100);
this.setBackground(Color.RED);
}
public void move(int xx,int yy,String g){x=xx;y=yy;name=g;}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawString(name,x,y);
}
class W implements Runnable{
int war = 0;
Pane k;
public W(Pane t)
{
k=t;
}
public void run() {
for(;war<100;war++)
{
Thread t = Thread.currentThread();;
k.move(2*war,war*war,t.getName());
k.repaint();
try {
Thread.sleep(700);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
}
}}