Small example of drawing with javaswing---

Source: Internet
Author: User
Tags rand

Javaswing is the necessary knowledge to make desktop programs, but the personal feeling, or a bit, but the effect can be achieved! I used this change before to monitor the computer CPU utilization of the application, this example is still very good!

1 Importjava.awt.BorderLayout;2 ImportJava.awt.EventQueue;3 4 ImportJavax.swing.JFrame;5 ImportJavax.swing.JPanel;6 ImportJavax.swing.border.EmptyBorder;7 8 9 Ten Importjavax.swing.*; One Importjava.awt.*; A Importjava.util.ArrayList; - Importjava.util.Collections; - Importjava.util.List; the ImportJava.util.Random; -  - classDatareceiverextendsJPanel { -     PrivateList<integer> values;//The container that holds the received data. +     Private Static Final intMax_value = 200;//The maximum value of the received data. -     Private Static Final intMax_count_of_values = 50;//the maximum number of data saved. +  A      PublicDatareceiver () { atValues = Collections.synchronizedlist (NewArraylist<integer>()); -  -         //uses a thread to simulate the generation of data. -         NewThread (NewRunnable () { - @Override -              Public voidrun () { inRandom Rand =NewRandom (); -  to                 Try { +                      while(true) { -AddValue (Rand.nextint (max_value));//generates a data and simulates receiving and placing it in a container. the repaint (); *Thread.Sleep (100); $                     }Panax Notoginseng}Catch(interruptedexception e) { - e.printstacktrace (); the                 } +             } A }). Start (); the     } +  - @Override $      Public voidpaintcomponent (Graphics g) { $         Super. paintcomponent (g); -  -Graphics2D g2d =(graphics2d) G; the G2d.setrenderinghint (renderinghints.key_antialiasing, renderinghints.value_antialias_on); - Wuyi         intW =getwidth (); the         inth =getheight (); -         intXDelta = w/max_count_of_values; Wu         intLength =values.size (); -  About          for(inti = 0; i < length-1; ++i) { $G2d.drawline (XDelta * (Max_count_of_values-length +i), Normalizevalueforyaxis (Values.get (i), h), -XDelta * (max_count_of_values-length + i + 1), Normalizevalueforyaxis (Values.get (i + 1) , h)); -         } -     } A  +     /** the * The received data is put into memory. -      * @paramvalue $      */ the     Private voidAddValue (intvalue) { the         //the loop uses a space to receive data. the         //it is best to implement an array of loops, rather than lazy use of ArrayList. the         if(Values.size () >max_count_of_values) { -Values.remove (0); in         } the  the Values.add (value); About     } the  the     /** the * The value of the y-axis direction is regulated. Causes value to be between [0, height] on the y-axis. +      * -      * @paramvalue the      * @paramHeightBayi      * @return the      */ the     Private intNormalizevalueforyaxis (intValueintheight) { -         return(int) ((Double) Height/max_value *value); -     } the  the     Private Static voidcreateguiandshow () { theJFrame frame =NewJFrame (""); the  -Frame.getcontentpane (). Add (Newdatareceiver ()); the  the         //Set Frame ' s close Operation and location in the screen. the frame.setdefaultcloseoperation (jframe.exit_on_close);94Frame.setsize (400, 400); theFrame.setlocationrelativeto (NULL); theFrame.setvisible (true); the     }98  About      Public Static voidMain (string[] args) { - createguiandshow ();101     }102}

Small example of drawing with javaswing---

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.