是一个转盘程序,在转的时候总是要闪烁。。
先上代码,解释在后面,谢谢!!
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
public class test extends Applet implements Runnable,ActionListener{

private Image img;
private Image[] arr;
private Image background;
private Image offscreen;
private BufferedImage bi;
private MediaTracker mt;
private Thread animation;
private int y;
private int iWheela,iWheelb,iWheelc;
private int iCounta,iCountb,iCountc;
private boolean bStopa,bStopb,bStopc;
private int iResulta,iResultb,iResultc;
private boolean bFirst;
private Random rand;
private Panel panel;
private Button button,up,down;
private TextField t1,t2;
private int score,debt;

public void init()
{
setSize(600,300);
iWheela=iWheelb=iWheelc=0;
iCounta=iCountb=iCountc=7;
bStopa=bStopb=bStopc=false;
bFirst=true;
score=20;
debt=0;
rand=new Random();
bi=new BufferedImage(75,105,BufferedImage.TYPE_INT_RGB);

mt=new MediaTracker(this);
arr=new Image[30];
img=getImage(getCodeBase(),"wheel.gif");
mt.addImage(img, 0);
try{
mt.waitForID(0);
}catch(InterruptedException e){} background=getImage(getCodeBase(),"background.jpg");
mt.addImage(background, 0);
try{
mt.waitForID(0);
}catch(InterruptedException e){}


offscreen=this.getGraphicsConfiguration().createCompatibleVolatileImage(getSize().width, getSize().height);

for(int i=0;i<30;i++)
{
arr[i]=createImage(new FilteredImageSource(img.getSource(),new CropImageFilter(0,y,100,100)));
y+=20;
mt.addImage(arr[i], 0);
try{
mt.waitForAll();
}catch(InterruptedException e){}
}

setLayout(new BorderLayout());
panel=new Panel();
panel.setLayout(new GridLayout(5,1));
button=new Button("开始");
t1=new TextField(""+score,20);
t2=new TextField(""+debt,20);
up=new Button("增加");
down=new Button("减少");
panel.add(button);
panel.add(t1);
panel.add(t2);
panel.add(up);
panel.add(down);
button.addActionListener(this);
up.addActionListener(this);
down.addActionListener(this);
add(panel,BorderLayout.EAST); y=0;


}

public void start()
{
if(animation==null)
{
iWheela=iWheelb=iWheelc=0;
iCounta=iCountb=iCountc=7;
bStopa=bStopb=bStopc=false;
animation=new Thread(this);
}
if(bFirst)
{
bFirst=false;
animation=null;
return;
}

animation.start();

}


public void stop()
{

animation=null; }

public void run()
{

Thread cur=Thread.currentThread();
while(cur==animation)
{
try{
Thread.sleep(20);

}catch(InterruptedException e){
break;
}
if(!bStopa)
iWheela++;
if(!bStopb)
iWheelb++;
if(!bStopc)
iWheelc++;
if(bStopa && bStopb && bStopc)
{
calculate();
stop();
}
update();
}


}

public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)offscreen.getGraphics();

g2d.drawImage(background,0,0,this);
((Graphics2D)bi.getGraphics()).drawImage(arr[iWheela%30],0,0,bi.getWidth(this),bi.getHeight(this),this);
g2d.drawImage(bi, null, 50, 90);
((Graphics2D)bi.getGraphics()).drawImage(arr[iWheelb%30],0,0,bi.getWidth(this),bi.getHeight(this),this);
g2d.drawImage(bi, null, 140, 90);
((Graphics2D)bi.getGraphics()).drawImage(arr[iWheelc%30],0,0,bi.getWidth(this),bi.getHeight(this),this);
g2d.drawImage(bi, null, 230, 90); g2d.dispose();

g.drawImage(offscreen,0,0,this);
}

public void update()
{
if(!bStopa)
{
if((iWheela%5)==0)
{
if(rand.nextBoolean())
{
iCounta--;
if(iCounta==0)
{
iResulta=iWheela%30;
bStopa=true;
}

}
}
}

if(!bStopb)
{
if((iWheelb%5)==0)
{
if(rand.nextBoolean())
{
iCountb--;
if(iCountb==0)
{
iResultb=iWheelb%30;
bStopb=true;
}
}
}
}

if(!bStopc)
{
if((iWheelc%5)==0)
{
if(rand.nextBoolean())
{
iCountc--;
if(iCountc==0)
{
iResultc=iWheelc%30;
bStopc=true;
}
}
}
}


if(offscreen==null || offscreen.getWidth(null)!=getSize().width ||
   offscreen.getHeight(null)!=getSize().height)
{
offscreen=createImage(getSize().width,getSize().height);
}
repaint();
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button)
{
start();
}
if(e.getSource()==up)
{
if(score>0 && debt<10)
{
score--;
debt++;
t1.setText(""+score);
t2.setText(""+debt);
}
}
if(e.getSource()==down)
{
if(debt>0)
{
score++;
debt--;
t1.setText(""+score);
t2.setText(""+debt);
}

}
}

public void calculate()
{
if(iResulta==iResultb && iResulta==iResultc)
{
score+=debt*3;
debt=0;
t1.setText(""+score);
t2.setText(""+debt);

}
else if(iResulta==iResultb || iResulta==iResultc || iResultb==iResultc)
{
score+=debt*2;
debt=0;
t1.setText(""+score);
t2.setText(""+debt);
}
else
{
debt=0;
t1.setText(""+score);
t2.setText(""+debt);
}

}

}
看过图形编程的书,说要避免闪烁可以使用后背缓冲,可是在这个APPLET里面我使用了啊,就是那个offscreen的IMAGE,都是先在他上面画图,然后一次性画在窗口里面的。请哪位高人帮忙解决一下,谢谢了