- package JavaApplet;
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.util.ArrayList;
- import java.util.List;
- //在Applet上绘制一个图像,可以调用drawImage静态方法
- public class JavaAppletDemo extends Applet
- {
- private MediaTracker mt;//来控制图片是否完全被加载
- private int imageSum = 5; //图片总数为10
- private Image image[];
- private int currentImage;//当前绘制的图像索引
- //双缓冲技术
- private Image imageBuff;
- private Graphics grs;
- public void init() {
- mt = new MediaTracker(this);//跟踪本对象
- imageBuff = createImage(600, 400);//创建图片缓冲
- grs = imageBuff.getGraphics();//将imageBuff关联到绘图
- grs.setColor(Color.WHITE);
- grs.fillRect(0, 0, 600, 400);
- image = new Image[imageSum];//为数组分配空间
- for(int x=0;x<imageSum;x++)
- {
- image[x] = getImage(getDocumentBase(),(x+1)+".gif");//getDocumentBase来返回URL路径
- mt.addImage(image[x], x);//将跟踪的图片添加到跟踪器
- }
- try {
- mt.waitForID(0);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(getDocumentBase());
- System.out.println(getCodeBase());
- }
- @Override
- public void start() {
- currentImage = 0;//每次从新启动的时候图片的索引都被初始化
- grs.drawImage(image[currentImage],0 , 0, this);//将图形绘制到缓冲区当中
- currentImage = 1;
- }
- @Override
- public void stop() {
- }
- @Override
- public void destroy() {
- }
- int x=0;//移动的坐标
- public void paint(Graphics g) {
- g.drawImage(imageBuff, 0, 0, this);
- if(mt.checkID(currentImage,true))
- {
- grs.fillRect(0, 0, 600, 400);
- grs.drawImage(image[currentImage], x, 10, this);
- x++;
- // currentImage =++currentImage%10;
- currentImage++;
- if(currentImage==imageSum){currentImage=0;}
- }
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- repaint();
- }
- public void update(Graphics g) {
- <FONT color=red>//去除闪烁 我在这里重写了 update方法 为什么还会闪烁?</FONT>
- paint(g);
- }
- }
复制代码 |