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

📄 catchm.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class CatchM extends Applet {

  /* Catching the Duke program by J M Bishop Dec 1996
   * =========================    Java 1.1 Jan 1998
   *
   * Try to catch the duke and hit it by pressing the left
   * mouse button.
   * Illustrates sound, images and movement
   * and mouse handling events. */

    int mx, my, limitx, limity ;
    int wins;
    int boardSize;
    Image duke;

    public void init() {
      wins = 0;
      boardSize = getSize().width - 1;
      duke = getImage(getCodeBase(),"duke.gif");
      this.addMouseListener (new mousePressHandler());
      this.addMouseMotionListener (new mouseMotionHandler());
    }

    class mousePressHandler extends MouseAdapter {

      public void mousePressed (MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        requestFocus();
        if (mx < x && x < mx+limitx &&
            my < y && y < my+limity) {
          wins++;
	     getAppletContext().showStatus("Caught it!  Total " + wins);
          play(getCodeBase(), "sounds/ouch.au");
        		}
        else {
          getAppletContext().showStatus("Missed again.");
          play(getCodeBase(), "sounds/haha.au");
        }
        repaint();
      }
   }

   public class mouseMotionHandler extends MouseMotionAdapter {
     public void mouseMoved(MouseEvent e) {
       if (e.getX() % 3 == 0 && e.getY() % 3 == 0)
         repaint();
     }
   }

   public void paint(Graphics g) {
     // wait till the image is in before getting the
     // size. Can't put these statements in init
     limitx = duke.getWidth(this);
     limity = duke.getHeight(this);
     int change = boardSize-limitx;

     // draw a boundary
     g.drawRect(0, 0, boardSize, boardSize);

      // calculate a new place for the duke
      // and draw it.
      mx = (int)(Math.random()*1000) % change;
      my = (int)(Math.random()*1000) % change;
      g.drawImage(duke, mx, my, this);
    }
}

⌨️ 快捷键说明

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