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

📄 jumpball.java

📁 java applet 非常全的实例。比较利于初学者进行研究。
💻 JAVA
字号:
import java.applet.*;
import java.awt.*;

public class jumpball extends Applet implements Runnable{
   Graphics g;
   FontMetrics FM;
   Color c;
   String words = "default";
   Thread t;
   
   int width = 300, height = 200;
   int ballx = 0, bally = 0;
   int stringW, stringH;
   int point = 50, ball = 10;
   int delay = 300;
   int wordsL;
   char Carray[];
   double decrement;
   
   public void init(){
   
      width = getBounds().width;
      height = getBounds().height;
      g = getGraphics();
   
      String parameter;
      
      parameter = getParameter("words");
      if(parameter!=null)
         words = parameter;
             
      parameter = getParameter("delay");
      if(parameter!=null)
         delay = Integer.parseInt(parameter);
      
      decrement = Math.PI/ 8;
      delay /= 8;
      
      wordsL = words.length();
      Carray = new char[wordsL];
      words.getChars(0,wordsL,Carray,0);
      
      Font font = new Font("TimesRoman", Font.BOLD,point);
      g.setFont(font);
      
      FM = g.getFontMetrics();
      
      stringW = FM.stringWidth(words);
      stringH = FM.getHeight();
      
      resize(stringW,stringH*2);

   }
   
   public void start(){  
      if(t == null) {
        t=new Thread(this);
        t.start();
       }
   }
   
   public void run(){
      int currentChar = 0;
      int x  = 0;
      int oldX = 0;
      int charW = FM.charWidth(Carray[currentChar]);

      
      while(true){
         try{
      
            c = new Color((float)Math.random(),(float)Math.random(),(float)Math.random());
            g.setColor(c);
            g.fillRect(0,0,width,height);

            g.setColor(Color.white);
            g.drawString(words,0,stringH+point);
         
            g.setColor(Color.black);
            int oldW = charW;
            charW = FM.charWidth(Carray[currentChar]);
            drawBall(c,oldX, x, oldW, charW);
         
            g.setColor(Color.black);
            g.drawChars(Carray, currentChar, 1, x, stringH + point);
         
            oldX = x;
            x+=charW;
            if(++currentChar == wordsL){
               currentChar = 0;
               x = 0;
            }
            Thread.sleep(500);
         }catch (InterruptedException e){}
         
      }
   }
   public void paint(Graphics g){
      g.setColor(Color.white);
      g.drawString(words,0,stringH+point);
   }
 
   void drawBall(Color c,int oldX, int x, int oldW, int charW){
      if(x < oldX)
         return;
        
      int x1 = oldX + oldW / 2, x2 = x + charW / 2;
      int midX = (x1 + x2) / 2, midY = stringH;
      int radius = midX - x1;
      int y1;
      
      midX-=ball / 2;
      for(double r = Math.PI;r >= 0; r-= decrement){
         try{
            g.setColor(c);
            g.fillArc(ballx, bally, ball, ball, 0, 360);
  
            x1 = midX + (int)(Math.cos(r) * radius);
            y1 = midY - (int)(Math.sin(r) * radius);
         
         
            g.setColor(Color.yellow);
            g.fillArc(x1, y1, ball, ball, 0, 360);
         
            ballx = x1;
            bally = y1;
            Thread.sleep(delay);
         }catch (InterruptedException e){}
      }
      
   }

} 

⌨️ 快捷键说明

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