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

📄 raceapplet.java

📁 java课件及例程有一些PPT及一些例程
💻 JAVA
字号:
import java.awt.*;
import java.lang.*;

public class RaceApplet extends java.applet.Applet implements Runnable{
      final static int SPACING  = 30 ;

      Animal ani1;
      Animal ani2;
      Thread  updateThread;
      public void init(){
         ani1 = new Animal("兔子" , 5);
         ani2 = new Animal("乌龟" , 5);
         ani1.setPriority(1);
         ani2.setPriority(9);
         
         if (updateThread == null) {
              updateThread = new Thread( this, "兔龟赛跑");
              updateThread.setPriority(10);
         }
      }
  

      public boolean mouseDown ( java.awt.Event evt , int x , int y){
           if ( ! updateThread.isAlive())
               updateThread.start();
           if ( ! ani1.isAlive())
               ani1.start();
           if ( ! ani2.isAlive())
               ani2.start();
           return  true ; 
      }
     
      public void paint (Graphics g ){
          g.setColor(Color.lightGray);
          g.fillRect(0,0, size().width , size().height);
          g.setColor(Color.white);
          g.drawString( ani1.getName(),0,SPACING);
          g.setColor(Color.green);
          g.drawString( ani2.getName(),0,2*SPACING);
          update(g);
      }
   
      
      public void update( Graphics g){
          g.setColor(Color.red);
          g.drawLine(SPACING,SPACING,SPACING+(ani1.distance/1000),SPACING);
          g.drawLine(SPACING,2*SPACING,SPACING+(ani2.distance/1000),2*SPACING);
      }


      public void run(){
          while (updateThread != null) {
               repaint();
               try{
                    updateThread.sleep(1000); 
                  }catch ( InterruptedException e) {
                                                    }
          }         
      }
    
      public void stop () {
          if (ani1.isAlive()) {
              ani1.stop();
              ani1=null;
          }
           if (ani2.isAlive()) {
              ani2.stop();
              ani2=null;
          }
            if (updateThread.isAlive()) {
              updateThread.stop();
              updateThread=null;
          }

      }

}


 class Animal extends Thread{
    public int distance =1;
    public int speed;

    public Animal(String name , int speed){
        super(name);
        this.speed = speed;
    }

    public void run(){
        while ( distance <450000) {
         distance += speed;
        }
    }

}

⌨️ 快捷键说明

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