闲来无事,就喜欢写写小的程序,前段时间写了个数码管的小程序,发现还是很有用处的: /*-**************************************************************\  *      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); 
        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(); 
    } }
  结果如下:
   
 
  |