仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2210|回复: 18
打印 上一主题 下一主题

[学习教程] JAVA网页编程之使用Java Applet编程完成动画绝技

[复制链接]
不帅 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:43:17 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
专门做了这个例子;而java的这个例子好像就是为了教学而写的,很多教学目的的例子是不考虑优化、性能的。编程绘制动画十分简单,只需遵照以下三步即可;(1)擦往整个小程序绘图区;(2)重画动画背景;(3)在新的位置绘制图形。但是,当动画连续帧之间运动和颜色不连续时就会发生闪烁现象(例子略)。问题出在小程序区的屏幕刷新上,有两种方法可以防止动画闪烁。第一种方法是只做局部刷新,即每次只擦除改变的部分。例如:如果要绘制一幅"飞行的星空"动画,每次刷新操作,先擦往前一位置的星星,再在新的位置绘制一个。但是,如果动画比较复杂,运动部分重叠较多,则局部刷新操作很繁琐也影响程序运行速度。在这种情况下,可以用另外一种方法(双缓存)来解决闪烁问题。它的核心思想是在屏幕外完成一帧的全部动画,然后把最后绘制完的帧显示在小程序屏幕上。过程如所示。


示例程序如下:

importjava.awt.*;
publicclassBallextendsMultiThreadApplet{
Imageball=null;
Imageapplet=null;
GraphicsappletG,ballG;
Publicvoidrun(){
If(ball==null){
Applet=createlmage(bounds().width,bounds().
Height);
Ball=createImage(70,70);
AppletG=applet.getGraphics();
BallG=ball.getGraphics();
BallG.setColor(Color.gray);
BallG.drawOral(0,0,70,70);{
For(intx=0;x<400;x++){Doubleangle="((double)x)/20;"Inty="(int)(Math.abs(Math.sin(angle))*80);"AppletG.clearRect(0,0,bounds().width.bounds(),Helght);DrawBackground(appletG);AppletG.drawImage(ball,x.80-y,this);This.getGraphics().drawImage(applet,0,0,this);Try{Thread.sleep(25);}catch(Exceptionignored){}}}privatevoiddrawBackground(Graphicsg){for(intI="0;I"<1;I++){g.drawLine(0.i*10,400,I*10);}}}
小程序首先用createImage()取得与小程序年夜小完全相同的屏外图形缓存,赋给变量applet,然后得到缓存的绘图对象appletG。以后对帧的刷新操作过程都是针对appletG。这包括清除帧、绘制背景、在新位置绘制图形。最初再用drawImage()方法把缓存复制到小程序的屏幕显示区。运行这个小程序,你会发现动画非常平滑,不存在闪烁现象。
(2)利用双缓冲手艺

  另外一种减小帧之间闪灼的办法是利用双缓冲,它在很多动画Applet中被利用。其次要道理是创立一个背景图象,将必要绘制的一帧画进图象,然后挪用DrawImage()将全部图象一次画到屏幕上往;优点是年夜部分绘制是离屏的,将离屏图象一次绘至屏幕上比间接在屏幕上绘制要无效很多,年夜年夜进步做图的功能。

  双缓冲可使动画光滑,但有一个弱点,要分派一张背景图象,假如图象相称年夜,这将必要很年夜一块内存;当你利用双缓冲手艺时,应重载update()。

  上面举一个时钟的例子来讲明怎样处置动画

//AnimatorDemo.java
importjava.util.*;
importjava.awt.*;
importjava.applet.*;
importjava.text.*;

publicclassAnimatorDemoextendsAppletimplementsRunnable
{
 Threadtimer;//用于显现时钟的线程
 intlastxs,lastys,lastxm,
 lastym,lastxh,lastyh;
 SimpleDateFormatformatter;//格局化工夫显现
 Stringlastdate;//保留以后工夫的字符串
 FontclockFaceFont;//设置显现时钟内里的数字的字体
 DatecurrentDate;//显现以后工夫
 ColorhandColor;//用于显现时针、分针和表盘的色彩
 ColornumberColor;//用于显现秒针和数字的色彩

 publicvoidinit()
 {
  intx,y;
  lastxs=lastys=lastxm=lastym=lastxh=lastyh=0;
  formatter=newSimpleDateFormat("yyyyEEEMMMddhh:mm:ss");
  currentDate=newDate();
  lastdate=formatter.format(currentDate);
  clockFaceFont=newFont("Serif",Font.PLAIN,14);
  handColor=Color.blue;
  numberColor=Color.darkGray;

  try{
   setBackground(newColor(Integer.parseInt(getParameter("bgcolor"),16)));
  }catch(ExceptionE){}
  try{
   handColor=newColor(Integer.parseInt(getParameter("fgcolor1"),16));
  }catch(ExceptionE){}
  try{
   numberColor=newColor(Integer.parseInt(getParameter("fgcolor2"),16));
  }catch(ExceptionE){}
  resize(300,300);//设置时钟窗口巨细
 }

 //盘算四分之一的圆弧
 publicvoidplotpoints(intx0,inty0,intx,inty,Graphicsg)
 {
  g.drawLine(x0+x,y0+y,x0+x,y0+y);
  g.drawLine(x0+y,y0+x,x0+y,y0+x);
  g.drawLine(x0+y,y0-x,x0+y,y0-x);
  g.drawLine(x0+x,y0-y,x0+x,y0-y);
  g.drawLine(x0-x,y0-y,x0-x,y0-y);
  g.drawLine(x0-y,y0-x,x0-y,y0-x);
  g.drawLine(x0-y,y0+x,x0-y,y0+x);
  g.drawLine(x0-x,y0+y,x0-x,y0+y);
 }

 //用Bresenham算法来画圆,个中(x0,y0)是圆的中央,r为圆半径
 publicvoidcircle(intx0,inty0,intr,Graphicsg)
 {
  intx,y;
  floatd;
  x=0;
  y=r;
  d=5/4-r;
  plotpoints(x0,y0,x,y,g);
  while(y>x){
   if(d<0){
    d=d+2*x+3;
    x++;
   }
   else{
    d=d+2*(x-y)+5;
    x++;
    y--; 
   }
   plotpoints(x0,y0,x,y,g);
  }
 }

 publicvoidpaint(Graphicsg)
 {
  intxh,yh,xm,ym,xs,ys,s=0,m=10,h=10,xcenter,ycenter;
  Stringtoday;

  currentDate=newDate();
  SimpleDateFormatformatter=newSimpleDateFormat("s",Locale.getDefault());
  try{
   s=Integer.parseInt(formatter.format(currentDate));
  }catch(NumberFormatExceptionn){
   s=0;
  }
  formatter.applyPattern("m");
  try{
   m=Integer.parseInt(formatter.format(currentDate));
  }catch(NumberFormatExceptionn){
   m=10;
  }
  formatter.applyPattern("h");
  try{
   h=Integer.parseInt(formatter.format(currentDate));
  }catch(NumberFormatExceptionn){
   h=10;
  }
  formatter.applyPattern("EEEMMMddHH:mm:ssyyyy");
  today=formatter.format(currentDate);
  //设置时钟的表盘的中央点为(80,55)
  xcenter=80;
  ycenter=55;

  //a=s*pi/2-pi/2(toswitch0,0from3:00to12:00)
  //x=r(cosa)+xcenter,y=r(sina)+ycenter

  xs=(int)(Math.cos(s*3.14f/30-3.14f/2)*45+xcenter);
  ys=(int)(Math.sin(s*3.14f/30-3.14f/2)*45+ycenter);
  xm=(int)(Math.cos(m*3.14f/30-3.14f/2)*40+xcenter);
  ym=(int)(Math.sin(m*3.14f/30-3.14f/2)*40+ycenter);
  xh=(int)(Math.cos((h*30+m/2)*3.14f/180-3.14f/2)*30+xcenter);
  yh=(int)(Math.sin((h*30+m/2)*3.14f/180-3.14f/2)*30+ycenter);

  //画时钟最表面的圆盘个中心在(xcenter,ycenter)半径为50
  g.setFont(clockFaceFont);
  g.setColor(handColor);
  circle(xcenter,ycenter,50,g);
  //画时钟表盘里的数字
  g.setColor(numberColor);
  g.drawString("9",xcenter-45,ycenter+3);
  g.drawString("3",xcenter+40,ycenter+3);
  g.drawString("12",xcenter-5,ycenter-37);
  g.drawString("6",xcenter-3,ycenter+45);

  //假如需要的话抹往然后重画
  g.setColor(getBackground());
  if(xs!=lastxs||ys!=lastys){
   g.drawLine(xcenter,ycenter,lastxs,lastys);
   g.drawString(lastdate,5,125);
  }
  if(xm!=lastxm||ym!=lastym){
   g.drawLine(xcenter,ycenter-1,lastxm,lastym);
   g.drawLine(xcenter-1,ycenter,lastxm,lastym);}
   if(xh!=lastxh||yh!=lastyh){
    g.drawLine(xcenter,ycenter-1,lastxh,lastyh);
    g.drawLine(xcenter-1,ycenter,lastxh,lastyh);}
   g.setColor(numberColor);
   g.drawString("",5,125);
   g.drawString(today,5,125);
   g.drawLine(xcenter,ycenter,xs,ys);
   g.setColor(handColor);
   g.drawLine(xcenter,ycenter-1,xm,ym);
   g.drawLine(xcenter-1,ycenter,xm,ym);
   g.drawLine(xcenter,ycenter-1,xh,yh);
   g.drawLine(xcenter-1,ycenter,xh,yh);
   lastxs=xs;lastys=ys;
   lastxm=xm;lastym=ym;
   lastxh=xh;lastyh=yh;
   lastdate=today;
   currentDate=null;
  }
  //applet的启动办法
  publicvoidstart()
  {
   timer=newThread(this);
   timer.start();
  }
  //applet的中断办法
  publicvoidstop()
  {
   timer=null;
  }
  //线程的run办法
  publicvoidrun()
  {
   Threadme=Thread.currentThread();
   while(timer==me){
    try{
     Thread.currentThread().sleep(1000);
    }
    catch(InterruptedExceptione){
    }
    repaint();
   }
  }
  //注重:这里重写了update()办法,只是挪用了paint()办法来打消闪灼征象
  publicvoidupdate(Graphicsg)
  {
   paint(g);
  }
 }

  上面是运转该Applet必要的AnimatorDemo.html的内容

<HTML>
<HEAD>
 <TITLE>一个时钟的例子</TITLE>
</HEAD>
<BODY>
 <hr>
 <appletcodebase="."ALIGN=MIDDLEcode="AnimatorDemo.class"width=200height=150>
 </applet>
</BODY>
</HTML>


C++编译的是本地码,优点是启动快,而且可以精确控制资源因此可以开发很高效的程序.缺点是编程麻烦,而且容易留下安全隐患.跨平台靠源代码在各个平台间分别编译(一处编写到处编译)
不帅 该用户已被删除
沙发
 楼主| 发表于 2015-2-19 06:10:28 | 显示全部楼层
你就该学一学Servlet了。Servlet就是服务器端小程序,他负责生成发送给客户端的HTML文件。JSP在执行时,也是先转换成Servlet再运行的。虽说JSP理论上可以完全取代Servlet,这也是SUN推出JSP的本意,可是Servlet用来控制流程跳转还是挺方便的,也令程序更清晰。接下来你应该学习一下Javabean了,可能你早就看不管JSP在HTML中嵌Java代码的混乱方式了,这种方式跟ASP又有什么区别呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-25 22:25

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表