Animation effect using Images in Applet and Thread in Java.
Source code:
import java.awt.*;
import java.applet.*;
/*
<applet code=SimpleImage_Animation.class width=400 height=400>
<param name="img1" value="1.png">
<param name="img2" value="2.png">
<param name="img3" value="3.png">
<param name="img4" value="4.png">
</applet>
*/
public class SimpleImage_Animation extends Applet
{
Image img1,img2,img3,img4,imgTmp;
public void init()
{
img1 = getImage(getDocumentBase(), getParameter("img1"));
img2 = getImage(getDocumentBase(), getParameter("img2"));
img3 = getImage(getDocumentBase(), getParameter("img3"));
img4 = getImage(getDocumentBase(), getParameter("img4"));
imgTmp=img1;
newthread s=new newthread(this);
}
public void paint(Graphics g)
{
g.drawImage(imgTmp, 0, 0, this);
}
}
//This class is used to create thread:
class newthread implements Runnable
{
Thread t;
SimpleImage_Animation obj;
newthread(SimpleImage_Animation obj)
{
this.obj=obj;
t=new Thread(this ,"animate");
t.start();
}
public void run()
{
try
{
for(;;)
{
Thread.sleep(700);
if(obj.imgTmp==obj.img1)
{
obj.imgTmp=obj.img2;
obj.repaint();
}
Thread.sleep(700);
if(obj.imgTmp==obj.img2)
{
obj.imgTmp=obj.img3;
obj.repaint();
}
Thread.sleep(700);
if(obj.imgTmp==obj.img3)
{
obj.imgTmp=obj.img4;
obj.repaint();
}
Thread.sleep(700);
if(obj.imgTmp==obj.img4)
{
obj.imgTmp=obj.img1;
obj.repaint();
}
}// For complete
} // Try complete
catch(Exception e)
{
System.out.println(e);
}
} // run complete
} // class complete
import java.applet.*;
/*
<applet code=SimpleImage_Animation.class width=400 height=400>
<param name="img1" value="1.png">
<param name="img2" value="2.png">
<param name="img3" value="3.png">
<param name="img4" value="4.png">
</applet>
*/
public class SimpleImage_Animation extends Applet
{
Image img1,img2,img3,img4,imgTmp;
public void init()
{
img1 = getImage(getDocumentBase(), getParameter("img1"));
img2 = getImage(getDocumentBase(), getParameter("img2"));
img3 = getImage(getDocumentBase(), getParameter("img3"));
img4 = getImage(getDocumentBase(), getParameter("img4"));
imgTmp=img1;
newthread s=new newthread(this);
}
public void paint(Graphics g)
{
g.drawImage(imgTmp, 0, 0, this);
}
}
//This class is used to create thread:
class newthread implements Runnable
{
Thread t;
SimpleImage_Animation obj;
newthread(SimpleImage_Animation obj)
{
this.obj=obj;
t=new Thread(this ,"animate");
t.start();
}
public void run()
{
try
{
for(;;)
{
Thread.sleep(700);
if(obj.imgTmp==obj.img1)
{
obj.imgTmp=obj.img2;
obj.repaint();
}
Thread.sleep(700);
if(obj.imgTmp==obj.img2)
{
obj.imgTmp=obj.img3;
obj.repaint();
}
Thread.sleep(700);
if(obj.imgTmp==obj.img3)
{
obj.imgTmp=obj.img4;
obj.repaint();
}
Thread.sleep(700);
if(obj.imgTmp==obj.img4)
{
obj.imgTmp=obj.img1;
obj.repaint();
}
}// For complete
} // Try complete
catch(Exception e)
{
System.out.println(e);
}
} // run complete
} // class complete
- Save the file with name SimpleImage_Animation.java
1. png,2.png,4.png,4.png files at the same location where your java file is stored.They are also shown below.You can modify as per your wish.
- Complie with: javac SimpleImage_Animation.java
- Run with: appletviewer SimpleImage_Animation.java
No comments:
Post a Comment