java绘制点阵数字(LED数字显示),转载源码

来源:互联网 发布:it外包平台 编辑:程序博客网 时间:2024/06/04 19:51

看到一个扫雷例子的源码,学习了下绘制LED数字显示的方法,感觉挺好,做笔记记录下

 

import java.awt.Color;import java.awt.Component;import java.awt.Graphics;import java.awt.Image;import java.awt.Polygon;import java.awt.image.BufferedImage;public class LedNumber extends Component {private Polygon segmentPolygon[];private int numberSegment[][] = { { 0, 1, 2, 3, 4, 5 }, // 0{ 1, 2 }, // 1{ 0, 1, 3, 4, 6 }, // 2{ 0, 1, 2, 3, 6 }, // 3{ 1, 2, 5, 6 }, // 4{ 0, 2, 3, 5, 6 }, // 5{ 0, 2, 3, 4, 5, 6 }, // 6{ 0, 1, 2 }, // 7{ 0, 1, 2, 3, 4, 5, 6 }, // 8{ 0, 1, 2, 3, 5, 6 } // 9};    private int div[] = {1,10,100,1000,10000,100000};    private Image numberImage[];    private Color fontColor = Color.red;   //the color of number    private Color bgColor = Color.black;    private Color maskColor = Color.darkGray;    private int dWidth = 12;    private int dHeight = 21;        public LedNumber(){    init();    }        public LedNumber(Color fc){    fontColor=fc;    init();    }        public LedNumber(Color fc,Color bgc){    fontColor=fc;    bgColor=bgc;    init();    }        public LedNumber(Color fc,Color bgc,Color mc){    fontColor=fc;    bgColor=bgc;    maskColor=mc;    init();    }               public void setBackGround(Color bgc){    this.bgColor=bgc;    }        public void setFontColor(Color fc){    this.fontColor=fc;    }        public void setMaskColor(Color mc){    this.maskColor=mc;    }        public void setDigitalWidth(int dWidth){    this.dWidth=dWidth;    }        public void setDigitalHeight(int dHeight){    this.dHeight=dHeight;    }           public Image getLedImage(int dg,int bound){    dg%=div[bound];    Image image=new BufferedImage(dWidth*bound,dHeight,BufferedImage.TYPE_INT_RGB);    Graphics g=image.getGraphics();    bound--;    for (int i = bound; i >= 0; i--) {g.drawImage(numberImage[dg / div[i]], (bound - i) * dWidth, 0, this);dg %= div[i];}return image;    }        public void init(){    segmentPolygon=new Polygon[7];    numberImage=new Image[10];    setNumberPolygon();    setNumberImage();        }           public void setNumberImage(){    int i=0;    int j=0;    int k;    Graphics g;    while(i<10){    numberImage[i]=new BufferedImage(15,20,BufferedImage.TYPE_INT_RGB);    g=numberImage[i].getGraphics();    g.setColor(bgColor);    g.fillRect(0, 0, 15, 20);    g.setColor(Color.DARK_GRAY);    j=0;    while(j<numberSegment[8].length){    k=numberSegment[8][j];    g.fillPolygon(segmentPolygon[k]);    j++;    }    g.setColor(fontColor);    j=0;    while(j<numberSegment[i].length){    k=numberSegment[i][j];    g.fillPolygon(segmentPolygon[k]);    j++;    }    i++;    }    }        public void setNumberPolygon(){    int mid=dHeight/2+1;    segmentPolygon[0]=new Polygon();    segmentPolygon[0].addPoint(2, 1);    segmentPolygon[0].addPoint(dWidth-2, 1);    segmentPolygon[0].addPoint(dWidth-5, 4);    segmentPolygon[0].addPoint(4, 4);    segmentPolygon[1]=new Polygon();    segmentPolygon[1].addPoint(dWidth-1, 1);    segmentPolygon[1].addPoint(dWidth-1, mid-1);    segmentPolygon[1].addPoint(dWidth-2, mid-1);    segmentPolygon[1].addPoint(dWidth-4, mid-3);    segmentPolygon[1].addPoint(dWidth-4, 4);    segmentPolygon[2] = new Polygon();        segmentPolygon[2].addPoint(dWidth-1, mid);        segmentPolygon[2].addPoint(dWidth-1, dHeight-2);        segmentPolygon[2].addPoint(dWidth-4, dHeight-5);        segmentPolygon[2].addPoint(dWidth-4, mid+1);        segmentPolygon[2].addPoint(dWidth-3, mid);        segmentPolygon[3] = new Polygon();        segmentPolygon[3].addPoint(dWidth-2, dHeight-1);        segmentPolygon[3].addPoint(1, dHeight-1);        segmentPolygon[3].addPoint(4, dHeight-4);        segmentPolygon[3].addPoint(dWidth-4, dHeight-4);        segmentPolygon[4] = new Polygon();        segmentPolygon[4].addPoint(1, dHeight-2);        segmentPolygon[4].addPoint(1, mid);        segmentPolygon[4].addPoint(3, mid);        segmentPolygon[4].addPoint(4, mid+1);        segmentPolygon[4].addPoint(4, dHeight-5);        segmentPolygon[5] = new Polygon();        segmentPolygon[5].addPoint(1, mid-1);        segmentPolygon[5].addPoint(1, 1);        segmentPolygon[5].addPoint(4, 4);        segmentPolygon[5].addPoint(4, mid-3);        segmentPolygon[5].addPoint(2, mid-1);        segmentPolygon[6] = new Polygon();        segmentPolygon[6].addPoint(3, mid-1);        segmentPolygon[6].addPoint(4, mid-2);        segmentPolygon[6].addPoint(dWidth-4, mid-2);        segmentPolygon[6].addPoint(dWidth-3, mid-1);        segmentPolygon[6].addPoint(dWidth-5, mid+1);        segmentPolygon[6].addPoint(4, mid+1);        }    }


 

测试代码:

 

import java.awt.BorderLayout;import javax.swing.BorderFactory;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.SwingUtilities;import com.hx.t.saolei.mine.LedNumber;public class Test2 extends JFrame {public static void main(String[] args) {new Test2();}private JPanel jp1;private JLabel jl1;private LedNumber led;private int timepassed;public TimeThread timeThread;public Test2() {setTitle("Led Number");setSize(300, 300);timepassed = 0;timeThread = new TimeThread();led = new LedNumber();jp1 = new JPanel();jl1 = new JLabel();jl1.setBorder(BorderFactory.createLoweredBevelBorder());jl1.setIcon(new ImageIcon(led.getLedImage(timepassed, 5)));jp1.add(jl1);this.add(jp1, BorderLayout.NORTH);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);timeThread.start();}class TimeThread extends Thread {public TimeThread() {}@Overridepublic void run() {while (timepassed < 1000 && Thread.currentThread() == timeThread) {SwingUtilities.invokeLater(new Runnable() {@Overridepublic void run() {jl1.setIcon(new ImageIcon(led.getLedImage(timepassed, 5)));}});try {Thread.sleep(999);} catch (InterruptedException e) {System.out.println("interupped");e.printStackTrace();}timepassed++;}}}}

 

通过Polygon画出来的1-9数字的LED点阵显示,通过这样做法,可以做出日常板子上显示的数字样式,也可以画出其他的样式,不过元素少些还好处理,也就处理些数字类的吧,不过举一反三,字母、特殊字符也可做出了,基本就是苦力活了

运行效果:

LED数字结构