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

📄 counter.java

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

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

import cn.pandaoen.widget.swt.LedDigit;

/**
 * A Counter is a custom widget that draws an fixed length of 
 * integer with a leading number of zeros. 
 * 
 * @author pan
 */
public class Counter extends Canvas {

	private int value;
	private int digits = 3;
	private int width;
	private int height;
	protected LedDigit ledDigit;

	/**
	 * Constructs a new instance of this class given its parent and a style value 
	 * describing its behavior and appearance.
	 */
	public Counter(Composite parent, int style) {
		super(parent, style);

		//FIXME: define size constant, where?
		width = 13;
		height = 23;
		addPaintListener(paintListener);
	}

	/*
	 * @see org.eclipse.swt.widgets.Composite#computeSize(int, int, boolean)
	 */
	public Point computeSize(int wHint, int hHint, boolean changed) {
		checkWidget();

		Rectangle trim = computeTrim(0, 0, digits * width, height);
		return new Point(trim.width, trim.height);
	}

	public void setValue(int value) {
		checkWidget();
		int newValue = Math.max(0, value);
		if (this.value == newValue)
			return;

		this.value = newValue;
		redraw();
	}

	public int getValue() {
		checkWidget();
		return value;
	}

	public void setDigits(int digits) {
		checkWidget();
		int newDigits = Math.max(0, digits);
		if (newDigits == this.digits)
			return;
		this.digits = newDigits;
		redraw();
	}

	public int getDigits() {
		checkWidget();
		return digits;
	}

	@Override
	public void dispose() {
		LedDigit ld = getLedDigit();
		if (ld != null)
			ld.dispose();
		super.dispose();
	}

	/**
	 * This method can be rewrited in subclass in
	 * order to use different led digit.
	 * 
	 * @return
	 */
	protected LedDigit getLedDigit() {
		if (ledDigit == null)
			ledDigit = new LedDigit(getDisplay());
		return ledDigit;
	}

	PaintListener paintListener = new PaintListener() {
		public void paintControl(PaintEvent e) {
			GC gc = e.gc;
			int x = width * (digits - 1);
			int value = getValue();
			for (int i = 0; i < digits; i++) {
				int d = value % 10;
				gc.drawImage(getLedDigit().getDigitImage(d), x, 0);
				value /= 10;
				x -= width;
			}
		}
	};
}

⌨️ 快捷键说明

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