import java.applet.Applet;
import java.awt.Graphics;class ImageTest extends Applet
{
Image[] MyPicture ;
Image offScreenIma;
Graphics GraphicsIma;
public void init()
{
MyPictrue=new Image[10];
//Image ImageBeauty;
offScreenIma=createImage(getSize().width,getSize().heigth);
GraphicsIma=offScreenIma.getGraphics();
for(int i=0;i<MyPicture.length;i++)
{
MyPicture[0]=getImage(getDocumentBase(),parseString(i)+".jpg");
GraphicsIma.drawImage(MyPicture[i],100,100,this);
}
void paint(Graphics g)
{
g.drawImage(offScreenIma,100,100,this);
}
}
import java.awt.Graphics;class ImageTest extends Applet
{
Image[] MyPicture ;
Image offScreenIma;
Graphics GraphicsIma;
public void init()
{
MyPictrue=new Image[10];
//Image ImageBeauty;
offScreenIma=createImage(getSize().width,getSize().heigth);
GraphicsIma=offScreenIma.getGraphics();
for(int i=0;i<MyPicture.length;i++)
{
MyPicture[0]=getImage(getDocumentBase(),parseString(i)+".jpg");
GraphicsIma.drawImage(MyPicture[i],100,100,this);
}
void paint(Graphics g)
{
g.drawImage(offScreenIma,100,100,this);
}
}
import java.awt.Graphics;
import java.awt.*;
class ImageTest extends Applet
{
Image[] MyPicture ;
Image offScreenIma;
Graphics GraphicsIma;
public void init()
{
MyPicture=new Image[10];
//Image ImageBeauty;
offScreenIma=createImage(getSize().width,getSize().height);
GraphicsIma=offScreenIma.getGraphics();
for(int i=0;i<MyPicture.length;i++)
{
MyPicture[0]=getImage(getDocumentBase(),"example"+i+".jpg");
GraphicsIma.drawImage(MyPicture[i],100,100,this);
}
}
public void paint(Graphics g)
{
g.drawImage(offScreenIma,100,100,this);
}
}
你的错有点多哦,包也没导入完,就开始用包中的方法了。简单修改了下
MyPicture[0]=getImage(getDocumentBase(),"example"+i+".jpg");
可能达不到我的目的
假设你有15幅图片,你可以再加入这些代码,实现你动画的效果
public void start()
{
mythread=new Thread(this);
mythread.start();
}
public void stop()
{
mythread=null;
}
public void run()
{
while(true)
{
if(count>15)
count=0;
repaint();
count++;
try{ mythread.sleep(200);
}
catch(InterruptedException e){}
}
}
public void paint(Graphics g) //再把paint方法改下
{ if((MyPicture[count])!=null)
g.drawImage(offScreenIma,100,100,this);
}
import java.applet.*;
import java.awt.*;
import java.awt.Graphics;public class ShowImage extends Applet {
private int currentImage;
private int totalImage=10;
private Image[] images=new Image[totalImage];
public void init(){
for(int i=0;i<10;i++)
images[i]=getImage(getDocumentBase(),"T"+(i+1)+".gif");
}
public void paint(Graphics g){
g.drawImage(images[currentImage], 30,20,this);
currentImage=++currentImage%10;
try {
Thread.sleep(500);
}
catch (Exception ex) {
ex.printStackTrace();
}
repaint();
} public void start(){
currentImage=0;
}}
方法二:使用双缓冲
import java.applet.*;
import java.awt.*;
import java.awt.Graphics;public class DoubleBuffer extends Applet {
private int currentImage;
private int totalImage=10;
private Image[] images=new Image[totalImage];
private Image buffImage;
private Graphics gBuf;
private int i=0;
private MediaTracker mt;//图像跟踪 public void init(){
mt=new MediaTracker(this);
buffImage=createImage(600,400);
gBuf=buffImage.getGraphics();
gBuf.setColor(Color.WHITE);
gBuf.fillRect(0,0,600,400); for(int i=0;i<10;i++){
images[i]=getImage(getDocumentBase(),"T"+(i+1)+".gif");
try {
mt.waitForID(i);
}
catch (Exception ex) {
ex.printStackTrace();
} }
}
public void paint(Graphics g){
g.drawImage(buffImage, 30,20,this);//将缓冲区的画的换到面板上。
if(mt.checkID(currentImage,true)){ gBuf.fillRect(0,0,600,400); //清除缓冲区。再画
gBuf.drawImage(images[currentImage], 30,20,this);
currentImage=++currentImage%10;
} try {
Thread.sleep(500);
}
catch (Exception ex) {
ex.printStackTrace();
}
repaint();
} public void start(){
currentImage=0;
gBuf.drawImage(images[currentImage], 30,20,this);//再缓冲区画.
currentImage=1;
} public void update(Graphics g){
paint(g);
}}
方法三 运用线程机制
import java.applet.*;
import java.awt.*;public class MutiThread extends Applet implements Runnable{
private int currentImage;
private int totalImage=10;
private Image[] images=new Image[totalImage];
private Image buffImage;
private Graphics gBuf;
private int i=0;
private MediaTracker mt;//图像跟踪
private boolean bStop; public void init(){
bStop=false;
mt=new MediaTracker(this);
buffImage=createImage(600,400);
gBuf=buffImage.getGraphics();
gBuf.setColor(Color.WHITE);
gBuf.fillRect(0,0,600,400); for(int i=0;i<10;i++){
images[i]=getImage(getDocumentBase(),"T"+(i+1)+".gif");
try {
mt.waitForID(i);
}
catch (Exception ex) {
ex.printStackTrace();
} }
}
public void paint(Graphics g){
g.drawImage(buffImage, 30,20,this);//将缓冲区的画的换到面板上。
} public void start(){
currentImage=0;
gBuf.drawImage(images[currentImage], 30,20,this);//再缓冲区画.
currentImage=1;
new Thread(this);
} public void update(Graphics g){
paint(g);
} public void stop(){
bStop=true;
} public void run(){
while(!bStop){
if(mt.checkID(currentImage,true)){ gBuf.fillRect(0,0,600,400); //清除缓冲区。再画
gBuf.drawImage(images[currentImage], 30,20,this);
currentImage=++currentImage%10;
} try {
Thread.sleep(500);
}
catch (Exception ex) {
ex.printStackTrace();
}
repaint(); }
}}
但是我还有一些很不成熟的问题.
比如说,己经用这个方法:
g.drawImage(buffImage, 30,20,this);将缓冲区的画的换到面板上。
为什么 还要这样写呢:
if(mt.checkID(currentImage,true)){ gBuf.fillRect(0,0,600,400); //清除缓冲区。再画
gBuf.drawImage(images[currentImage], 30,20,this);
为什么这里的:gBuf.fillRect(0,0,600,400); 可以清除缓冲区,再画?
还有,运用线程机制的时候,创建的线程在哪里启动的呢?
public void start(){
currentImage=0;
gBuf.drawImage(images[currentImage], 30,20,this);//再缓冲区画.
currentImage=1;
new Thread(this).start(); //忘写start()了,呵呵
}
2 gBuf.fillRect(0,0,600,400);
将缓冲区,用我们先前设置的颜色填充gBuf.setColor(Color.WHITE);
在将下一副画画上,如果不使用gBuf.fillRect(0,0,600,400);
得到的将是叠加的效果.
3 使用gBuf.drawImage(images[currentImage], 30,20,this);
不能保证在内存中真的把画完全画完
(这点我也有点疑惑)
共勉
class MuilThread implements Runnable
{
public static void main(String[] args)
{ //static int i=0;
MuilThread mt=new MuilThread();
Thread t1=new Thread(mt);
Thread t2=new Thread(mt);
System.out.println("线程t1启动");
t1.start();
try
{
Thread.sleep(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("线程t2启动");
t2.start();
}
public void run()
{
int count=10;
for( int i=0;i<count;i++)
{
System.out.println(i);
}
count=++count%10;
}
}