我在调试的时候发现的问题,在使用线程实现applet的时候图片加载不完整(不用线程的话就没问题),想了三天了没想出问题到底在哪,夜不能寐啊,求助论坛的各位大大帮我看看,究竟是什么原因,下面是我的applet源码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package imagetest;
import java.applet.*;
import java.awt.*;/**
*
* @author fun
*/
public class MultiThread extends Applet implements Runnable {
private Image[] imgs;
private int totalImages=3;
private int currentImage;
private int i=0;
private Image imgBuf;
private Graphics gBuf;
private MediaTracker mt;
private boolean bStop=false;
public void init()
{
mt=new MediaTracker(this);
imgBuf=createImage(600,400);
gBuf=imgBuf.getGraphics();
gBuf.setColor(Color.white);
gBuf.fillRect(0,0,600,400);
imgs=new Image[totalImages];
for(int i=0;i<totalImages;i++)
{
imgs[i]=getImage(getDocumentBase(),(i+1)+".gif");
mt.addImage(imgs[i],i);
}
try {
mt.waitForID(0);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void start()
{
currentImage=0;
gBuf.drawImage(imgs[currentImage],0,0,this);
currentImage=1;
new Thread(this).start();
}
public void stop()
{
bStop=true;
}
public void paint(Graphics g)
{
System.out.println("draw:"+i++);
g.drawImage(imgBuf,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
while(!bStop)
{
if(mt.checkID(currentImage,true))
{
gBuf.fillRect(0, 0, 600, 400);
gBuf.drawImage(imgs[currentImage], 0, 0, this);
currentImage = ++currentImage % 3;
}
System.out.print("1212121212");
try
{
Thread.sleep(500);
}
catch (Exception ex)
{
//ex.printStackTrace();
}
repaint();
}
}
}下面是调用这个applet的html:
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<applet code="imagetest.MultiThread.class" width=600 height=400 ></applet>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
TODO write content
</body>
</html>图片就随便搞几个静态图片重命名成1.gif 2.gif 3.gif 和html文件放在同一个目录下就可以了 我就不贴了编译完以后把html和gif文件扔到编译过后的文件夹里,运行html文件,可以看到图片确实是有显示,但是有的时候会显示不完整,实在是想不通~
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package imagetest;
import java.applet.*;
import java.awt.*;/**
*
* @author fun
*/
public class MultiThread extends Applet implements Runnable {
private Image[] imgs;
private int totalImages=3;
private int currentImage;
private int i=0;
private Image imgBuf;
private Graphics gBuf;
private MediaTracker mt;
private boolean bStop=false;
public void init()
{
mt=new MediaTracker(this);
imgBuf=createImage(600,400);
gBuf=imgBuf.getGraphics();
gBuf.setColor(Color.white);
gBuf.fillRect(0,0,600,400);
imgs=new Image[totalImages];
for(int i=0;i<totalImages;i++)
{
imgs[i]=getImage(getDocumentBase(),(i+1)+".gif");
mt.addImage(imgs[i],i);
}
try {
mt.waitForID(0);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void start()
{
currentImage=0;
gBuf.drawImage(imgs[currentImage],0,0,this);
currentImage=1;
new Thread(this).start();
}
public void stop()
{
bStop=true;
}
public void paint(Graphics g)
{
System.out.println("draw:"+i++);
g.drawImage(imgBuf,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
while(!bStop)
{
if(mt.checkID(currentImage,true))
{
gBuf.fillRect(0, 0, 600, 400);
gBuf.drawImage(imgs[currentImage], 0, 0, this);
currentImage = ++currentImage % 3;
}
System.out.print("1212121212");
try
{
Thread.sleep(500);
}
catch (Exception ex)
{
//ex.printStackTrace();
}
repaint();
}
}
}下面是调用这个applet的html:
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<applet code="imagetest.MultiThread.class" width=600 height=400 ></applet>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
TODO write content
</body>
</html>图片就随便搞几个静态图片重命名成1.gif 2.gif 3.gif 和html文件放在同一个目录下就可以了 我就不贴了编译完以后把html和gif文件扔到编译过后的文件夹里,运行html文件,可以看到图片确实是有显示,但是有的时候会显示不完整,实在是想不通~
你能否将时间间隔调大,并减少无谓的画图动作如gBuf.fillRect(0, 0, 600, 400).
不是时间的问题 500ms应该不短了 而且就算我改成5s也存在一样的问题gBuf.fillRect(0, 0, 600, 400)这个方法相当于用白色覆盖这个矩形区域 写一个方法是因为我重写了update方法 本来这个类似操作是在update方法中执行的
System.out.print("1212121212");
repaint(); //先调用repaint,再sleep,因为repaint只是通知applet要发生paint,
//但不一定保证paint立即发生,所以会发生交错,
//即paint还没来得及发生,run又重新更新imgs[currentImage]了
try
{
Thread.sleep(500);
}
catch (Exception ex)
{
//ex.printStackTrace();
}
//repaint();