是一个转盘程序,在转的时候总是要闪烁。。
先上代码,解释在后面,谢谢!!
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,都是先在他上面画图,然后一次性画在窗口里面的。请哪位高人帮忙解决一下,谢谢了
先上代码,解释在后面,谢谢!!
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,都是先在他上面画图,然后一次性画在窗口里面的。请哪位高人帮忙解决一下,谢谢了
解决方案 »
- 怎样识别日文汉字与中文汉字
- 关于JAVA的菜鸟问题,JAVA是否支持GOTO语句?
- JAVA获取POS机(通过COM口)输入而获得内容问题
- 自己做的一个骑士飞行棋的一个小游戏 存在一些小错误 比如棋子踩到地雷不会退格
- session过期自动注销前的判断,请大家帮帮忙呢
- 关于JTable和JScrollPane的问题
- 请问如何使用jmf播放.swf文件
- 关于IBM Aglet的一个问题,希望有人知道
- 请问用java通过pop3协议如何获取信件内容.
- 请教各位大侠:怎样才能判断出JTextField中的数据是日期?
- BigDecimal 输出精度控制
- 最近服务器经常负载很高,导致业务终止,高手帮忙看看c3p0配置是否合理。
帮帮忙,谢谢