Minesweeper Source
(Last Update 10:36 on Friday, 02 March 2001)
- // Java Minesweeper applet
- // Neil Rashbrook
- // Last modified June 17 1998
- import java.applet.*;
- import java.awt.*;
- final class jm11 extends Applet {
-
- public Image getImage(java.net.URL url, String s) {
-
- try {
- java.io.InputStream is = getClass().getResourceAsStream(s);
- byte b[] = new byte[is.available()];
- is.read(b);
- return Toolkit.getDefaultToolkit().createImage(b);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public java.net.URL getCodeBase() {
- return null;
- }
- }
- final class jmimage implements java.awt.image.ImageObserver {
-
- private final Component target;
- private final Applet applet;
- private final Image all, list[], buffer;
- private final Graphics g;
- private final int observed, width, height;
- private int last = -1;
- public jmimage(Component target, Applet applet, String base, int count, int observed, int width, int height) {
-
- this.target = target;
- this.applet = applet;
- this.list = new Image[count];
- this.observed = observed;
- this.width = width;
- this.height = height;
- all = applet.getImage(applet.getCodeBase(), base + "s.gif");
- try {
-
- target.prepareImage(all, target);
- buffer = target.createImage(width, height);
- try {
- g = buffer.getGraphics();
- } catch (Exception e) {
- e.printStackTrace();
- g = null;
- }
- } catch (Exception e) {
- e.printStackTrace();
- buffer = null;
- g = null;
- }
- try {
-
- for (int i = 0; i < count; i++) {
- list[i] = applet.getImage(applet.getCodeBase(), base + Character.forDigit(i, count) + ".gif");
- target.prepareImage(list[i], this);
- }
- } finally {
- return;
- }
- }
- public void drawImage(int num, Graphics g, int x, int y) {
-
- if (list[num] != null && (target.checkImage(list[num], this) & target.ALLBITS) != 0) g.drawImage(list[num], x, y, this);
- else if (all != null && (target.checkImage(all, target) & target.ALLBITS) != 0) {
-
- if (this.g == null) g.create(x, y, width, height).drawImage(all, -(width * num), 0, target);
- else {
-
- if (num != last) {
- last = num;
- this.g.drawImage(all, -(width * num), 0, target);
- }
- g.drawImage(buffer, x, y, this);
- }
- }
- }
- public synchronized boolean imageUpdate(Image img, int flags, int x, int y, int width, int height) {
- return ((flags & (ALLBITS | ABORT)) == 0);
- }
- public static boolean isReady(jmimage imgs) {
- return imgs != null && (imgs.all == null || (imgs.target.checkImage(imgs.all, imgs.target) & (ALLBITS | ABORT)) != 0);
- }
- }
- public final class jmapplet extends Applet implements Runnable {
-
- public static final int BEGINNER = 0, INTERMEDIATE = 1, EXPERT = 2, CUSTOM = 3;
- private static final int NEW = 0, LOST = 1, WON = 2, PLAY = 3, STOP = 4;
- private static final int NONE = 0, FIELD = 3, BUTTON = 4;
- private boolean buttoned;
- private int state, down, flags, seconds, left;
- private int board[][];
- private long startTime, stopTime;
- private Thread ticker;
- private int marks, game, rows, cols, mines, width, height;
- private int row, col, push;
- private jmimage tfields;
- private static jmimage buttons, digits, fields, tields;
- private static AudioClip won, lost;
- public jmapplet() {
- setBackground(Color.lightGray);
- }
- public void destroy() {
- ticker.interrupt();
- }
- private static void draw3DBorder(Graphics g, int x, int y, int width, int height, Color topleft, Color botright) {
- g.setColor(topleft);
- g.drawLine(x, y, x, y + height);
- g.drawLine(x, y, x + width, y);
- x++;
- y++;
- g.setColor(botright);
- g.drawLine(x + width, y, x + width, y + height);
- g.drawLine(x, y + height, x + width, y + height);
- }
- public String getAppletInfo() {
- return "Minesweeper Applet © 1998 Neil Rashbrook";
- }
- public int getCols() {
- return cols;
- }
- public boolean getFields() {
- return tfields == tields;
- }
- public int getGame() {
- return game;
- }
- private static int getInteger(int value, int min, int max) {
- if (value < min) return min;
- else if (value > max) return max;
- else return value;
- }
- private int getInteger(String name) {
-
- try {
- return Integer.parseInt(getParameter(name));
- } catch (Exception e) {
- return 0;
- }
- }
- public boolean getMarks() {
- return marks == 0x30;
- }
- public int getMines() {
- return mines;
- }
- public Dimension getMinimumSize() {
- return new Dimension(width, height);
- }
- public String[][] getParameterInfo() {
-
- return new String[][] {
- {"level", "String", "Beginner, Intermediate or Expert"},
- {"rows", "int", "Rows 8-24"},
- {"cols", "int", "Columns 8-30"},
- {"mines", "int", "Mines 10+"},
- {"marks", "boolean", "Use ? marks"},
- {"graphics", "boolean", "Use old graphics"}};
- }
- public Dimension getPreferredSize() {
- return new Dimension(width, height);
- }
- public int getRows() {
- return rows;
- }
- public int getTime() {
- return seconds;
- }
- public boolean imageUpdate(Image img, int flags, int x, int y, int width, int height) {
- return (flags & ALLBITS) == 0 || super.imageUpdate(img, flags, x, y, width, height);
- }
- private boolean inButton(Event evt, int x, int y) {
- return !evt.metaDown() && y > 16 && y <= 40 && x + x + 24 >= width && x + x - 24 < width;
- }
- private int inField(Event evt, int x, int y) {
-
- if (x >= 12 && y >= 55 && x < width - 12 && y < height - 12) {
- col = (x - 12) >> 4;
- row = (y - 55) >> 4;
- return evt.metaDown() ? 2 : 1;
- }
- return 0;
- }
- public void init() {
-
- final Applet applet;
- ticker = new Thread(this, "Ticker");
- ticker.setDaemon(true);
- ticker.start();
- if (won == null && (won = getAudioClip(getCodeBase(), "jmwon.au")) != null) {
- won.play();
- won.stop();
- }
- if (lost == null && (lost = getAudioClip(getCodeBase(), "jmlost.au")) != null) {
- lost.play();
- lost.stop();
- }
- try {
- applet = (Applet)Class.forName("jm11").newInstance();
- } catch (Exception e) {
- applet = this;
- }
- if (buttons == null) buttons = new jmimage(this, this, "button", 5, 0, 24, 24);
- if (digits == null) digits = new jmimage(this, this, "digit", 11, 0, 13, 23);
- if (tields == null) tields = new jmimage(this, this, "tield", 16, 12, 16, 16);
- if (fields == null) fields = new jmimage(this, this, "field", 16, 12, 16, 16);
- tfields = "true".equalsIgnoreCase(getParameter("graphics")) ? tields : fields;
- setMarks("true".equalsIgnoreCase(getParameter("marks")));
- String s = getParameter("level");
- if (s == null || "Beginner".equalsIgnoreCase(s)) newBeginner();
- else if ("Intermediate".equalsIgnoreCase(s)) newIntermediate();
- else if ("Expert".equalsIgnoreCase(s)) newExpert();
- else newCustom(getInteger("rows"), getInteger("cols"), getInteger("mines"));
- }
- public boolean isFocusTraversable() {
- return true;
- }
- public Dimension minimumSize() {
- return new Dimension(width, height);
- }
- public boolean mouseDown(Event evt, int x, int y) {
-
- final Graphics g = getGraphics();
- if (buttoned = inButton(evt, x, y)) {
- down = BUTTON;
- paintButton(g);
- return true;
- } else if (state % PLAY == 0) {
-
- if ((push = inField(evt, x, y)) != 2 || board[row][col] >= 0x30) {
- down = FIELD;
- paintButton(g);
- repaintPush();
- } else {
- if ((board[row][col] & 0x10) != 0) flags--;
- board[row][col] += 0x10;
- board[row][col] %= marks;
- if ((board[row][col] & 0x10) != 0) flags++;
- paintFlags(g);
- push = 0;
- repaint(col * 16 + 12, row * 16 + 55, 16, 16);
- }
- }
- g.dispose();
- return true;
- }
- public boolean mouseDrag(Event evt, int x, int y) {
-
- if (buttoned) {
-
- final int newdown = inButton(evt, x, y) ? BUTTON : NONE;
- if (down != newdown) {
- down = newdown;
- final Graphics g = getGraphics();
- paintButton(g);
- g.dispose();
- }
- } else if (down == FIELD) {
- repaintPush();
- push = inField(evt, x, y);
- repaintPush();
- }
- return true;
- }
- public boolean mouseUp(Event evt, int x, int y) {
-
- if (buttoned) {
- if (inButton(evt, x, y)) newGame();
- } else if (down == FIELD) {
-
- repaintPush();
- if (inField(evt, x, y) > 0) switch (push) {
- case 1:
- startTimer();
- revealField(row, col);
- break;
- case 2:
-
- int check = board[row][col];
- for (int l = Math.max(0, row - 1); l < Math.min(rows, row + 2); l++)
-
- for (int m = Math.max(0, col - 1); m < Math.min(cols, col + 2); m++)
- if ((board[l][m] & 0x30) == 0x10) check--;
- if (check != 0x30) break;
- for (int l = Math.max(0, row - 1); l < Math.min(rows, row + 2); l++)
-
- for (int m = Math.max(0, col - 1); m < Math.min(cols, col + 2); m++)
- revealField(l, m);
- break;
- }
- push = 0;
- }
- if (down != NONE) {
- down = NONE;
- final Graphics g = getGraphics();
- paintButton(g);
- g.dispose();
- }
- return true;
- }
- public void newGame() {
-
- state = NEW;
- down = NONE;
- flags = 0;
- seconds = 0;
- left = rows * cols - mines;
- board = new int[rows][cols];
- for (int i = 0; i < mines; ) {
-
- int j = (int)(Math.random() * rows);
- int k = (int)(Math.random() * cols);
- if (board[j][k] == 9) continue;
- board[j][k] = 9;
- for (int l = Math.max(0, j - 1); l < Math.min(rows, j + 2); l++)
-
- for (int m = Math.max(0, k - 1); m < Math.min(cols, k + 2); m++)
- if (board[l][m] < 9) board[l][m]++;
- i++;
- }
- repaint();
- }
- public void newBeginner() {
- game = BEGINNER;
- newGame(8, 8, 10);
- }
- public void newCustom(int rows, int cols, int mines) {
- game = CUSTOM;
- rows = getInteger(rows, 8, 24);
- cols = getInteger(cols, 8, 30);
- newGame(rows, cols, getInteger(mines, 10, (this.rows - 1) * (this.cols - 1)));
- }
- public void newExpert() {
- game = EXPERT;
- newGame(16, 30, 99);
- }
- private void newGame(int rows, int cols, int mines) {
- this.rows = rows;
- this.cols = cols;
- this.mines = mines;
- height = rows * 16 + 67;
- width = cols * 16 + 24;
- resize(width, height);
- newGame();
- }
- public void newIntermediate() {
- game = INTERMEDIATE;
- newGame(16, 16, 40);
- }
- public void paint(Graphics g) {
-
- Rectangle clipRect = g.getClipRect();
- if (clipRect.x < 12 || clipRect.y < 55 || clipRect.x + clipRect.width > width - 12 || clipRect.y + clipRect.height > height - 12) {
- draw3DBorder(g, 0, 0, width - 2, height - 2, Color.white, Color.gray);
- draw3DBorder(g, 1, 1, width - 4, height - 4, Color.white, Color.gray);
- draw3DBorder(g, 2, 2, width - 6, height - 6, Color.white, Color.gray);
- draw3DBorder(g, 9, 9, width - 20, 35, Color.gray, Color.white);
- draw3DBorder(g, 10, 10, width - 22, 33, Color.gray, Color.white);
- draw3DBorder(g, (width - 26) >> 1, 15, 24, 24, Color.gray, Color.gray);
- draw3DBorder(g, 16, 15, 39, 23, Color.gray, Color.white);
- draw3DBorder(g, width - 56, 15, 39, 23, Color.gray, Color.white);
- draw3DBorder(g, 9, 52, width - 20, height - 63, Color.gray, Color.white);
- draw3DBorder(g, 10, 53, width - 22, height - 65, Color.gray, Color.white);
- draw3DBorder(g, 11, 54, width - 24, height - 67, Color.gray, Color.white);
- paintButton(g);
- paintFlags(g);
- paintSeconds(g);
- }
- paintFields(g, (clipRect.y - 55) >> 4, (clipRect.x - 12) >> 4, (clipRect.y + clipRect.height - 40) >> 4, (clipRect.x + clipRect.width + 3) >> 4);
- }
- private void paintButton(Graphics g) {
- if (jmimage.isReady(buttons)) buttons.drawImage(down == NONE ? state % PLAY : down, g, (width - 24) >> 1, 16);
- }
- private void paintFields(Graphics g, int i, int k, int l, int m) {
-
- if (jmimage.isReady(tfields)) {
-
- if (i < 0) i = 0;
- if (k < 0) k = 0;
- if (l > rows) l = rows;
- if (m > cols) m = cols;
- for (; i < l; i++) for (int j = k; j < m; j++) {
-
- int offset;
- if (board[i][j] >= 0x30) offset = board[i][j] & 0xF;
- else if (state == WON) offset = 13;
- else if (state == LOST && (board[i][j] & 0x1F) == 9) offset = 10;
- else if (state == LOST && board[i][j] >= 0x10 && board[i][j] <= 0x18) offset = 11;
- else if ((board[i][j] & 0x10) == 0 && i > row - push && i < row + push && j > col - push && j < col + push)
- offset = (board[i][j] & 0x20) == 0 ? 0 : 15;
- else offset = 12 + (board[i][j] >> 4);
- tfields.drawImage(offset, g, j * 16 + 12, i * 16 + 55);
- }
- }
- }
- private void paintFlags(Graphics g) {
- paintNumber(g, 17, 16, mines - flags);
- }
- private void paintNumber(Graphics g, int x, int y, int n) {
-
- if (jmimage.isReady(digits)) {
-
- boolean onGraphics = g == null;
- if (onGraphics) g = getGraphics();
- if (g == null) return;
- final int offset;
- if (n >= 0) offset = n / 100 % 10;
- else {
- n = -n;
- offset = 10;
- }
- digits.drawImage(offset, g, x, y);
- digits.drawImage(n / 10 % 10, g, x + 13, y);
- digits.drawImage(n % 10, g, x + 26, y);
- if (onGraphics) g.dispose();
- }
- }
- private void paintSeconds(Graphics g) {
- paintNumber(g, width - 55, 16, seconds);
- }
- public Dimension preferredSize() {
- return new Dimension(width, height);
- }
- private void repaintFields() {
- repaint(12, 55, width - 24, height - 67);
- }
- private void repaintPush() {
- int i = Math.max(row + 1 - push, 0);
- int k = Math.max(col + 1 - push, 0);
- int l = Math.min(row + push, rows);
- int m = Math.min(col + push, cols);
- repaint(k * 16 + 12, i * 16 + 55, (m - k) * 16, (l - i) * 16);
- }
- private void revealField(int j, int k) {
-
- if ((board[j][k] & 0x10) == 0) {
-
- board[j][k] |= 0x30;
- repaint(k * 16 + 12, j * 16 + 55, 16, 16);
- switch (board[j][k]) {
- case 0x39:
- stop(LOST);
- repaintFields();
- break;
- case 0x30:
-
- for (int l = Math.max(0, j - 1); l < Math.min(rows, j + 2); l++)
-
- for (int m = Math.max(0, k - 1); m < Math.min(cols, k + 2); m++)
- revealField(l, m);
- /* no break */
- default:
-
- if (--left == 0) {
-
- stop(WON);
- if (flags < mines) {
- flags = mines;
- final Graphics g = getGraphics();
- paintFlags(g);
- g.dispose();
- repaintFields();
- }
- }
- break;
- }
- }
- }
- public synchronized void run() {
-
- try {
-
- for (;;) {
-
- if (state == PLAY) stopTime = System.currentTimeMillis();
- long elapsed = stopTime - startTime;
- seconds = state == NEW ? 0 : (int)(elapsed / 1000);
- final Graphics g = getGraphics();
- if (g != null) {
- paintSeconds(g);
- g.dispose();
- }
- if (state == WON) getParent().postEvent(new Event(this, Event.ACTION_EVENT, this));
- if (state == PLAY) wait((seconds + 1) * 1000 - elapsed);
- else wait();
- }
- } catch (InterruptedException e) {
- }
- }
- public void setCustom() {
- game = CUSTOM;
- }
- public void setFields(boolean graphics) {
- tfields = graphics ? tields : fields;
- repaintFields();
- }
- public void setMarks(boolean marks) {
- this.marks = marks ? 0x30 : 0x20;
- }
- public synchronized void start() {
-
- if (state == STOP) {
- state = PLAY;
- startTime += System.currentTimeMillis() - stopTime;
- notifyAll();
- }
- }
- private synchronized void startTimer() {
-
- if (state == NEW) {
- state = PLAY;
- startTime = System.currentTimeMillis();
- notifyAll();
- }
- }
- public void stop() {
- stop(STOP);
- }
- private synchronized void stop(int state) {
-
- if (this.state == PLAY) {
- this.state = state;
- stopTime = System.currentTimeMillis();
- notifyAll();
- if (state == LOST) lost.play();
- else if (state == WON) won.play();
- }
- }
- public void update(Graphics g) {
- paint(g);
- }
- }
- final class jmicon extends Canvas {
-
- private final Image icon;
- public jmicon(Image icon) {
- setBackground(Color.lightGray);
- this.icon = icon;
- }
- public Dimension getMinimumSize() {
- int width = icon.getWidth(this), height = icon.getHeight(this);
- if (width >= 0 && height >= 0) return new Dimension(width + width, height + height);
- else return super.getMinimumSize();
- }
- public Dimension getPreferredSize() {
- int width = icon.getWidth(this), height = icon.getHeight(this);
- if (width >= 0 && height >= 0) return new Dimension(width + width, height + height);
- else return super.getMinimumSize();
- }
- public Dimension minimumSize() {
- int width = icon.getWidth(this), height = icon.getHeight(this);
- if (width >= 0 && height >= 0) return new Dimension(width + width, height + height);
- else return super.minimumSize();
- }
- public void paint(Graphics g) {
- Rectangle bounds = bounds();
- int width = icon.getWidth(this), height = icon.getHeight(this);
- g.drawImage(icon, (bounds.width - width) >> 1, (bounds.height - height) >> 1, getBackground(), this);
- }
- public Dimension preferredSize() {
- int width = icon.getWidth(this), height = icon.getHeight(this);
- if (width >= 0 && height >= 0) return new Dimension(width + width, height + height);
- else return super.preferredSize();
- }
- }
- final class jmdialog extends Dialog {
-
- public boolean cancelled;
- public Button ok, cancel;
- private boolean labels;
- public jmdialog(Frame parent, String title, int x, int y, int width, int height) {
- super(parent, title, true);
- setBackground(Color.lightGray);
- setResizable(false);
- setFont(new Font(getFont().getName(), Font.PLAIN, getFont().getSize() - 1));
- Point p = parent.location();
- reshape(p.x + x, p.y + y, width, height);
- }
- public boolean action(Event evt, Object obj) {
-
- if ("Reset Scores".equals(obj)) {
-
- getParent().action(evt, obj);
- for (int i = 2; i <= 10; i += 4) {
- ((Label)getComponent(i)).setText("999");
- ((Label)getComponent(i + 2)).setText("Anonymous");
- }
- } else {
- cancelled = "Cancel".equals(obj);
- hide();
- }
- return true;
- }
- private static boolean focusComponent(Container cont) {
-
- int count = cont.countComponents();
- for (int i = 0; i < count; i++) {
-
- Component comp = cont.getComponent(i);
- if (comp instanceof Container && focusComponent((Container)comp)) return true;
- else if (comp instanceof Button || comp instanceof TextField) {
- comp.requestFocus();
- return true;
- }
- }
- return false;
- }
- public boolean gotFocus(Event evt, Object arg) {
- if (evt.target instanceof TextField) ((TextField)evt.target).selectAll();
- else if (evt.target == this) focusComponent(this);
- return true;
- }
- public boolean handleEvent(Event evt) {
-
- if (!labels) {
- ok.setLabel("OK");
- if (cancel != null) cancel.setLabel("Cancel");
- labels = true;
- }
- if (evt.id != evt.WINDOW_DESTROY) return super.handleEvent(evt);
- cancelled = true;
- hide();
- return true;
- }
- public void hide() {
- getParent().enable();
- super.hide();
- }
- public boolean keyDown(Event evt, int key) {
-
- if (evt.modifiers == 0) switch (key) {
- case evt.ENTER:
- if (evt.target instanceof Button) return action(evt, ((Button)evt.target).getLabel());
- break;
- case evt.ESCAPE:
- cancelled = true;
- hide();
- return true;
- case evt.TAB:
- ((Component)evt.target).nextFocus();
- return true;
- }
- return false;
- }
- public boolean lostFocus(Event evt, Object arg) {
- if (evt.target instanceof TextField) ((TextField)evt.target).select(0, 0);
- return true;
- }
- }
- final class jmframe extends Frame {
-
- private final jmapplet j;
- private CheckboxMenuItem g[], m, c;
- private int times[];
- private String best[];
- public jmframe(Image icon, AppletStub stub) {
- super("Minesweeper");
- setIconImage(icon);
- setBackground(Color.lightGray);
- setResizable(false);
- j = new jmapplet();
- j.setStub(stub);
- add("Center", j);
- pack();
- j.init();
- times = new int[4];
- times[2] = times[1] = times[0] = 999;
- best = new String[3];
- best[2] = best[1] = best[0] = "Anonymous";
- MenuBar mb = new MenuBar();
- Menu game = new Menu("Game");
- g = new CheckboxMenuItem[4];
- g[0] = new CheckboxMenuItem("Beginner");
- g[1] = new CheckboxMenuItem("Intermediate");
- g[2] = new CheckboxMenuItem("Expert");
- g[3] = new CheckboxMenuItem("Custom...");
- m = new CheckboxMenuItem("Marks (?)");
- m.setState(j.getMarks());
- c = new CheckboxMenuItem("Color");
- c.setState(j.getFields());
- g[j.getGame()].setState(true);
- game.add("New F2");
- game.add("-");
- game.add(g[0]);
- game.add(g[1]);
- game.add(g[2]);
- game.add(g[3]);
- game.add("-");
- game.add(m);
- game.add(c);
- game.add("-");
- game.add("Best Times...");
- game.add("-");
- game.add("Exit");
- mb.add(game);
- Menu help = new Menu("Help");
- help.add("Help Topics");
- help.add("-");
- help.add("About Minesweeper");
- mb.add(help);
- mb.setHelpMenu(help);
- pack();
- setMenuBar(mb);
- pack();
- }
- public boolean action(Event evt, Object obj) {
-
- for (int i = 0; i < 4; i++) if (evt.target == g[i]) {
-
- int game = i;
- for (i = 0; i < 4; i++) g[i].setState(i == game);
- switch (game) {
- case 0: j.newBeginner(); break;
- case 1: j.newIntermediate(); break;
- case 2: j.newExpert(); break;
- default:
-
- jmdialog d = new jmdialog(this, "Custom Field", 25, 60, 237, 136);
- GridBagLayout l = new GridBagLayout();
- d.setLayout(l);
- GridBagConstraints g = new GridBagConstraints();
- g.insets.right = 11;
- g.insets.left = 11;
- //g.fill = g.HORIZONTAL;
- g.weightx = 1;
- g.gridx = 2;
- l.setConstraints(d.add(d.ok = new Button("Reset Scores")), g);
- g.anchor = g.NORTH;
- g.gridy = 1;
- l.setConstraints(d.add(d.cancel = new Button("Reset Scores")), g);
- g.anchor = g.CENTER;
- g.insets.right = 0;
- g.gridy = 0;
- g.gridx = 0;
- l.setConstraints(d.add(new Label("Height:")), g);
- g.gridy = 1;
- l.setConstraints(d.add(new Label("Width:")), g);
- g.gridy = 2;
- l.setConstraints(d.add(new Label("Mines:")), g);
- g.gridx = 1;
- g.insets.left = 0;
- g.weighty = 2;
- g.gridy = 0;
- l.setConstraints(d.add(new TextField(Integer.toString(j.getRows()), 2)), g);
- g.weighty = 1;
- g.gridy = 1;
- l.setConstraints(d.add(new TextField(Integer.toString(j.getCols()), 2)), g);
- g.weighty = 2;
- g.gridy = 2;
- l.setConstraints(d.add(new TextField(Integer.toString(j.getMines()), 3)), g);
- d.validate();
- ((Window)d).show();
- if (!d.cancelled) {
- int rows = getInteger(d.getComponent(5));
- int cols = getInteger(d.getComponent(6));
- int mines = getInteger(d.getComponent(7));
- j.newCustom(rows, cols, mines);
- }
- j.setCustom();
- }
- pack();
- return true;
- }
- if (obj == "New F2") j.newGame();
- else if (obj == "Help Topics") j.getAppletContext().showDocument(j.getCodeBase());
- else if (evt.target == m) j.setMarks(m.getState());
- else if (evt.target == c) j.setFields(c.getState());
- else if (obj == "Exit") {
- j.stop();
- hide();
- } else if (obj == "About Minesweeper") {
- jmdialog d = new jmdialog(this, "About Minesweeper", 25, 60, 0, 0);
- d.add("West", new jmicon(getIconImage()));
- Panel p = new Panel();
- d.add("South", p);
- p.add(d.ok = new Button("Reset Scores"));
- p = new Panel();
- d.add("Center", p);
- p.setLayout(new GridLayout(0, 1));
- p.add(new Label("Java Minesweeper 1.1", Label.CENTER));
- p.add(new Label("by Neil Rashbrook", Label.CENTER));
- p.add(new Label("based on Minesweeper", Label.CENTER));
- p.add(new Label("by Robert Donner and Curt Johnson", Label.CENTER));
- d.pack();
- ((Window)d).show();
- } else if ("Reset Scores".equals(obj)) {
- times[2] = times[1] = times[0] = 999;
- best[2] = best[1] = best[0] = "Anonymous";
- } else {
-
- if (evt.target == j) {
- int game = j.getGame();
- if (j.getTime() >= times[game]) return true;
- String s;
- switch (game) {
- case 0: s = "You have the fastest time for beginner"; break;
- case 1: s = "You have the fastest time for intermediate"; break;
- case 2: s = "You have the fastest time for expert"; break;
- default: return true;
- }
- jmdialog d = new jmdialog(this, "Congratulations", 25, 60, 228, 139);
- GridBagLayout l = new GridBagLayout();
- d.setLayout(l);
- GridBagConstraints g = new GridBagConstraints();
- g.insets.left = 13;
- g.insets.right = 13;
- g.weightx = 1;
- g.weighty = 1;
- g.anchor = g.SOUTHWEST;
- g.gridy = 0;
- l.setConstraints(d.add(new Label(s)), g);
- g.anchor = g.NORTHWEST;
- g.gridy = 1;
- l.setConstraints(d.add(new Label("level. Please type your name:")), g);
- g.fill = g.HORIZONTAL;
- g.gridy = 2;
- TextField t = new TextField(best[game], 5);
- l.setConstraints(d.add(t), g);
- g.fill = g.NONE;
- g.anchor = g.NORTHEAST;
- g.gridy = 3;
- l.setConstraints(d.add(d.ok = new Button("Reset Scores")), g);
- d.validate();
- ((Window)d).show();
- best[game] = t.getText();
- times[game] = j.getTime();
- } else if (obj != "Best Times...") {
- System.err.println("action:" + evt + "," + obj);
- return false;
- }
- jmdialog d = new jmdialog(this, "Best Times", 25, 60, 330, 179);
- GridBagLayout l = new GridBagLayout();
- d.setLayout(l);
- GridBagConstraints g = new GridBagConstraints();
- g.weighty = 1;
- g.fill = g.HORIZONTAL;
- g.insets.left = 22;
- g.gridx = 0;
- g.gridy = 0;
- g.gridwidth = 2;
- l.setConstraints(d.add(new Label("Fastest Mine Sweepers")), g);
- g.gridwidth = 1;
- for (int i = 0; i < 3; i++) {
- String s;
- if (i == 0) s = "Beginner:";
- else s = i == 1 ? "Intermediate:" : "Expert:";
- g.gridy++;
- g.insets.left = 22;
- g.anchor = g.WEST;
- l.setConstraints(d.add(new Label(s)), g);
- g.insets.left = 0;
- g.gridx++;
- g.anchor = g.EAST;
- l.setConstraints(d.add(new Label(String.valueOf(times[i]), Label.RIGHT)), g);
- g.gridx++;
- g.anchor = g.WEST;
- l.setConstraints(d.add(new Label(" seconds")), g);
- g.gridx++;
- l.setConstraints(d.add(new Label(best[i])), g);
- g.gridx = 0;
- }
- g.gridy++;
- g.gridwidth = 4;
- g.weightx = 1;
- g.fill = g.NONE;
- g.anchor = g.EAST;
- Panel p = new Panel();
- l.setConstraints(d.add(p), g);
- l = new GridBagLayout();
- p.setLayout(l);
- g.gridwidth = 1;
- g.gridx = 1;
- g.gridy = 0;
- g.insets.right = 11;
- g.insets.bottom = 11;
- l.setConstraints(p.add(d.ok = new Button("Reset Scores")), g);
- g.gridx = 0;
- l.setConstraints(p.add(new Button("Reset Scores")), g);
- d.validate();
- ((Window)d).show();
- }
- return true;
- }
- public void destroy() {
- j.destroy();
- }
- public static int getInteger(Component j) {
-
- try {
- return Integer.parseInt(((TextField)j).getText());
- } catch (Exception e) {
- return 0;
- }
- }
- public boolean handleEvent(Event evt) {
-
- switch (evt.id) {
- case evt.WINDOW_DESTROY: hide();
- case evt.WINDOW_ICONIFY: j.stop(); break;
- case evt.WINDOW_MOVED:
- case evt.WINDOW_DEICONIFY: pack(); j.start(); break;
- case evt.WINDOW_EXPOSE:
- default:
- return super.handleEvent(evt);
- }
- return true;
- }
- public boolean keyDown(Event evt, int key) {
- if (evt.modifiers == 0) switch (key) {
- case evt.ESCAPE: hide(); j.stop(); return true;
- case evt.F1: j.getAppletContext().showDocument(j.getCodeBase()); return true;
- case evt.F2: j.newGame(); return true;
- }
- return false;
- }
- }
- final public class jmloader extends Applet implements AppletStub {
-
- private jmframe f;
- private Image icon;
- public jmloader() {
- setBackground(Color.lightGray);
- }
- public void appletResize(int width, int height) {
- if (f != null) f.pack();
- }
- public void destroy() {
- f.destroy();
- f.dispose();
- f = null;
- }
- public String getAppletInfo() {
- return "Minesweeper Applet © 1998 Neil Rashbrook";
- }
- public String[][] getParameterInfo() {
-
- return new String[][] {
- {"level", "String", "Beginner, Intermediate or Expert"},
- {"rows", "int", "Rows 8-24"},
- {"cols", "int", "Columns 8-30"},
- {"mines", "int", "Mines 10+"},
- {"marks", "boolean", "Use ? marks"},
- {"graphics", "boolean", "Use old graphics"},
- {"show", "boolean", "Show window on startup"}};
- }
- public void init() {
-
- icon = getImage(getCodeBase(), "icon.gif");
- f = new jmframe(icon, this);
- if ("true".equalsIgnoreCase(getParameter("show"))) {
- f.pack();
- f.show();
- }
- }
- public boolean isFocusTraversable() {
- return true;
- }
- public boolean keyDown(Event evt, int key) {
- f.pack();
- f.show();
- return super.keyDown(evt, key);
- }
- public boolean mouseDown(Event evt, int x, int y) {
- f.pack();
- f.show();
- return true;
- }
- public void paint(Graphics g) {
- Rectangle bounds = bounds();
- int width = icon.getWidth(this), height = icon.getHeight(this);
- g.drawImage(icon, (bounds.width - width) >> 1, (bounds.height - height) >> 1, getBackground(), this);
- }
- }

38.107.191.104