手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Java Swing与线程的结合应用

(三)

[日期:2012-07-21] 来源:Linux社区  作者:gaowen_han [字体:大 中 小]

Java Swing与线程的结合应用(三) :

  1. package com.han;  
  2.   
  3. import java.awt.BasicStroke;  
  4. import java.awt.Color;  
  5. import java.awt.Container;  
  6. import java.awt.Graphics2D;  
  7. import java.util.Random;  
  8.   
  9. import javax.swing.JFrame;  
  10.   
  11. /** 
  12.  * 利用线程在Swing窗口(顶级Container, 与JFrame有区别在于多了标题栏的空间)中画动态线条 
  13.  *  
  14.  * @author HAN 
  15.  *  
  16.  */  
  17. public class ThreadAndSwing_3 extends JFrame {  
  18.   
  19.     /** 
  20.      *  
  21.      */  
  22.     private static final long serialVersionUID = -913556444768132509L;  
  23.   
  24.     static Thread thread;  
  25.     Container container;  
  26.   
  27.     enum Colors {  
  28.         BLACK(Color.BLACK),  
  29.         BLUE(Color.BLUE),  
  30.         CYAN(Color.CYAN),  
  31.         GREEN(Color.GREEN),   
  32.         ORANGE(Color.ORANGE),  
  33.         YELLOW(Color.YELLOW),   
  34.         RED(Color.RED),   
  35.         PINK(Color.PINK),   
  36.         LIGHT_GRAY(Color.LIGHT_GRAY);  
  37.   
  38.         private Color c;  
  39.   
  40.         private Colors(Color c) {  
  41.             this.c = c;  
  42.         }  
  43.   
  44.         public Color getColor() {  
  45.             return this.c;  
  46.         }  
  47.     }  
  48.   
  49.     public ThreadAndSwing_3() {  
  50.         // TODO Auto-generated constructor stub   
  51.         container = getContentPane();  
  52.         container.setLayout(null);  
  53.     }  
  54.   
  55.     void paintJFrame() {  
  56.         thread = new Thread(new Runnable() {  
  57.   
  58.             @Override  
  59.             public void run() {  
  60.                 // TODO Auto-generated method stub   
  61.                 /* 
  62.                  * Creates a graphics context for Container (you can also try it 
  63.                  * for JFrame) 
  64.                  */  
  65.                 Graphics2D g = (Graphics2D) container.getGraphics();  
  66.   
  67.                 Colors[] colors = Colors.values();  
  68.                 Random random = new Random();  
  69.                 int y2 = 30;  
  70.                 float width = 0;  
  71.   
  72.                 while (true) {  
  73.                     try {  
  74.                         Thread.sleep(500);  
  75.                     } catch (InterruptedException e) {  
  76.                         // TODO Auto-generated catch block   
  77.                         e.printStackTrace();  
  78.                     }  
  79.                       
  80.                     /* 
  81.                      * Returns a pseudorandom, uniformly distributed int value 
  82.                      * between 0 (inclusive) and the specified value (exclusive) 
  83.                      */  
  84.                     Color c = colors[random.nextInt(colors.length)].getColor();  
  85.                     // System.out.println(c);   
  86.   
  87.                     g.setColor(c);  
  88.                     g.setStroke(new BasicStroke(width++));  
  89.   
  90.                     /* 
  91.                      * Draws a line, using the current color, between the points 
  92.                      * (x1, y1) and (x2, y2) in this graphics context's 
  93.                      * coordinate system. 
  94.                      */  
  95.                     g.drawLine(30, y2, 100, y2++);  
  96.   
  97.                     if (y2 > 70) {  
  98.                         width = 0;  
  99.                         y2 = 30;  
  100.                     }  
  101.   
  102.                 }  
  103.   
  104.             }  
  105.   
  106.         });  
  107.         thread.start();  
  108.     }  
  109.   
  110.     /** 
  111.      * @param args 
  112.      */  
  113.     public static void main(String[] args) {  
  114.         // TODO Auto-generated method stub   
  115.         ThreadAndSwing_3 frame = new ThreadAndSwing_3();  
  116.         frame.setTitle("利用线程在Swing窗口中画动态线条");  
  117.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  118.         frame.setBounds(100, 100, 300, 200);  
  119.         frame.setVisible(true); // display the frame   
  120.   
  121.         /* 
  122.          * because g will be null if Graphics g = container.getGraphics(); 
  123.          * before the display of the related component 
  124.          */  
  125.         frame.paintJFrame();  
  126.     }  
  127.   
  128. }  
linux
【内容导航】
第1页:(一) 第2页:(二)
第3页:(三)
相关资讯       Java  Swing  Java Swing 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款