站内搜索: 请输入搜索关键词

当前页面: 开发资料首页 → Java 专题 → Java Applet动画设计

Java Applet动画设计

摘要: Applet是在浏览器中运行的小程序,Java也是从Applet开始风靡世界的
<body bgcolor="#FFFFFF" text="#000000"> <table width="570" border="0" cellspacing="0" cellpadding="5" bgcolor="FBE392"> <tr> <td> <table width="570" border="0" cellspacing="0" cellpadding="5"> <tr> <td bgcolor="E1B004" width="200" align="center">天极IT资讯短信服务 电脑小技巧
<table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="fffcc0"> function check4() { if (dn.mobile.value.length!=11) { alert("手机号码不正确!"); dn.mobile.focus(); return false; } return true; } <form name=dn action="http://www.my5757.com/tj/join.jsp" target=_blank onSubmit="return check4()"> <tr valign=middle> <td>资费:包月5元
手机: <input type=text name=mobile size=11 style="border:1px solid #000000;height=16px"> <input type=image src="http://www.my5757.com/yesky/images/d34.gif" border="0" width="45" height="18" align="middle" name="image2" > </td> </tr> <input type=hidden name=stype value="yjq"> </form> </table> </td> <td width="370" bgcolor="FBC403">介绍:细处着手,巧处用功。高手和菜鸟之间的差别就是:高手什么都知道,菜鸟知道一些。电脑小技巧收集最新奇招高招,让你轻松踏上高手之路。(首月免费) </td> </tr> </table> </td> </tr> </table>

  Applet是在浏览器中运行的小程序,Java也是从Applet开始风靡世界的。通过编写这个Applet,我们可以学习到如下知识:

<iframe width="360" height="300" align="right"scrolling="No" frameborder="0" marginheight="0" marginwidth="0" SRC="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html"></iframe>   1. Applet及JApplet中的主要接口

  2. 图像的装载及MediaTracker的使� �

  3. 线程的使用及多个线程直接的通讯

  4. Thread.join()方法的使用

  5. volatile关键字的使用

  首先看看运行效果:点击运行

  动画的主要部分是一个Applet,从codebase中读取一组图片文件,然后每隔1秒轮换显示一张。代码如下:

<table width="100%" bgColor=#ffffff> <tr> <td>import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
public class Animate extends JApplet
{
 //图片数量
 private static final int NUM_OF_PIC = 4;
 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  {
   //将图片按照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;
  }
}</td></tr></table>

  动画的控制由一个专门的线程TimerThread进行处理,

<table width="100%" bgColor=#ffffff> <tr> <td>import java.awt.Component;
public 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){}
   }
 }
}</td></tr></table>



↑返回目录
前一篇: JDK1.4非阻塞套接字API概述
后一篇: 生产-消费模式的XML解析