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

📄 ball.java

📁 学习java语言
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
public class Ball {
  public static void main(String args[]) {
    MyFrame frame = new MyFrame();
    frame.setBounds(10,10,500,500);
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter() {
                              public void windowClosing(WindowEvent e) {
                                 System.exit(0);
                               }
                            });
   }
}
class MyFrame extends Frame implements Runnable {
   Thread redBall,blueBall;   
   MyCanvas red,blue;
   double t = 0;
   MyFrame() {
     redBall = new Thread(this);
     blueBall = new Thread(this);
     red = new MyCanvas(Color.red);
     blue = new MyCanvas(Color.blue);
     setLayout(null);
     add(red);
     add(blue);
     red.setLocation(60,100);
     blue.setLocation(60,100);
     redBall.start();
     blueBall.start();
   }
   public void run() {
      while(true) {
      	
        t = t + 0.2;
        if(t>20) t = 0;
        
        if(Thread.currentThread() == redBall) {
          int x = 60;
          int h = (int)(1.0/2*t*t*3.8) + 60;
          red.setLocation(x, h);
          try{ redBall.sleep(1);
          }  catch(InterruptedException e){}
        }
        else if(Thread.currentThread() == blueBall) {
          int x = 60 + (int)(26*t);
          int h = (int)(1.0/2*t*t*3.8) + 60;
          blue.setLocation(x, h);
          try{ blueBall.sleep(1);
          } catch(InterruptedException e) {}
        }
       }
    }
}
class MyCanvas extends Canvas {
   Color c;
   MyCanvas(Color c) {
     setSize(20,20);
     this.c = c;
   }
   public void paint(Graphics g) {
     g.setColor(c);
     g.fillOval(0,0,20,20);
   }
}                                                  

⌨️ 快捷键说明

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