bufferedgraphicstest.java

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

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

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

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

          // 要绘制的Image
          private Image pipe;

          // 图像帧的宽和高
          private int imageWidth;
          private int imageHeight;

          // Image对象的位置,速度和转速
          private int x;
          private int y;
 
          private int vx;
          private int vy;

          private double rot;

          private AffineTransform at;
          private final double ONE_RADIAN = Math.toRadians(10);

          public void init()
          {
              // 创建要绘制的图像
              pipe = getImage(getDocumentBase(), "pipe.gif"); 
              while(pipe.getWidth(this) <= 0);

              // 创建屏外图像
              offscreen = new BufferedGraphics(this);

              imageWidth = pipe.getWidth(this);
              imageHeight = pipe.getHeight(this);

              vx = 3+(int)(Math.random()*5);
              vy = 3+(int)(Math.random()*5);

              x = getSize().width/2  - imageWidth/2;
              y = getSize().height/2 - imageHeight/2;
              rot = 0;
              at = AffineTransform.getTranslateInstance(x, y);

          }   // init

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

          public void stop() 
          {
              animation = null;
          }

          public void run() 
          {
               Thread t = Thread.currentThread();
	       while (t == animation)
               {
                    try
                    {     
                         Thread.sleep(33);
                    }
                    catch(InterruptedException e) 
                    {
                         break;
                    }

                    repaint();
                }              
          }   // run

          public void update(Graphics g)
          {
               // 更新对象的位置
               x += vx;
               y += vy;

               // 保持对象在窗体之内
               if(x < 0)
               {
                    x = 0;
                    vx = -vx;
               }
               else if(x > getSize().width - imageWidth)
               {
                    x = getSize().width - imageWidth;
                    vx = -vx;
               }

               if(y < 0)
               {
                    y = 0;
                    vy = -vy;
               }
               else if(y > getSize().height - imageHeight)
               {
                    y = getSize().height - imageHeight;
                    vy = -vy;
               }

               if(vx > 0)
                    rot += ONE_RADIAN;
               else
                    rot -= ONE_RADIAN;


               // 设置图像的变换
               at.setToIdentity();
               at.translate(x + imageWidth/2, y + imageHeight/2);     
               at.rotate(rot);
               at.translate(-imageWidth/2, -imageHeight/2);
               
               paint(g);
          }     

          public void paint(Graphics g) 
          {
               // 校验并清除屏外图像
               Graphics2D bg = (Graphics2D)offscreen.getValidGraphics();
               bg.setColor(Color.BLACK);
               bg.fill(new Rectangle(getSize().width, getSize().height));

               // 在屏外图像中绘制管状体
               bg.drawImage(pipe, at, this);

               // 把屏外图像绘制到 applet窗体上 
               g.drawImage(offscreen.getBuffer(), 0, 0, this);

          }    // paint

     }    // BufferedGraphicsTest  

⌨️ 快捷键说明

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