import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Component;
public class Animate extends JApplet {
//图片数量
private static final int NUM_OF_PIC = 5;
int count;
Image pics[];
TimerThread timer;
public void init(){
count = 1;
pics = new Image[NUM_OF_PIC];
MediaTracker tracker = new MediaTracker(this);
for(int i = 0; i<NUM_OF_PIC; i++){
//将图片按照0,1,...,NUM_OF_PIC -1,放置在目录中,格式为.jpg
pics[i] = getImage(getCodeBase(), new Integer(i).toString()+".jpg");
tracker.addImage(pics[i], 0);
}
tracker.checkAll(true);
}
public void start(){
timer = new TimerThread(this, 1000);
timer.start();
}
public void stop() {
timer.shouldRun = false;
try{
timer.join();
//等待timer线程退出
}
catch (InterruptedException e){};
}
public void paint(Graphics g){
g.drawImage(pics[count++], 0, 0, null);
if(count == NUM_OF_PIC) count = 0;
}
}class TimerThread extends Thread{
Component comp;
int timediff;
// shouldRun声明为volatile
volatile boolean shouldRun;
public TimerThread(Component comp, int timediff){
super("TimerThread(" + timediff + " millseconds");
this.comp = comp;
this.timediff = timediff;
shouldRun = true;
}
public void run(){
while(shouldRun){
try{
comp.repaint();
sleep(timediff);
}
catch (Exception e){}
}
}
}
这段代码是从网上找来的,我弄到我机子上运行了一下,Applet是启动了,但没达到预期效果啊!有没高人指点下!图片都放在代码同一个目录下了,都是jpg格式的,名字是0,1,2,3,4.不知道是不是因为图片大小不一样所以没反应呢!!!我用的是eclips 6.5企业版。
还有,怎么把这个Applet嵌入到网页上呢?
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Component;
public class Animate extends JApplet {
//图片数量
private static final int NUM_OF_PIC = 5;
int count;
Image pics[];
TimerThread timer;
public void init(){
count = 1;
pics = new Image[NUM_OF_PIC];
MediaTracker tracker = new MediaTracker(this);
for(int i = 0; i<NUM_OF_PIC; i++){
//将图片按照0,1,...,NUM_OF_PIC -1,放置在目录中,格式为.jpg
pics[i] = getImage(getCodeBase(), new Integer(i).toString()+".jpg");
tracker.addImage(pics[i], 0);
}
tracker.checkAll(true);
}
public void start(){
timer = new TimerThread(this, 1000);
timer.start();
}
public void stop() {
timer.shouldRun = false;
try{
timer.join();
//等待timer线程退出
}
catch (InterruptedException e){};
}
public void paint(Graphics g){
g.drawImage(pics[count++], 0, 0, null);
if(count == NUM_OF_PIC) count = 0;
}
}class TimerThread extends Thread{
Component comp;
int timediff;
// shouldRun声明为volatile
volatile boolean shouldRun;
public TimerThread(Component comp, int timediff){
super("TimerThread(" + timediff + " millseconds");
this.comp = comp;
this.timediff = timediff;
shouldRun = true;
}
public void run(){
while(shouldRun){
try{
comp.repaint();
sleep(timediff);
}
catch (Exception e){}
}
}
}
这段代码是从网上找来的,我弄到我机子上运行了一下,Applet是启动了,但没达到预期效果啊!有没高人指点下!图片都放在代码同一个目录下了,都是jpg格式的,名字是0,1,2,3,4.不知道是不是因为图片大小不一样所以没反应呢!!!我用的是eclips 6.5企业版。
还有,怎么把这个Applet嵌入到网页上呢?
解决方案 »
- 关于多线程中wait使用的问题。
- 问一个面对对象中的问题,在线等
- 入门新手求助,二维数组查找
- 在WEB那边没人看...发这边吧.....JSP获得session中数据的问题
- JAVA 串口通信问题 .. 麻烦大家了
- 如何生成exe文件??如何写bat文件??
- 急!为什么TextArea 中没出现 JScrollPane
- 着急,运行 jar文件时控制台的输出能看见么?
- 编译jdk1.4自带的demo---swingset2.java 出错。。。
- 请问这个接口是如何直接调用接口中的方法的?没有找到将接口实现为类实例的代码
- 深拷贝代码中关于forName方法触发ClassNotFoundException异常的问题
- 有个关于Java读取xml文件的小问题
或者:
你自己写一个小的index.htm文件,文件内容:
<applet code=Animate width=200 height=200>
</applet>
或者:
你自己写一个小的index.htm文件,文件内容:
<applet code=Animate(package name: com.Animate) width=200 height=200>
</applet>
有那位高手能帮我写个功能和上面代码一样功能的Applet给我吗,我对线程编程不熟!
<applet code="Animate.java" width=200 height=200>
</applet>
import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Component;
public class Animate extends JApplet {
//图片数量
private static final int NUM_OF_PIC = 5;
int count;
Image pics[];
TimerThread timer;
public void init(){
count = 1;
pics = new Image[NUM_OF_PIC];
MediaTracker tracker = new MediaTracker(this);
for(int i = 0; i<NUM_OF_PIC; i++){
//将图片按照0,1,...,NUM_OF_PIC -1,放置在目录中,格式为.jpg
pics[i] = getImage(getCodeBase(), new Integer(i).toString()+".jpg");
tracker.addImage(pics[i], 0);
}
tracker.checkAll(true);
}
public void start(){
timer = new TimerThread(this, 1000);
timer.start();
}
public void stop() {
timer.shouldRun = false;
try{
timer.join();
//等待timer线程退出
}
catch (InterruptedException e){};
}
public void paint(Graphics g){
g.drawImage(pics[count++], 0, 0, null);
if(count == NUM_OF_PIC) count = 0;
}
}class TimerThread extends Thread{
Component comp;
int timediff;
long time1=System.currentTimeMillis();
long time2=System.currentTimeMillis();
// shouldRun声明为volatile
volatile boolean shouldRun;
public TimerThread(Component comp, int timediff){
super("TimerThread(" + timediff + " millseconds");
this.comp = comp;
this.timediff = timediff;
shouldRun = true;
}
public void run(){
if(time2-time1>=5000)
shouldRun=false;
else
{
shouldRun=true;
try{
comp.repaint();
sleep(timediff);
}
catch (Exception e){}
}
}
}