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

📄 c11n.java

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

import org.eclipse.swt.graphics.Point;

import org.apache.log4j.Logger;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;
import java.util.Properties;

import cn.pandaoen.game.minesweeper.actions.MineSweeperMenu;
import cn.pandaoen.game.minesweeper.res.Resources;

/**
 * Customization Service
 *  
 * @author pan
 */
public final class C11N {

	public static final C11N c11n = new C11N();
	private Properties properties = new Properties();
	private Logger logger = Logger.getLogger(C11N.class);
	private boolean dirty;

	static final String MINES = "mines"; //$NON-NLS-1$
	static final String ROWS = "row"; //$NON-NLS-1$
	static final String COLUMNS = "columns"; //$NON-NLS-1$
	static final String MODE = "mode"; //$NON-NLS-1$
	static final String SOUND = "sound"; //$NON-NLS-1$
	static final String LANG = "language"; //$NON-NLS-1$
	static final String X = "x"; //$NON-NLS-1$
	static final String Y = "y"; //$NON-NLS-1$

	/**
	 * The constants should not be changed!
	 * @see MineSweeperMenu#modeItems
	 */
	public static final int BEGINNER = 0;
	public static final int INTERMEDIATE = 1;
	public static final int EXPERT = 2;
	public static final int CUSTOM = 3;
	public static final int EN = 0;
	public static final int CN = 1;
	public static final int DE = 2;

	public Locale getLocale() {
		String sLocale = properties.getProperty(LANG, "0"); //$NON-NLS-1$
		int lang = 0;
		try {
			lang = Integer.parseInt(sLocale);
		} catch (Exception e) {
			logger.warn(e);
		}
		if (lang < 0 || lang > 2)
			lang = 0;
		return Resources.res.getLocales()[lang];
	}

	public Point getLocation() {
		String sX = properties.getProperty(X, "0"); //$NON-NLS-1$
		String sY = properties.getProperty(Y, "0"); //$NON-NLS-1$
		Point res = new Point(0, 0);
		try {
			res.x = Integer.parseInt(sX);
			res.y = Integer.parseInt(sY);
		} catch (Exception e) {
			logger.warn(e);
		}

		return res;
	}

	public void setLocation(Point location) {
		if (!dirty)
			dirty = true;

		properties.put(X, String.valueOf(location.x));
		properties.put(Y, String.valueOf(location.y));
	}

	public void setLocale(Locale locale) {
		Locale[] ls = Resources.res.getLocales();
		for (int i = 0; i < ls.length; i++) {
			if (ls[i] == locale) {
				setLocale(i);
				return;
			}
		}
	}

	public void setLocale(int locale) {
		if (!dirty)
			dirty = true;

		properties.put(LANG, String.valueOf(locale));
	}

	public boolean getSound() {
		String sSound = properties.getProperty(SOUND, "true"); //$NON-NLS-1$
		return sSound.equals("true"); //$NON-NLS-1$
	}

	public Point getSize(int mode) {
		if (mode == BEGINNER)
			return new Point(9, 9);
		if (mode == INTERMEDIATE)
			return new Point(16, 16);
		if (mode == EXPERT)
			return new Point(16, 30);
		return getCustomSize();
	}

	public void setSize(Point size) {
		if (!dirty)
			dirty = true;

		properties.put(ROWS, String.valueOf(size.x));
		properties.put(COLUMNS, String.valueOf(size.y));
	}

	public Point getSize() {
		return getSize(getMode());
	}

	public int getMines() {
		return getMines(getMode());
	}

	public void setMines(int mines) {
		if (!dirty)
			dirty = true;

		properties.put(MINES, String.valueOf(mines));
	}

	public int getMines(int mode) {
		if (mode == BEGINNER)
			return 10;
		if (mode == INTERMEDIATE)
			return 40;
		if (mode == EXPERT)
			return 99;
		return getCustomMines();
	}

	private int getCustomMines() {
		String sMines = properties.getProperty(MINES, "10"); //$NON-NLS-1$
		int mines;
		try {
			mines = Integer.parseInt(sMines);
		} catch (Exception e) {
			logger.warn(e);
			return 10;
		}

		return mines;
	}

	public int getMode() {
		String sMode = properties.getProperty(MODE, "1"); //$NON-NLS-1$
		int mode = 1;
		try {
			mode = Integer.parseInt(sMode);
		} catch (Exception e) {
			logger.warn(e);
		}

		if (mode < 0 || mode > 3)
			return 0;
		return mode;
	}

	public void setMode(int mode) {
		if (!dirty)
			dirty = true;

		properties.put(MODE, String.valueOf(mode));
	}

	private Point getCustomSize() {
		String sRows = properties.getProperty(ROWS, "16"); //$NON-NLS-1$
		String sColumns = properties.getProperty(COLUMNS, "16"); //$NON-NLS-1$

		int rows, columns;
		try {
			rows = Integer.parseInt(sRows);
			columns = Integer.parseInt(sColumns);
		} catch (Exception e) {
			logger.warn(e);
			return new Point(16, 16);
		}

		return new Point(rows, columns);
	}

	public void save() {
		if (!dirty)
			return;

		logger.info("Save custom settings"); //$NON-NLS-1$
		try {
			properties.store(new FileOutputStream("minesweeper.ini"), //$NON-NLS-1$
					"Setting for Minesweeper"); //$NON-NLS-1$
		} catch (IOException e) {
			logger.warn(e);
		}
	}

	C11N() {
		try {
			properties.load(new FileInputStream("minesweeper.ini")); //$NON-NLS-1$
		} catch (IOException e) {
			logger.warn(e);
		}
	}
}

⌨️ 快捷键说明

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