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

📄 moveball.java

📁 Java 入门书的源码
💻 JAVA
字号:
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/* Uses a thread to move a ball ten times 
 * when the user presses a button. 
 */

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class MoveBall extends Applet 
                     implements ActionListener, Runnable {
  private int x = 50, y = 50;
  private Button move;

  public void init() {
    move = new Button("Move");
    add(move);
    move.addActionListener(this);
  }
  public void actionPerformed(ActionEvent event) {
    x = 50; y= 50;
    Thread t = new Thread(this);
    t.start();
  }
  public void paint(Graphics g) {
    g.fillOval(x,y,40,40);
  }
  public void run() {
    for(int i=0; i<10; i++) {
      x+=9;
      y+=9;
      repaint();
      try {
        Thread.sleep(300);
      }catch(InterruptedException e) {
         e.printStackTrace();
      }   
    }
  }
}

⌨️ 快捷键说明

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