import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ActiveImage extends Applet implements Runnable,ActionListener{
Image iImages[];
Thread aThread;
int iFrame;
int sleeptime;
String graphfile;
int graphcount;
AudioClip au;
Button b1,b2;
public void init() {
int i,j;
iFrame=0;
aThread=null;
sleeptime=Integer.parseInt(getParameter("sleeptime"));
graphfile=getParameter("graphfile");
graphcount=Integer.parseInt(getParameter("graphcount"));
iImages=new Image[graphcount];
String fname=graphfile;
j=fname.indexOf(".");
for(i=0;i<graphcount;i++)
{
fname=fname.substring(0,j-1)+i+fname.substring(j);
iImages[i]=getImage(getDocumentBase(),"image/"+fname);
}
au=getAudioClip(getDocumentBase(),"wav/bb.wav");
au.play();
Panel p1=new Panel();
b1=new Button("START");
b2=new Button("SToP");
p1.add(b1);
p1.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setLayout(new BorderLayout());
add(p1,"South");
}
public void start()
{
if (aThread==null)
{
aThread=new Thread(this);
aThread.start();
b1.setEnabled(false);
}
}
public void stop()
{
if(aThread!=null)
{
aThread.interrupt();
aThread=null;
au.stop();
b2.setEnabled(false);
}
}
public void run()
{
while(true)
{
iFrame++;
iFrame %=(iImages.length);
repaint();
try
{Thread.sleep(sleeptime); }
catch(InterruptedException e)
{break;}
}
}
public void update(Graphics g)
{
g.clearRect(0,0,300,200);
g.drawImage(iImages[iFrame],0,0,this);}
public void actionPerformed(ActionEvent e)
{
if((e.getSource()==b1)&&(aThread==null))
{
aThread=new Thread(this);
aThread.start();
b1.setEnabled(false);
b2.setEnabled(true);
au.loop();
}
if((e.getSource()==b2)&&(aThread!=null))
{
aThread.interrupt();
aThread=null;
b1.setEnabled(true);
b2.setEnabled(false);
au.stop();
}
}}html
<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="000000">
<CENTER>
<APPLET
code = "ActiveImage.class"
width = "500"
height = "300"
>
<param name=sleeptime value=1000>
<param name=graphfile value="STAR00.GIF" >
<param name=graphcount value=6>
</APPLET>
</CENTER>
</BODY>
</HTML>循环显示几个gif的文件 可是显示的时候狂闪 怎样解决?
import java.applet.*;
import java.awt.event.*;
public class ActiveImage extends Applet implements Runnable,ActionListener{
Image iImages[];
Thread aThread;
int iFrame;
int sleeptime;
String graphfile;
int graphcount;
AudioClip au;
Button b1,b2;
public void init() {
int i,j;
iFrame=0;
aThread=null;
sleeptime=Integer.parseInt(getParameter("sleeptime"));
graphfile=getParameter("graphfile");
graphcount=Integer.parseInt(getParameter("graphcount"));
iImages=new Image[graphcount];
String fname=graphfile;
j=fname.indexOf(".");
for(i=0;i<graphcount;i++)
{
fname=fname.substring(0,j-1)+i+fname.substring(j);
iImages[i]=getImage(getDocumentBase(),"image/"+fname);
}
au=getAudioClip(getDocumentBase(),"wav/bb.wav");
au.play();
Panel p1=new Panel();
b1=new Button("START");
b2=new Button("SToP");
p1.add(b1);
p1.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setLayout(new BorderLayout());
add(p1,"South");
}
public void start()
{
if (aThread==null)
{
aThread=new Thread(this);
aThread.start();
b1.setEnabled(false);
}
}
public void stop()
{
if(aThread!=null)
{
aThread.interrupt();
aThread=null;
au.stop();
b2.setEnabled(false);
}
}
public void run()
{
while(true)
{
iFrame++;
iFrame %=(iImages.length);
repaint();
try
{Thread.sleep(sleeptime); }
catch(InterruptedException e)
{break;}
}
}
public void update(Graphics g)
{
g.clearRect(0,0,300,200);
g.drawImage(iImages[iFrame],0,0,this);}
public void actionPerformed(ActionEvent e)
{
if((e.getSource()==b1)&&(aThread==null))
{
aThread=new Thread(this);
aThread.start();
b1.setEnabled(false);
b2.setEnabled(true);
au.loop();
}
if((e.getSource()==b2)&&(aThread!=null))
{
aThread.interrupt();
aThread=null;
b1.setEnabled(true);
b2.setEnabled(false);
au.stop();
}
}}html
<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="000000">
<CENTER>
<APPLET
code = "ActiveImage.class"
width = "500"
height = "300"
>
<param name=sleeptime value=1000>
<param name=graphfile value="STAR00.GIF" >
<param name=graphcount value=6>
</APPLET>
</CENTER>
</BODY>
</HTML>循环显示几个gif的文件 可是显示的时候狂闪 怎样解决?
一旦click "start" button 后在actionPerformed()中又创建一个线程,做同样的事情,而这两个线程没有作同步处理。可能是显示gif狂闪的原因吧,楼主的意思,应该是只有一个worker线程来不停地加载gif,而这里面确有两个worker thread?
这样就不闪了
一个是播放gif的,一个是播放声音的 不知道这两个是不是有关系? lifang276()
要把Thread.sleep(10); //暂停程序执行10秒 加在哪里呀