一个数字显示管的小程序
2005-01-16 12:33
190 查看
闲来无事,就喜欢写写小的程序,前段时间写了个数码管的小程序,发现还是很有用处的。
自己做了个测试程序,显示结果如下:
以下是程序源代码和测试程序代码。希望大家能给点建议。包括程序结构,功能方面的都很欢迎。
/*-**************************************************************/
* Author : OuJinLiang
* Copyright : SEI.BUAA (2003) .copyright reserved.
* Date : 2004-12-24
/*-**************************************************************/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
/**
* 数码管
*/
public class Digital {
// 单个字符的默认宽度
private static final int DEFAULT_WIDTH = 50;
// 单个字符的默认高度
private static final int DEFAULT_HEIGHT = 90;
// 默认背景色
private static final Color DEFAULT_BACKGROUND = Color.BLACK;
// 默认前景色
private static final Color DEFAULT_FOREGROUND = Color.RED;
private static int bits[][];
private static int polygon[][][];
private Color background;
private Color foreground;
private int width;
private int height;
private int gap;
private int offset;
private Polygon polygonArray[] = new Polygon[9];
static {
bits = new int[][] { { '0', 0x03F }, { '1', 0x018 }, { '2', 0x06E }, { '3', 0x07C }, { '4', 0x059 }, { '5', 0x075 }, { '6', 0x077 }, { '7', 0x01C }, { '8', 0x07F }, { '9', 0x07D }, { 'A', 0x05F }, { 'B', 0x073 }, { 'C', 0x027 }, { 'D', 0x07A }, { 'E', 0x067 }, { 'F', 0x047 }, { 'T', 0x184 }, { 'P', 0x04F }, { 'M', 0x19F }, { ':', 0x180 }, { '-', 0x040 }, { -1, 0x0 }, };
int p1[][] = { { 0, 0, 10, 10 }, { 2, 43, 38, 12 } };
int p2[][] = { { 0, 0, 10, 10 }, { 46, 88, 78, 52 } };
int p3[][] = { { 2, 48, 38, 12 }, { 0, 0, 10, 10 } };
int p4[][] = { { 50, 50, 40, 40 }, { 2, 43, 38, 12 } };
int p5[][] = { { 50, 50, 40, 40 }, { 46, 88, 77, 52 } };
int p6[][] = { { 2, 48, 38, 12 }, { 90, 90, 80, 80 } };
int p7[][] = { { 2, 12, 38, 48, 38, 12 }, { 45, 40, 40, 45, 50, 50 } };
int p8[][] = { { 20, 20, 30, 30 }, { 12, 38, 38, 12 } };
int p9[][] = { { 20, 20, 30, 30 }, { 52, 78, 78, 52 } };
polygon = new int[][][] { p1, p2, p3, p4, p5, p6, p7, p8, p9, };
}
/**
* 创建一个数字管的实例。初始化为默认的宽度、高度、前景色、背景色(50,90, red, black)。
* 其中的宽度为单个字符的宽度。
*/
public Digital() {
this(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
/**
* 创建一个数字管的实例。初始化为指定的宽度、高度和默认的前景色、背景色 (red, black)。
* 其中的宽度为单个字符的宽度。
*/
public Digital(int width, int height) {
this(width, height, DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
}
/**
* 创建一个数字管的实例。初始化为指定的宽度、高度、前景色、背景色。 其中的宽度为单个字符的宽度。
*/
public Digital(int width, int height, Color foreground, Color background) {
this.width = width;
this.height = height;
this.foreground = foreground;
this.background = background;
this.gap = 10;
this.offset = 0;
for (int i = 0; i < polygonArray.length; ++i) {
polygonArray[i] = new Polygon(polygon[i][0], polygon[i][1], polygon[i][0].length);
}
resetRatio();
}
/**
* 在指定位置画一个字符串。
* @param g 图形设备
* @param x 起始 X 坐标
* @param y 起始 X 坐标
* @param str 需要绘制的字符串。
*/
public void draw(Graphics g, int x, int y, String str) {
if (str == null || str.equals("")) {
return;
}
int strCount = str.length();
int strWidth = strCount * width + (strCount - 1) * gap + Math.abs(offset);
int rectX = offset > 0 ? x : x + offset;
Color oldColor = g.getColor();
g.setColor(background);
g.fillRect(rectX, y, strWidth, height);
//
for (int i = 0; i < str.length(); ++i) {
drawChar(g, x, y, str.charAt(i));
x += gap + width;
}
g.setColor(oldColor);
}
/**
* 在指定位置画一个字符。
* @param g 图形设备
* @param x 起始 X 坐标
* @param y 起始 X 坐标
* @param str 需要绘制的字符。
*/
public void drawChar(Graphics g, int x, int y, char ch) {
int bits = getBits(ch);
for (int i = 0; i < 9; ++i) {
if (((bits >> i) & 1) != 0) {
drawPloygon(g, polygonArray[i], x, y);
}
}
}
/**
* 重新设置单个字符的宽度
*/
public void setWidth(int w) {
this.width = w;
resetRatio();
}
/**
* 重新设置单个字符的高度
*/
public void setHeight(int h) {
this.height = h;
resetRatio();
}
/**
* 重新设置前景色
*/
public void setForeground(Color fg) {
foreground = fg;
}
/**
* 重新设置背景色
*/
public void setBackground(Color bg) {
background = bg;
}
/**
* 设置字符的倾斜偏移
*/
public void setOffset(int o) {
this.offset = o;
resetRatio();
}
/**
* 设置字符之间的间隔
*/
public void setGap(int gap) {
this.gap = gap;
}
private void drawPloygon(Graphics g, Polygon p1, int x, int y) {
g.translate(x, y);
g.setColor(foreground);
g.fillPolygon(p1);
g.setColor(background);
g.drawPolygon(p1);
g.translate(-x, -y);
}
private int getBits(char ch) {
if (ch >= 'a' && ch <= 'z') {
ch = (char) ('A' + (ch - 'a'));
}
bits[bits.length - 1][0] = ch;
int i = 0;
while (bits[i][0] != ch) {
++i;
}
return bits[i][1];
}
private void resetRatio() {
double wRatio = (double) width / (double) DEFAULT_WIDTH;
double hRatio = (double) height / (double) DEFAULT_HEIGHT;
double offsetRatio = (double) offset / (double) DEFAULT_HEIGHT;
for (int j = 0; j < polygonArray.length; ++j) {
Polygon p = polygonArray[j];
for (int i = 0; i < p.xpoints.length; ++i) {
p.ypoints[i] = (int) (hRatio * polygon[j][1][i]);
p.xpoints[i] = (int) (wRatio * polygon[j][0][i]) + (int) (offsetRatio * (height - p.ypoints[i]));
}
}
}
}
测试程序如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* DigitalTest
*/
public class DigitalTest extends JPanel {
Digital dg1;
Digital dg2;
Digital dg3;
public DigitalTest() {
super();
dg1 = new Digital();
dg1.setGap(15);
dg2 = new Digital(75, 135);
dg3 = new Digital(50, 90, Color.yellow, Color.blue);
dg3.setOffset(15);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// draw digital string;
dg1.draw(g, 0, 0, String.valueOf(1234567890));
dg2.draw(g, 0, 120, "abcdef-:");
dg3.draw(g, 0, 270, "9:24 am");
}
public static void main(String[] args) {
final JFrame frm = new JFrame("digital test");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = frm.getContentPane();
c.setLayout(new BorderLayout());
DigitalTest digital = new DigitalTest();
c.add(digital, "Center");
frm.setSize(700, 500);
frm.setLocationRelativeTo(null);
frm.show();
}
}
自己做了个测试程序,显示结果如下:
以下是程序源代码和测试程序代码。希望大家能给点建议。包括程序结构,功能方面的都很欢迎。
/*-**************************************************************/
* Author : OuJinLiang
* Copyright : SEI.BUAA (2003) .copyright reserved.
* Date : 2004-12-24
/*-**************************************************************/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
/**
* 数码管
*/
public class Digital {
// 单个字符的默认宽度
private static final int DEFAULT_WIDTH = 50;
// 单个字符的默认高度
private static final int DEFAULT_HEIGHT = 90;
// 默认背景色
private static final Color DEFAULT_BACKGROUND = Color.BLACK;
// 默认前景色
private static final Color DEFAULT_FOREGROUND = Color.RED;
private static int bits[][];
private static int polygon[][][];
private Color background;
private Color foreground;
private int width;
private int height;
private int gap;
private int offset;
private Polygon polygonArray[] = new Polygon[9];
static {
bits = new int[][] { { '0', 0x03F }, { '1', 0x018 }, { '2', 0x06E }, { '3', 0x07C }, { '4', 0x059 }, { '5', 0x075 }, { '6', 0x077 }, { '7', 0x01C }, { '8', 0x07F }, { '9', 0x07D }, { 'A', 0x05F }, { 'B', 0x073 }, { 'C', 0x027 }, { 'D', 0x07A }, { 'E', 0x067 }, { 'F', 0x047 }, { 'T', 0x184 }, { 'P', 0x04F }, { 'M', 0x19F }, { ':', 0x180 }, { '-', 0x040 }, { -1, 0x0 }, };
int p1[][] = { { 0, 0, 10, 10 }, { 2, 43, 38, 12 } };
int p2[][] = { { 0, 0, 10, 10 }, { 46, 88, 78, 52 } };
int p3[][] = { { 2, 48, 38, 12 }, { 0, 0, 10, 10 } };
int p4[][] = { { 50, 50, 40, 40 }, { 2, 43, 38, 12 } };
int p5[][] = { { 50, 50, 40, 40 }, { 46, 88, 77, 52 } };
int p6[][] = { { 2, 48, 38, 12 }, { 90, 90, 80, 80 } };
int p7[][] = { { 2, 12, 38, 48, 38, 12 }, { 45, 40, 40, 45, 50, 50 } };
int p8[][] = { { 20, 20, 30, 30 }, { 12, 38, 38, 12 } };
int p9[][] = { { 20, 20, 30, 30 }, { 52, 78, 78, 52 } };
polygon = new int[][][] { p1, p2, p3, p4, p5, p6, p7, p8, p9, };
}
/**
* 创建一个数字管的实例。初始化为默认的宽度、高度、前景色、背景色(50,90, red, black)。
* 其中的宽度为单个字符的宽度。
*/
public Digital() {
this(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
/**
* 创建一个数字管的实例。初始化为指定的宽度、高度和默认的前景色、背景色 (red, black)。
* 其中的宽度为单个字符的宽度。
*/
public Digital(int width, int height) {
this(width, height, DEFAULT_FOREGROUND, DEFAULT_BACKGROUND);
}
/**
* 创建一个数字管的实例。初始化为指定的宽度、高度、前景色、背景色。 其中的宽度为单个字符的宽度。
*/
public Digital(int width, int height, Color foreground, Color background) {
this.width = width;
this.height = height;
this.foreground = foreground;
this.background = background;
this.gap = 10;
this.offset = 0;
for (int i = 0; i < polygonArray.length; ++i) {
polygonArray[i] = new Polygon(polygon[i][0], polygon[i][1], polygon[i][0].length);
}
resetRatio();
}
/**
* 在指定位置画一个字符串。
* @param g 图形设备
* @param x 起始 X 坐标
* @param y 起始 X 坐标
* @param str 需要绘制的字符串。
*/
public void draw(Graphics g, int x, int y, String str) {
if (str == null || str.equals("")) {
return;
}
int strCount = str.length();
int strWidth = strCount * width + (strCount - 1) * gap + Math.abs(offset);
int rectX = offset > 0 ? x : x + offset;
Color oldColor = g.getColor();
g.setColor(background);
g.fillRect(rectX, y, strWidth, height);
//
for (int i = 0; i < str.length(); ++i) {
drawChar(g, x, y, str.charAt(i));
x += gap + width;
}
g.setColor(oldColor);
}
/**
* 在指定位置画一个字符。
* @param g 图形设备
* @param x 起始 X 坐标
* @param y 起始 X 坐标
* @param str 需要绘制的字符。
*/
public void drawChar(Graphics g, int x, int y, char ch) {
int bits = getBits(ch);
for (int i = 0; i < 9; ++i) {
if (((bits >> i) & 1) != 0) {
drawPloygon(g, polygonArray[i], x, y);
}
}
}
/**
* 重新设置单个字符的宽度
*/
public void setWidth(int w) {
this.width = w;
resetRatio();
}
/**
* 重新设置单个字符的高度
*/
public void setHeight(int h) {
this.height = h;
resetRatio();
}
/**
* 重新设置前景色
*/
public void setForeground(Color fg) {
foreground = fg;
}
/**
* 重新设置背景色
*/
public void setBackground(Color bg) {
background = bg;
}
/**
* 设置字符的倾斜偏移
*/
public void setOffset(int o) {
this.offset = o;
resetRatio();
}
/**
* 设置字符之间的间隔
*/
public void setGap(int gap) {
this.gap = gap;
}
private void drawPloygon(Graphics g, Polygon p1, int x, int y) {
g.translate(x, y);
g.setColor(foreground);
g.fillPolygon(p1);
g.setColor(background);
g.drawPolygon(p1);
g.translate(-x, -y);
}
private int getBits(char ch) {
if (ch >= 'a' && ch <= 'z') {
ch = (char) ('A' + (ch - 'a'));
}
bits[bits.length - 1][0] = ch;
int i = 0;
while (bits[i][0] != ch) {
++i;
}
return bits[i][1];
}
private void resetRatio() {
double wRatio = (double) width / (double) DEFAULT_WIDTH;
double hRatio = (double) height / (double) DEFAULT_HEIGHT;
double offsetRatio = (double) offset / (double) DEFAULT_HEIGHT;
for (int j = 0; j < polygonArray.length; ++j) {
Polygon p = polygonArray[j];
for (int i = 0; i < p.xpoints.length; ++i) {
p.ypoints[i] = (int) (hRatio * polygon[j][1][i]);
p.xpoints[i] = (int) (wRatio * polygon[j][0][i]) + (int) (offsetRatio * (height - p.ypoints[i]));
}
}
}
}
测试程序如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* DigitalTest
*/
public class DigitalTest extends JPanel {
Digital dg1;
Digital dg2;
Digital dg3;
public DigitalTest() {
super();
dg1 = new Digital();
dg1.setGap(15);
dg2 = new Digital(75, 135);
dg3 = new Digital(50, 90, Color.yellow, Color.blue);
dg3.setOffset(15);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// draw digital string;
dg1.draw(g, 0, 0, String.valueOf(1234567890));
dg2.draw(g, 0, 120, "abcdef-:");
dg3.draw(g, 0, 270, "9:24 am");
}
public static void main(String[] args) {
final JFrame frm = new JFrame("digital test");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = frm.getContentPane();
c.setLayout(new BorderLayout());
DigitalTest digital = new DigitalTest();
c.add(digital, "Center");
frm.setSize(700, 500);
frm.setLocationRelativeTo(null);
frm.show();
}
}
相关文章推荐
- 汇编语言: 从键盘上输入一串字符(用回车键结束,使用 10 号功能调用。)放在 STRING 中,试 编制一个程序测试字符串中是否存在数字。如有,则把 CL 的第 5 位置 1,否则将该位置置 0。
- 编写程序,输入一位三位整数,把三个数字逆序组成一个新数,再输出
- java 编写程序实现从控制台接收一个 5 位以上的整数,使用数组来判断该数字 * 是否是回文数。(例如:789987,12344321是回文数)
- 一个python小程序,从文本文件中读取每行的两组数字,进行排序后输出.
- 写一个去除AI2XAML注释及多余数字位数的WPF窗体程序
- 使用Olami SDK实现一个语音输入数字进行24点计算的iOS程序
- 43.编写一个程序,判断用户输入的字符是否是数字,若是数字,则输出“a numerical character”
- 编写一个程序,要求接收一个数字,程序会将这个数字以二进制方式打印,例如:数字10 , 以1010的方式打印出来
- 写一个程序判断字符串中数字的位置
- 解决应用程序数字签名问题的一个程序
- arm9板子上写了一个小程序:手写数字及英文大小写字符的识别
- 编写一个程序,使其从标准输入读取字符,直到遇到0字符(注意是0字符,不是数字)
- 编写程序,当程序运行后,根据屏幕提示输入一个数字字符串,输入后统计有多少个偶数数字和奇数数字。
- 编写程序,当程序运行后,根据屏幕提示输入一个数字字符串,输入后统计有多少个偶数数字和奇数数字。
- //1.编写代码模拟三次密码输入的场景。//2.编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出。
- 【程序18】 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时 共有5个数相加),几个数相加有键盘控制
- 编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出。
- 编写一个程序统计输入字符串中: 各个数字、空白字符、以及其他所有字符出现的次数
- 文章标题编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- 3.7 编写一个程序,以字符形式输入某进制的数字,将其转换为十进制数值并显示出