import java.applet.*;
import java.awt.*;
public class photoshow extends Applet implements Runnable{
Image spin[],offscreen;
Thread animator;
final int totalImages=4,delay=100;
int x=0,x1=0,currrentImage=0;
public void init(){
spin=new Image[totalImages];
Toolkit defaultTk=Toolkit.getDefaultToolkit();
MediaTracker tracker=new MediaTracker(this);
for(int i=0;i<totalImages;i++){
spin[i]=defaultTk.getImage((i+1)+".gif");
tracker.addImage(spin[i], i);
try{
tracker.waitForID(i);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
x=0;
animator=new Thread(this);
animator.start();
}
public void run(){
while(Thread.currentThread()==animator){
repaint();
try{
Thread.sleep(delay);
}
catch(InterruptedException e){
e.printStackTrace();
} }
}
public void paint(Graphics g){
/*try{
Thread.sleep(delay);
}
catch(InterruptedException e){
e.printStackTrace();
}*/
g.drawImage(spin[currrentImage],10+x,30,this);
x=(x+5)%400;
currrentImage=++currrentImage%totalImages; }
public void update(Graphics g){
/* g.setColor(Color.blue);
g.fillRect(x1, 30, spin[0].getWidth(this), spin[0].getHeight(this));*/
paint(g);
if(offscreen==null){
offscreen=createImage(getSize().width,getSize().height);
Graphics og=offscreen.getGraphics(); // 创建供绘制闭屏图(off-screen image)使用的图形上下文。
og.setClip(0, 0, getSize().width, getSize().height);
og.setColor(Color.yellow);
og.fillRect(x1, 30, spin[0].getWidth(this),spin[0].getHeight(this));
og.drawImage(spin[currrentImage],10+x,30,this);
super.paint(og);
g.drawImage(offscreen,0,0,null);
x1=x;
x=(x+5)%400;
currrentImage=++currrentImage%totalImages; }
}
}
请分别命名四张图片为1 2 3 4 格式为gif 通过html执行怎么图片运动时后面跟着许多叠影啊,怎么消除,达到每次只显示一张的效果,谢谢了!!
import java.awt.*;
public class photoshow extends Applet implements Runnable{
Image spin[],offscreen;
Thread animator;
final int totalImages=4,delay=100;
int x=0,x1=0,currrentImage=0;
public void init(){
spin=new Image[totalImages];
Toolkit defaultTk=Toolkit.getDefaultToolkit();
MediaTracker tracker=new MediaTracker(this);
for(int i=0;i<totalImages;i++){
spin[i]=defaultTk.getImage((i+1)+".gif");
tracker.addImage(spin[i], i);
try{
tracker.waitForID(i);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
x=0;
animator=new Thread(this);
animator.start();
}
public void run(){
while(Thread.currentThread()==animator){
repaint();
try{
Thread.sleep(delay);
}
catch(InterruptedException e){
e.printStackTrace();
} }
}
public void paint(Graphics g){
/*try{
Thread.sleep(delay);
}
catch(InterruptedException e){
e.printStackTrace();
}*/
g.drawImage(spin[currrentImage],10+x,30,this);
x=(x+5)%400;
currrentImage=++currrentImage%totalImages; }
public void update(Graphics g){
/* g.setColor(Color.blue);
g.fillRect(x1, 30, spin[0].getWidth(this), spin[0].getHeight(this));*/
paint(g);
if(offscreen==null){
offscreen=createImage(getSize().width,getSize().height);
Graphics og=offscreen.getGraphics(); // 创建供绘制闭屏图(off-screen image)使用的图形上下文。
og.setClip(0, 0, getSize().width, getSize().height);
og.setColor(Color.yellow);
og.fillRect(x1, 30, spin[0].getWidth(this),spin[0].getHeight(this));
og.drawImage(spin[currrentImage],10+x,30,this);
super.paint(og);
g.drawImage(offscreen,0,0,null);
x1=x;
x=(x+5)%400;
currrentImage=++currrentImage%totalImages; }
}
}
请分别命名四张图片为1 2 3 4 格式为gif 通过html执行怎么图片运动时后面跟着许多叠影啊,怎么消除,达到每次只显示一张的效果,谢谢了!!
2. 把所有绘制图形的代码都放到下面的方法中, 而不是paint方法
protected void paintComponent(Graphics g) {
super.paintComponent(g);// 这一句非常重要, 如果没有了, 那么双缓冲的功能也就没了. // 这下面是你所有的绘制图形的代码.
}3. 不用修改update方法, 以后需要更新的地方直接调用update就可以了.这样做, 是因为Swing的组件, 即这里使用的JApplet自身就带有双缓冲功能, 而不用你去自己实现.