LED Display Simulation (GUI display in Java)

Source: Internet
Author: User

First, change the color every second.


The jar package generated by this applet: Http://pan.baidu.com/s/1sjFj9ST

The following source code:

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);p rivate color c;private color (Color c) {this.c = C;} Public Color Getmycolor () {return c;}}

2.ledframe.java

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 s Erialversionuid = 1l;public JTextField tf;public JButton bt;public JPanel p;public String num; Ledcanvas canvas = new Ledcanvas (this);p ublic ledframe () {settitle ("LED display simulation"), 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 ("Please enter the number string you want to generate:"), L.sethorizontalalignment (Jlabel.left);p. Add (l), tf = new JTextField;p. Add (TF BT = new JButton ("OK");p. Add (BT);p ack ();} Public String Gettf () {Bt.addactionlistener (new ActionListener () {//final String s; @Overridepublic void ActionpeRformed (ActionEvent e) {num= tf.gettext ();//tf.settext (""); Canvas.repaint ();}}); return num;} public static void Main (string[] args) {new Ledframe (). init ();}}

3.ledcanvas.java

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 {@Overridepublic void actionperformed (ActionEvent e) {c = ColorValue (); rep Aint (1000);}} public void Paint (Graphics g) {font f=new font ("Arial", 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 (CO Lor 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,}};}}}


LED Display Simulation (GUI display in Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.