⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testimage.java

📁 用java编写的一个电梯模型程序
💻 JAVA
字号:
/**
    TestImage是Applet的一个子类,实现了Runnable接口
    它可与主线程并行操作
    自行判断平移方向
*/

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.applet.Applet;

public class TestImage extends Applet implements Runnable{
        Image myImage[] = new Image[5];
        Image usImage[] = new Image[5];
        Image currenting;

        // 进电梯过程的图片
        String[] bizGif = {"myPic\\biz01.jpg","myPic\\biz02.jpg",
                           "myPic\\biz03.jpg","myPic\\biz04.jpg","myPic\\biz05.jpg"};
        // 出电梯过程的图片
        String[] bizGif2 = {"usPic\\biz05.jpg","usPic\\biz04.jpg",
                           "usPic\\biz03.jpg","usPic\\biz02.jpg","usPic\\biz01.jpg"};
        public int xPos;
        public int firstXPos;  // 动画开始点横坐标
        public int secondXPos; // 动画结束点横坐标
        public int yPos;       //  纵坐标
        Thread newTest = null;
        Modoul modoul = null;
        int floor;
       
        /*  TestImage(int xPos,int yPos,int secondXPos,Modoul modoul,int floor)
            函数参数:1. xpos ----- 平移开始点
                      2. ypos ----- 平移的纵坐标
                      3. secondXPos ------- 平移结束点
                      4. modoul ------- 主体类的对象
                      5. floor ------- 当前层
        */
        public TestImage(int xPos,int yPos,int secondXPos,Modoul modoul,int floor){
             this.firstXPos = xPos;
             this.yPos = yPos;
             this.secondXPos = secondXPos;
             this.modoul = modoul;  // 将applet的对象传入
             this.floor = floor;
        }
        public void start(){
           if(newTest == null){
                newTest = new Thread(this,"ok");
                newTest.start();
           }
           for(int i=0;i<bizGif.length;++i){
                 myImage[i]= modoul.getImage(modoul.getCodeBase(),bizGif[i]);
                 usImage[i]= modoul.getImage(modoul.getCodeBase(),bizGif2[i]);
           }
        }
        public void run(){
             runToLight(firstXPos,secondXPos);  // 动画执行函数
        }
        public void  inferiorpaint(){
           if(currenting != null){
                 (modoul.getGraphics()).drawImage(currenting,xPos,yPos,this);
                  try{
                       Thread.sleep(150);
                  } catch (InterruptedException e){
                  }
           if(firstXPos < secondXPos){  // 平移从左向右进行
                 (modoul.getGraphics()).clearRect(xPos,yPos,20,currenting.getHeight(this));
           }
           else {        // 平移从右向左进行
                 (modoul.getGraphics()).clearRect(xPos+currenting.getWidth(this),yPos,20,currenting.getHeight(this));
           } 
           }           
        }
        public void runToLight(int start,int end){

          if(start < end){     // 有人想乘坐电梯
             currenting = myImage[0];
             for(int i=start,j=0;i<end;i+=15,j++){
                  xPos = i;
                  if(j<5){
                     currenting = myImage[j];
                  }
                  else {
                     j=0;
                  }
                  inferiorpaint();   //绘图
                  try{
                       Thread.sleep(150);
                  } catch (InterruptedException e){
                  }
             }
          Same.floorDraw[floor-1] = true; // floor层有乘客等待的标志
          }
          else {
             currenting = usImage[0];
              for(int i=start,j=0;i>end;i-=15,j++){
                  xPos = i;
                  if(j<5){
                     currenting = usImage[j];
                  }
                  else {
                     j=0;
                  }
                  inferiorpaint();      // 绘制图片
                  try{
                       Thread.sleep(150);  // 延时
                  } catch (InterruptedException e){
                  }
             }             
          }
        }

} 

⌨️ 快捷键说明

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