//先上效果图,每一秒改变一次颜色
//这个小程序生成的jar包下载地址:http://pan.baidu.com/s/1sjFj9ST
下面上源代码:
//1.color.java
package com.huowolf; import java.awt.Color; enum color{ red(Color.RED), orange(Color.ORANGE),yellow(Color.YELLOW), green(Color.GREEN),gray(Color.GRAY),blue(Color.BLUE), pink(Color.pink),black(Color.BLACK); private Color c; private color(Color c) { this.c = c; } public Color getMyColor() { return c; } }
package com.huowolf; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LEDFrame extends JFrame { private static final long serialVersionUID = 1L; public JTextField tf; public JButton bt; public JPanel p; public String num; LEDCanvas canvas = new LEDCanvas(this); public LEDFrame() { setTitle("LED显示模拟"); setLocation(100,150); setSize(900,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); add(canvas,BorderLayout.CENTER); setVisible(true); } private void init() { p = new JPanel(); add(p,BorderLayout.NORTH); JLabel l = new JLabel("请输入你要生成的数字串:"); l.setHorizontalAlignment(JLabel.LEFT); p.add(l); tf = new JTextField(10); p.add(tf); bt = new JButton("确定"); p.add(bt); pack(); } public String getTF() { bt.addActionListener(new ActionListener() { //final String s; @Override public void actionPerformed(ActionEvent e) { num= tf.getText(); //tf.setText(""); canvas.repaint(); } }); return num; } public static void main(String[] args) { new LEDFrame().init(); } }
package com.huowolf; import java.awt.Canvas; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.Timer; public class LEDCanvas extends Canvas { private static final long serialVersionUID = 7922500886L; int [][]data=null; LEDFrame LEDf = null; public Timer timer; public Color c; public LEDCanvas(LEDFrame f) { this.LEDf = f; ActionListener listener = new TimerListener(); timer = new Timer(1000,listener); timer.start(); } class TimerListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { c = ColorValue(); repaint(1000); } } public void paint(Graphics g){ Font f=new Font("宋体",Font.BOLD,25); g.setFont(f); g.setColor(c); String str = LEDf.getTF(); int pos=20; for(int k=0;str!=null && k<str.length();k++) { char ch = str.charAt(k); print(ch); int y =10; for(int i=0;i<data.length;i++) { int x =pos; x+=20; for(int j=0;j<data[i].length;j++) { //System.out.print(data[i][j]); if(data[i][j]== 1) { g.fillOval(x,y, 12, 16); } x+=15; } //System.out.println(); y += 25; } pos+=100; } } public Color ColorValue(){ Random random = new Random(); int r = random.nextInt(color.values().length); color c = null; for(color c1 : color.values()) { if(c1.ordinal() == r) { //System.out.println(c1.getMyColor()); c = c1; break; } } return c.getMyColor(); } public void print (char c) { if('0'==c) { data = new int[][] {{1,1,1,1,1,}, {1,0,0,0,1,}, {1,0,0,0,1,}, {1,0,0,0,1,}, {1,1,1,1,1,} }; } if('1'==c) { data = new int[][] {{0,0,0,1,0,}, {0,0,0,1,0,}, {0,0,0,1,0,}, {0,0,0,1,0,}, {0,0,0,1,0,} }; } if('2'==c) { data = new int[][] {{1,1,1,1,1,}, {0,0,0,0,1,}, {1,1,1,1,1,}, {1,0,0,0,0,}, {1,1,1,1,1,} }; } if('3'==c) { data = new int[][] {{1,1,1,1,1,}, {0,0,0,0,1,}, {1,1,1,1,1,}, {0,0,0,0,1,}, {1,1,1,1,1,} }; } if('4'==c) { data = new int[][] {{1,0,0,0,1}, {1,0,0,0,1}, {1,1,1,1,1,}, {0,0,0,0,1,}, {0,0,0,0,1,} }; } if('5'==c) { data = new int[][] {{1,1,1,1,1,}, {1,0,0,0,0,}, {1,1,1,1,1,}, {0,0,0,0,1,}, {1,1,1,1,1,} }; } if('6'==c) { data = new int[][] {{1,1,1,1,1,}, {1,0,0,0,0,}, {1,1,1,1,1,}, {1,0,0,0,1,}, {1,1,1,1,1,} }; } if('7'==c) { data = new int[][] {{1,1,1,1,1,}, {0,0,0,0,1,}, {0,0,0,0,1,}, {0,0,0,0,1,}, {0,0,0,0,1,} }; } if('8'==c) { data = new int[][] {{1,1,1,1,1,}, {1,0,0,0,1,}, {1,1,1,1,1,}, {1,0,0,0,1,}, {1,1,1,1,1,} }; } if('9'==c) { data = new int[][] {{1,1,1,1,1,}, {1,0,0,0,1,}, {1,1,1,1,1,}, {0,0,0,0,1,}, {1,1,1,1,1,} }; } } }
原文地址:http://blog.csdn.net/huolang_vip/article/details/45149125