一個數字顯示管的小程式

來源:互聯網
上載者:User

閑來無事,就喜歡寫寫小的程式,前段時間寫了個數位管的小程式,發現還是很有用處的。
自己做了個測試程式,顯示結果如下:

以下是程式原始碼和測試程式碼。希望大家能給點建議。包括程式結構,功能方面的都很歡迎。

/*-**************************************************************/
 *      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();

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.