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

📄 minesweeper.java

📁 模仿windows的扫雷游戏 SWT编写的 需要log4j 1.2.4
💻 JAVA
字号:
package cn.pandaoen.game.minesweeper;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import java.util.Locale;

import cn.pandaoen.game.minesweeper.actions.MineSweeperMenu;
import cn.pandaoen.game.minesweeper.border.BoardBottomBorder;
import cn.pandaoen.game.minesweeper.border.BoardLeftBorder;
import cn.pandaoen.game.minesweeper.border.BoardRightBorder;
import cn.pandaoen.game.minesweeper.border.BoardSeparateBorder;
import cn.pandaoen.game.minesweeper.border.BoardTopBorder;
import cn.pandaoen.game.minesweeper.res.Resources;

/**
 * Minesweeper is one of those addictive little games.
 * This class is the main class of the game Minesweeper.
 *  
 * @author pan
 */
public class MineSweeper {

	private static Logger logger = Logger.getRootLogger();

	public static void main(String[] args) {
		// configure log4j
		PropertyConfigurator.configureAndWatch("log4j.properties", 60 * 1000); //$NON-NLS-1$
		logger.trace("Minesweeper opened."); //$NON-NLS-1$

		Locale defaultLocale = C11N.c11n.getLocale();
		logger.info("Default locale = " + defaultLocale.getDisplayName()); //$NON-NLS-1$
		Locale.setDefault(defaultLocale);
		Display.setAppName("Minesweeper"); //$NON-NLS-1$

		final Display display = new Display();
		// no resize, no maximize
		Shell shell = new Shell(display, SWT.DIALOG_TRIM | SWT.MIN);

		new MineSweeper(shell);

		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		C11N.c11n.save();
		Resources.res.dispose();
		display.dispose();
		logger.trace("Minesweeper closed."); //$NON-NLS-1$
	}

	private Point size;
	private int mines;
	private Board board;
	private Counter counter;
	private Button button;
	private Timer timer;
	private Shell shell;

	public MineSweeper(Shell s) {
		this.shell = s;

		// restore the shell location and add listener to save the
		// location when shell is closed.
		shell.setLocation(C11N.c11n.getLocation());
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				C11N.c11n.setLocation(shell.getLocation());
			}
		});
		final Image app = Resources.res.getImage("app.gif"); //$NON-NLS-1$
		shell.setText(Resources.res.getString("Minesweeper")); //$NON-NLS-1$
		shell.setImage(app);

		createContent();
	}

	private void createContent() {
		size = C11N.c11n.getSize();
		mines = C11N.c11n.getMines();

		Composite headComposite = new Composite(shell, SWT.NONE);
		headComposite.setLayout(new GridLayout(3, false));
		counter = new Counter(headComposite, SWT.NONE);
		button = new Button(headComposite, SWT.PUSH);
		button.setImage(Resources.res.getImage("smile.gif")); //$NON-NLS-1$
		button.setToolTipText(Resources.res.getString("New.Tooltip")); //$NON-NLS-1$
		timer = new Timer(headComposite, SWT.NONE);
		counter.setValue(mines);
		counter.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,
				false));
		button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
		timer.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));

		board = new Board(shell, SWT.NONE);
		board.setMinesAndSize(mines, size.x, size.y);

		createBorders(headComposite);

		addListeners();
		new MineSweeperMenu(this);
	}

	public void newGame() {
		logger.trace("Start a new game."); //$NON-NLS-1$
		board.reset();
		counter.setValue(board.getMineCount());
		timer.reset();
		button.setImage(Resources.res.getImage("smile.gif")); //$NON-NLS-1$
	}

	private void createBorders(Composite headComposite) {
		BoardLeftBorder border1 = new BoardLeftBorder(shell);
		BoardTopBorder border2 = new BoardTopBorder(shell);
		BoardRightBorder border3 = new BoardRightBorder(shell);
		BoardBottomBorder border4 = new BoardBottomBorder(shell);
		BoardSeparateBorder border5 = new BoardSeparateBorder(shell);

		FormLayout formLayout = new FormLayout();
		shell.setLayout(formLayout);

		FormData data = new FormData();
		data.width = 10;
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(100, 0);
		border1.setLayoutData(data);

		data = new FormData();
		data.height = 10;
		data.left = new FormAttachment(border1, -2, SWT.DEFAULT);
		data.right = new FormAttachment(border3, 2, SWT.DEFAULT);
		border2.setLayoutData(data);

		data = new FormData();
		data.width = 10;
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		data.bottom = new FormAttachment(100, 0);
		border3.setLayoutData(data);

		data = new FormData();
		data.height = 10;
		data.left = new FormAttachment(border1, -2, SWT.DEFAULT);
		data.right = new FormAttachment(border3, 2, SWT.DEFAULT);
		data.bottom = new FormAttachment(100, 0);
		border4.setLayoutData(data);

		data = new FormData();
		data.left = new FormAttachment(border1, 0, SWT.DEFAULT);
		data.right = new FormAttachment(border3, 0, SWT.DEFAULT);
		data.top = new FormAttachment(border2, 0, SWT.DEFAULT);
		headComposite.setLayoutData(data);

		data = new FormData();
		data.height = 10;
		data.left = new FormAttachment(border1, -2, SWT.DEFAULT);
		data.right = new FormAttachment(border3, 2, SWT.DEFAULT);
		data.top = new FormAttachment(headComposite, 0, SWT.DEFAULT);
		border5.setLayoutData(data);

		data = new FormData();
		data.left = new FormAttachment(border1, 1, SWT.DEFAULT);
		data.right = new FormAttachment(border3, -1, SWT.DEFAULT);
		data.bottom = new FormAttachment(border4, -1, SWT.DEFAULT);
		data.top = new FormAttachment(border5, 1, SWT.DEFAULT);
		board.setLayoutData(data);
	}

	public Board getBoard() {
		return board;
	}

	public Counter getCounter() {
		return counter;
	}

	public Timer getTimer() {
		return timer;
	}

	public Button getButtom() {
		return button;
	}

	public Shell getShell() {
		return shell;
	}

	private void addListeners() {
		final Image care = Resources.res.getImage("care.gif"); //$NON-NLS-1$
		final Image sad = Resources.res.getImage("sad.gif"); //$NON-NLS-1$
		final Image smile = Resources.res.getImage("smile.gif"); //$NON-NLS-1$

		// stop to record time if shell is iconfied
		// restart when shell is restored
		Listener shellListener = new Listener() {
			public void handleEvent(Event event) {
				switch (event.type) {
				case SWT.Iconify:
					logger.trace("iconfied, stop timer"); //$NON-NLS-1$
					timer.stop();
					break;
				case SWT.Deiconify:
					logger.trace("deiconfied, start timer"); //$NON-NLS-1$
					timer.start();
					break;
				}
			}
		};
		shell.addListener(SWT.Iconify, shellListener);
		shell.addListener(SWT.Deiconify, shellListener);

		Listener mouseListener = new Listener() {
			public void handleEvent(Event event) {
				switch (event.type) {
				case SWT.MouseDown:
					if (event.button == 1 || event.button == 2)
						button.setImage(care);
					break;
				case SWT.MouseUp:
					if (board.isExploded()) {
						button.setImage(sad);
						timer.stop();
					} else if (board.isSwept()) {
						win();
					} else {
						button.setImage(smile);
						counter.setValue(board.getMineCount()
								- board.getFlagCount());
						if (!timer.isRunning()) {
							timer.start();
						}
					}
					break;
				}
			}
		};
		board.addListener(SWT.MouseDown, mouseListener);
		board.addListener(SWT.MouseUp, mouseListener);

		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				newGame();
			}
		});
	}

	private void win() {
		final Image happy = Resources.res.getImage("happy.gif"); //$NON-NLS-1$
		button.setImage(happy);
		timer.stop();

		BestTime bestTime = BestTime.loadBestTime();
		int mode = C11N.c11n.getMode();
		if (mode != C11N.CUSTOM) {
			int score = bestTime.getScore(mode);
			if (score > timer.getValue()) {
				NameInputDialog dlg = new NameInputDialog(shell);
				dlg.setScore(timer.getValue());
				dlg.open();
			}
		}
	}
}

⌨️ 快捷键说明

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