撰寫一"踩地雷"遊戲, 遊戲規則同Windows 95中之踩地雷程式. 使用者可以自訂長, 寬, 以及地雷數. 你不可使用Button或Label等元件來建立地雷區. 你必須 用Graphics的方式畫出來. 當踏到四周都沒有地雷的那格時, 需自動翻開.
/*
 * 踩地雷 
 * FileName: Mine2.java
 * Author:   Shiuh-Sheng Yu
 * Date:     5/19/1999
 * Last Update: 5/19/1999
 */
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Mine extends Component implements MouseListener, ActionListener {
    public Dimension getPreferredSize() {
        return new Dimension(220,220);
    }
    void reset() {
        // initialize your data structure
        repaint();
    }
    public void paint(Graphics g) {
        // draw your content
    }
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("Exit"))          System.exit(0);
        else if (command.equals("New Game")) reset();
        else                                 System.out.println("Unknown Command");
    }
    public Mine(int x, int y) {
        // set up your user interface
        reset();
    }
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {
        int x = e.getX()/20 + 1;
        int y = e.getY()/20 + 1;
        // do your mouse operation
        repaint();
    }
    public static void main(String[] argv) {
        new Mine(10,10);
    }
}
可用Frame.addWindowListener(new CloseWindow(Frame,true)) 來接收關閉視窗的事件, 並結束程式. CloseWindow的原始程式碼如下:
import java.awt.*;
import java.awt.event.*;
public class CloseWindow extends WindowAdapter implements ActionListener {
    Window w;
    boolean quit=false;
    CloseWindow(Window w) {
        super();
        this.w = w;
    }
    CloseWindow(Window w, boolean quit) {
        super();
        this.w = w;
        this.quit = quit;
    }
    public void actionPerformed(ActionEvent e) {
        w.dispose();
        if (quit) {
            System.exit(0);
        }
    }
    public void windowClosing(WindowEvent e) {
        w.dispose();
        if (quit) {
            System.exit(0);
        }
    }
}
Java範例區也有類似的作品