scenescrolltest.java

来自「java learn PPT java learn PPT java learn」· Java 代码 · 共 107 行

JAVA
107
字号
     import java.applet.*;
     import java.awt.*;
     import java.awt.image.*;
     import java.awt.geom.*;
     import java.util.*;

     public class SceneScrollTest extends Applet implements Runnable
     {
          // 动画线程
          private Thread animation;

          // 屏外绘制图像
          private VolatileGraphics offscreen;

          // 一个放置可滚动物体的场景
          private SpaceScene scene;
 
          public void init()
          {               
               // 用整个applet窗体的大小创建场景             
               scene = new SpaceScene(getBounds());

               // "背景"和"前景"场景图像
               StaticActor[] actors = new StaticActor[2];
               StaticActorGroup group;

               // 创建前景
               group = new StaticActorGroup("mountain.gif");
               group.init(this);
        
               actors[1] = new StaticActor(group);
               actors[1].setPos(0.0, (double) getSize().height - actors[1].getHeight());
               actors[1].setVel(-3, 0);

               // 创建背景
               group = new StaticActorGroup("haze.gif");
               group.init(this);
               
               actors[0] = new StaticActor(group);
               actors[0].setPos(0.0, (double) getSize().height - actors[1].getHeight());
               actors[0].setVel(-1, 0);
                  
               // 设置scenery
               scene.setScenery(actors);                              
            
               
               offscreen = new VolatileGraphics(this);

               animation = new Thread(this);
               
               AnimationStrip.observer = this;

          }   // init


          public void start()
          {
              // 启动动画线程
              animation.start();
          }

          public void stop() 
          {
              animation = null;
          }

          // 执行一个标准绘制循环
          public void run() 
          {
               Thread t = Thread.currentThread();
	       while (t == animation)
               {
                    repaint();

                    try
                    {     
                         Thread.sleep(10);
                    }
                    catch(InterruptedException e) 
                    {     
                         break;
                    }
                    
                }              
           }   // run

           // 让场景执行它的update方法 
           public void update(Graphics g)
           {
                scene.update();

                paint(g);
           }

           // 绘制场景
           public void paint(Graphics g) 
           {
               Graphics2D bg = (Graphics2D)offscreen.getValidGraphics();
               bg.setPaint(Color.BLACK);
               bg.fill(getBounds());

               scene.paint(bg);
               
               g.drawImage(offscreen.getBuffer(), 0, 0, this);
           } 

     }     // SceneScrollTest

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?