📄 leddigit.java
字号:
package cn.pandaoen.widget.swt;
import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.widgets.Display;
import java.util.ArrayList;
import java.util.List;
/**
* 23 x 13 Raster Led Digit
*
* @author pan
*/
public class LedDigit {
private Image[] digits = new Image[10];
private List<Image> images;
private Display display;
private RGB fontRGB;
private RGB bgRGB;
private RGB maskRGB;
private Logger logger = Logger.getLogger(LedDigit.class);
/**
*
* @param display
*/
public LedDigit(Display display) {
this(display, display.getSystemColor(SWT.COLOR_RED), display
.getSystemColor(SWT.COLOR_BLACK), display
.getSystemColor(SWT.COLOR_DARK_RED));
}
/**
*
* @param display
* @param fontColor
* @param backgroundColor
* @param maskColor
*/
public LedDigit(Display display, Color fontColor, Color backgroundColor,
Color maskColor) {
this(display, fontColor.getRGB(), backgroundColor.getRGB(), maskColor
.getRGB());
}
/**
*
* @param display
* @param fontRGB
* @param backgroundRGB
* @param maskRGB
*/
public LedDigit(Display display, RGB fontRGB, RGB backgroundRGB, RGB maskRGB) {
this.display = display;
this.fontRGB = fontRGB;
this.bgRGB = backgroundRGB;
this.maskRGB = maskRGB;
}
/**
*
* @param digit
* @return
*/
public Image getDigitImage(int digit) {
if (digit < 0 || digit > 9) {
logger.warn("getDigitImage is called with non digit"); //$NON-NLS-1$
return null;
}
if (digits[digit] == null)
createDigitImage(digit);
return digits[digit];
}
/**
*
* @param digit
* @param width
* @param height
* @return
*/
public Image getDigitImage(int digit, int width, int height) {
Image image = getDigitImage(digit);
if (image == null)
return null;
if (width == 13 && height == 23)
return image;
if (images == null)
images = new ArrayList<Image>();
Image scaledImage = new Image(display, width, height);
images.add(scaledImage);
GC gc = new GC(scaledImage);
gc.setAdvanced(true);
if (!gc.getAdvanced()) {
logger.warn("Platform " + SWT.getPlatform() //$NON-NLS-1$
+ " don't support advance image operations"); //$NON-NLS-1$
return image;
}
Transform transform = new Transform(display);
float m11 = width / 13.0f;
float m22 = height / 23.0f;
transform.setElements(m11, 0, 0, m22, 0, 0);
gc.setTransform(transform);
gc.drawImage(image, 0, 0);
gc.dispose();
transform.dispose();
return scaledImage;
}
/**
* Give back an image from the number
* @param number
* @return
*/
public Image getNumberImage(int number) {
if (number < 0) {
logger
.warn("getNumberImage called with negative number " + number + "."); //$NON-NLS-1$//$NON-NLS-2$
number = -number;
}
int len = 0;
int n = number;
do {
len++;
n /= 10;
} while (n > 0);
Image image = new Image(display, 13 * len, 23);
if (images == null)
images = new ArrayList<Image>();
images.add(image);
GC gc = new GC(image);
for (int i = len - 1; i >= 0; i--) {
n = number % 10;
number /= 10;
Image digit = getDigitImage(n);
gc.drawImage(digit, i * 13, 0);
}
gc.dispose();
return image;
}
/**
* Dispose method release all images resource.
*/
public void dispose() {
for (int i = 0; i < digits.length; i++)
if (digits[i] != null)
digits[i].dispose();
if (images != null)
for (Image img : images)
img.dispose();
}
private void createDigitImage(int digit) {
PaletteData pdata = new PaletteData(0xFF0000, 0xFF00, 0xFF);
ImageData imageData = new ImageData(13, 23, 24, pdata);
int bg = pdata.getPixel(bgRGB);
int font = pdata.getPixel(fontRGB);
int mask = pdata.getPixel(maskRGB);
int bits = DIGIT_DEF[digit];
for (int i = 0; i < 23; i++) {
for (int j = 0; j < 13; j++) {
int v = DIGIT_PATTERN[i][j];
if (v == -1)
imageData.setPixel(j, i, bg);
else {
int seg = v % 7;
int flag = (1 << seg);
if ((bits & flag) != 0) {
// This segment should be drawn.
imageData.setPixel(j, i, font);
} else if (v < 7) {
imageData.setPixel(j, i, mask);
} else {
imageData.setPixel(j, i, bg);
}
}
}
}
Image image = new Image(display, imageData);
digits[digit] = image;
}
// 0
// __
// 5 |__| 1
// 4 |__| 2
//
// 3
private static final byte[] DIGIT_DEF = { 0x3F, 0x6, 0x5B, 0x4F, 0x66,
0x6D, 0x7D, 0x7, 0x7F, 0x6F };
// Code Generated, don't change
private static final int[][] DIGIT_PATTERN = {
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
{ -1, -1, 7, 0, 7, 0, 7, 0, 7, 0, 7, -1, -1 },
{ -1, 5, -1, 7, 0, 7, 0, 7, 0, 7, -1, 1, -1 },
{ -1, 12, 5, -1, 7, 0, 7, 0, 7, -1, 1, 8, -1 },
{ -1, 5, 12, 5, -1, -1, -1, -1, -1, 1, 8, 1, -1 },
{ -1, 12, 5, 12, -1, -1, -1, -1, -1, 8, 1, 8, -1 },
{ -1, 5, 12, 5, -1, -1, -1, -1, -1, 1, 8, 1, -1 },
{ -1, 12, 5, 12, -1, -1, -1, -1, -1, 8, 1, 8, -1 },
{ -1, 5, 12, 5, -1, -1, -1, -1, -1, 1, 8, 1, -1 },
{ -1, 12, 5, -1, -1, -1, -1, -1, -1, -1, 1, 8, -1 }, // 9
{ -1, 5, -1, 13, 6, 13, 6, 13, 6, 13, -1, 1, -1 }, // 10
{ -1, -1, 13, 6, 13, 6, 13, 6, 13, 6, 13, -1, -1 }, // 11
{ -1, 4, -1, 13, 6, 13, 6, 13, 6, 13, -1, 2, -1 }, // 12
{ -1, 11, 4, -1, -1, -1, -1, -1, -1, -1, 2, 9, -1 }, // 13
{ -1, 4, 11, 4, -1, -1, -1, -1, -1, 2, 9, 2, -1 },
{ -1, 11, 4, 11, -1, -1, -1, -1, -1, 9, 2, 9, -1 },
{ -1, 4, 11, 4, -1, -1, -1, -1, -1, 2, 9, 2, -1 },
{ -1, 11, 4, 11, -1, -1, -1, -1, -1, 9, 2, 9, -1 },
{ -1, 4, 11, 4, -1, -1, -1, -1, -1, 2, 9, 2, -1 },
{ -1, 11, 4, -1, 10, 3, 10, 3, 10, -1, 2, 9, -1 },
{ -1, 4, -1, 10, 3, 10, 3, 10, 3, 10, -1, 2, -1 },
{ -1, -1, 10, 3, 10, 3, 10, 3, 10, 3, 10, -1, -1 },
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } };
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -