论坛首页 Java企业应用论坛

Using Swing and AWT in Swt

浏览 3632 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-09-22  
OO
一个awt做的模拟时钟的例子(代码来源Java2程序设计150例)
很简单,应该很容易看懂,
第二个例子在第一例子的基础上进行了小修改在swt上运行awt,swing。
java 代码
 
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.awt.geom.*;  
  4. import java.util.Calendar;  
  5. import javax.swing.*;  
  6.   
  7. public class Clock extends JPanel implements ActionListener  
  8. {  
  9.     // 创建时钟的外形  
  10.     protected static Ellipse2D face = new Ellipse2D.Float(339494);  
  11.     // 创建时钟的标记  
  12.     protected static GeneralPath tick = new GeneralPath();  
  13.     static  
  14.     {  
  15.         tick.moveTo(100100);  
  16.         tick.moveTo(490);  
  17.         tick.lineTo(510);  
  18.         tick.lineTo(516);  
  19.         tick.lineTo(496);  
  20.         tick.lineTo(490);  
  21.     }  
  22.     // 创建时针  
  23.     protected static GeneralPath hourHand = new GeneralPath();  
  24.     static  
  25.     {  
  26.         hourHand.moveTo(5015);  
  27.         hourHand.lineTo(5350);  
  28.         hourHand.lineTo(5053);  
  29.         hourHand.lineTo(4750);  
  30.         hourHand.lineTo(5015);  
  31.     }  
  32.     // 创建分针  
  33.     protected static GeneralPath minuteHand = new GeneralPath();  
  34.     static  
  35.     {  
  36.         minuteHand.moveTo(502);  
  37.         minuteHand.lineTo(5350);  
  38.         minuteHand.lineTo(5058);  
  39.         minuteHand.lineTo(4750);  
  40.         minuteHand.lineTo(502);  
  41.     }  
  42.     // 创建秒针  
  43.     protected static GeneralPath secondHand = new GeneralPath();  
  44.     static  
  45.     {  
  46.         secondHand.moveTo(495);  
  47.         secondHand.lineTo(515);  
  48.         secondHand.lineTo(5162);  
  49.         secondHand.lineTo(4962);  
  50.         secondHand.lineTo(495);  
  51.     }  
  52.     // 设置时钟的颜色  
  53.     protected static Color faceColor = new Color(220220220);  
  54.     protected static Color hourColor = Color.red.darker();  
  55.     protected static Color minuteColor = Color.blue.darker();  
  56.     protected static Color secondColor = new Color(1801800);  
  57.     protected static Color pinColor = Color.gray.brighter();  
  58.     // 创建时钟的枢纽  
  59.     protected Ellipse2D pivot = new Ellipse2D.Float(474766);  
  60.     protected Ellipse2D centerPin = new Ellipse2D.Float(494922);  
  61.   
  62.     // 创建绕时钟枢纽转的变换  
  63.     protected AffineTransform hourTransform =  
  64.                     AffineTransform.getRotateInstance(05050);  
  65.     protected AffineTransform minuteTransform =  
  66.                     AffineTransform.getRotateInstance(05050);  
  67.     protected AffineTransform secondTransform =  
  68.                     AffineTransform.getRotateInstance(05050);  
  69.     // 创建每秒触发一次的Timer  
  70.     protected Timer timer = new Timer(1000this);  
  71.     protected Calendar calendar = Calendar.getInstance();  
  72.   
  73.     public Clock()  
  74.     {  
  75.         setPreferredSize(new Dimension(100100));  
  76.     }  
  77.     // 当plane加入container中时发生  
  78.     public void addNotify()  
  79.     {  
  80.         super.addNotify();  
  81.         timer.start();  
  82.     }  
  83.     // 当plane从container中移处时发生  
  84.     public void removeNotify()  
  85.     {  
  86.         timer.stop();  
  87.         super.removeNotify();  
  88.     }  
  89.   
  90.     public void actionPerformed(ActionEvent event)  
  91.     {  
  92.         // 更新calender的时间  
  93.         this.calendar.setTime(new java.util.Date());  
  94.         int hours = this.calendar.get(Calendar.HOUR);  
  95.         int minutes = this.calendar.get(Calendar.MINUTE);  
  96.         int seconds = this.calendar.get(Calendar.SECOND);  
  97.   
  98.         //设置变换,使得时针、分针、秒针各自按绕枢纽旋转一定的角度  
  99.         hourTransform.setToRotation(((double) hours) *  
  100.                                             (Math.PI / 6.0), 5050);  
  101.         minuteTransform.setToRotation(((double) minutes) *  
  102.                                             (Math.PI / 30.0), 5050);  
  103.         secondTransform.setToRotation(((double) seconds) *  
  104.                                             (Math.PI / 30.0), 5050);  
  105.         repaint();  
  106.     }  
  107.   
  108.     public void paint(Graphics g)  
  109.     {  
  110.         super.paint(g);  
  111.   
  112.         // 得到图形上下文和抗锯齿处理  
  113.         Graphics2D g2 = (Graphics2D) g;  
  114.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  115.                             RenderingHints.VALUE_ANTIALIAS_ON);  
  116.         g2.setPaint(faceColor);  
  117.         g2.fill(face);  
  118.         g2.setPaint(Color.black);  
  119.         g2.draw(face);  
  120.   
  121.         // 产生钟面上12个滴答位置  
  122.         for (double p = 0.0; p < 12.0; p += 1.0)  
  123.         {  
  124.             //利用变换画出同心的滴答的标线  
  125.             g2.fill(tick.createTransformedShape(  
  126.                 AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,  
  127.                         5050)));  
  128.         }  
  129.         g2.setPaint(hourColor);  
  130.         g2.fill(hourHand.createTransformedShape(hourTransform));  
  131.         g2.setPaint(minuteColor);  
  132.         g2.fill(minuteHand.createTransformedShape(minuteTransform));  
  133.         g2.setPaint(secondColor);  
  134.         g2.fill(secondHand.createTransformedShape(secondTransform));  
  135.         g2.fill(pivot);  
  136.         g2.setPaint(pinColor);  
  137.         g2.fill(centerPin);  
  138.     }  
  139.   
  140.     public static void main(String[] args)  
  141.     {  
  142.         JFrame frame = new JFrame();  
  143.         frame.setLocation(700400);  
  144.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  145.         frame.getContentPane().add(new Clock());  
  146.         frame.pack();  
  147.         frame.show();  
  148.     }  
  149. }  


我把例子做了一些修改让他在swt的基础上运行。
java 代码
 
  1. import java.awt.Color;  
  2. import java.awt.Dimension;  
  3. import java.awt.Graphics;  
  4. import java.awt.Graphics2D;  
  5. import java.awt.RenderingHints;  
  6. import java.awt.event.ActionEvent;  
  7. import java.awt.event.ActionListener;  
  8. import java.awt.geom.AffineTransform;  
  9. import java.awt.geom.Ellipse2D;  
  10. import java.awt.geom.GeneralPath;  
  11. import java.util.Calendar;  
  12.   
  13. import javax.swing.JPanel;  
  14. import javax.swing.Timer;  
  15.   
  16. import org.eclipse.swt.SWT;  
  17. import org.eclipse.swt.awt.SWT_AWT;  
  18. import org.eclipse.swt.layout.FillLayout;  
  19. import org.eclipse.swt.widgets.Composite;  
  20. import org.eclipse.swt.widgets.Display;  
  21. import org.eclipse.swt.widgets.Shell;  
  22.   
  23. public class Swt_awtClock extends JPanel implements ActionListener{  
  24.   
  25.         // 创建时钟的外形  
  26.         protected static Ellipse2D face = new Ellipse2D.Float(339494);  
  27.         // 创建时钟的标记  
  28.         protected static GeneralPath tick = new GeneralPath();  
  29.         static  
  30.         {  
  31.             tick.moveTo(100100);  
  32.             tick.moveTo(490);  
  33.             tick.lineTo(510);  
  34.             tick.lineTo(516);  
  35.             tick.lineTo(496);  
  36.             tick.lineTo(490);  
  37.         }  
  38.         // 创建时针  
  39.         protected static GeneralPath hourHand = new GeneralPath();  
  40.         static  
  41.         {  
  42.             hourHand.moveTo(5015);  
  43.             hourHand.lineTo(5350);  
  44.             hourHand.lineTo(5053);  
  45.             hourHand.lineTo(4750);  
  46.             hourHand.lineTo(5015);  
  47.         }  
  48.         // 创建分针  
  49.         protected static GeneralPath minuteHand = new GeneralPath();  
  50.         static  
  51.         {  
  52.             minuteHand.moveTo(502);  
  53.             minuteHand.lineTo(5350);  
  54.             minuteHand.lineTo(5058);  
  55.             minuteHand.lineTo(4750);  
  56.             minuteHand.lineTo(502);  
  57.         }  
  58.         // 创建秒针  
  59.         protected static GeneralPath secondHand = new GeneralPath();  
  60.         static  
  61.         {  
  62.             secondHand.moveTo(495);  
  63.             secondHand.lineTo(515);  
  64.             secondHand.lineTo(5162);  
  65.             secondHand.lineTo(4962);  
  66.             secondHand.lineTo(495);  
  67.         }  
  68.         // 设置时钟的颜色  
  69.         protected static Color faceColor = new Color(220220220);  
  70.         protected static Color hourColor = Color.red.darker();  
  71.         protected static Color minuteColor = Color.blue.darker();  
  72.         protected static Color secondColor = new Color(1801800);  
  73.         protected static Color pinColor = Color.gray.brighter();  
  74.         // 创建时钟的枢纽  
  75.         protected Ellipse2D pivot = new Ellipse2D.Float(474766);  
  76.         protected Ellipse2D centerPin = new Ellipse2D.Float(494922);  
  77.   
  78.         // 创建绕时钟枢纽转的变换  
  79.         protected AffineTransform hourTransform =  
  80.                         AffineTransform.getRotateInstance(05050);  
  81.         protected AffineTransform minuteTransform =  
  82.                         AffineTransform.getRotateInstance(05050);  
  83.         protected AffineTransform secondTransform =  
  84.                         AffineTransform.getRotateInstance(05050);  
  85.         // 创建每秒触发一次的Timer  
  86.         protected Timer timer = new Timer(1000this);  
  87.         protected Calendar calendar = Calendar.getInstance();  
  88.   
  89.         public Swt_awtClock()  
  90.         {  
  91.             setPreferredSize(new Dimension(100100));  
  92.         }  
  93.         // 当plane加入container中时发生  
  94.         public void addNotify()  
  95.         {  
  96.             super.addNotify();  
  97.             timer.start();  
  98.         }  
  99.         // 当plane从container中移处时发生  
  100.         public void removeNotify()  
  101.         {  
  102.             timer.stop();  
  103.             super.removeNotify();  
  104.         }  
  105.   
  106.         public void actionPerformed(ActionEvent event)  
  107.         {  
  108.             // 更新calender的时间  
  109.             this.calendar.setTime(new java.util.Date());  
  110.             int hours = this.calendar.get(Calendar.HOUR);  
  111.             int minutes = this.calendar.get(Calendar.MINUTE);  
  112.             int seconds = this.calendar.get(Calendar.SECOND);  
  113.   
  114.             //设置变换,使得时针、分针、秒针各自按绕枢纽旋转一定的角度  
  115.             hourTransform.setToRotation(((double) hours) *  
  116.                                                 (Math.PI / 6.0), 5050);  
  117.             minuteTransform.setToRotation(((double) minutes) *  
  118.                                                 (Math.PI / 30.0), 5050);  
  119.             secondTransform.setToRotation(((double) seconds) *  
  120.                                                 (Math.PI / 30.0), 5050);  
  121.             repaint();  
  122.         }  
  123.   
  124.         public void paint(Graphics g)  
  125.         {  
  126.             super.paint(g);  
  127.   
  128.             // 得到图形上下文和抗锯齿处理  
  129.             Graphics2D g2 = (Graphics2D) g;  
  130.             g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
  131.                                 RenderingHints.VALUE_ANTIALIAS_ON);  
  132.             g2.setPaint(faceColor);  
  133.             g2.fill(face);  
  134.             g2.setPaint(Color.black);  
  135.             g2.draw(face);  
  136.   
  137.             // 产生钟面上12个滴答位置  
  138.             for (double p = 0.0; p < 12.0; p += 1.0)  
  139.             {  
  140.                 //利用变换画出同心的滴答的标线  
  141.                 g2.fill(tick.createTransformedShape(  
  142.                     AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,  
  143.                             5050)));  
  144.             }  
  145.             g2.setPaint(hourColor);  
  146.             g2.fill(hourHand.createTransformedShape(hourTransform));  
  147.             g2.setPaint(minuteColor);  
  148.             g2.fill(minuteHand.createTransformedShape(minuteTransform));  
  149.             g2.setPaint(secondColor);  
  150.             g2.fill(secondHand.createTransformedShape(secondTransform));  
  151.             g2.fill(pivot);  
  152.             g2.setPaint(pinColor);  
  153.             g2.fill(centerPin);  
  154.         }  
  155.   
  156.     public static void main(String[] args) {  
  157.         final Display display = new Display( );  
  158.         final Shell shell = new Shell(display);  
  159.         shell.setText("Using Swing and AWT in Swt");  
  160.   
  161.         shell.setSize(135145);  
  162.         shell.setLayout(new FillLayout());  
  163.   
  164.         Composite composite = new Composite(shell, SWT.EMBEDDED);  
  165.   
  166.          java.awt.Frame frame = SWT_AWT.new_Frame(composite);  
  167.   
  168.         frame.setLocation(700400);  
  169.   
  170. //        frame.getContentPane().add(new Swt_awtClock());  
  171.         java.awt.Panel panel = new java.awt.Panel( );  
  172.         frame.add(panel);  
  173.         panel.add(new Swt_awtClock());  
  174.         frame.pack();  
  175.         frame.show();  
  176.   
  177.         shell.open( );  
  178.         while(!shell.isDisposed( )) {  
  179.             if (!display.readAndDispatch( )) display.sleep( );  
  180.         }  
  181.         display.dispose( );  
  182.     }  
  183. }  
   发表时间:2008-01-11  
恩,关键就只有两行代码:
#         Composite composite = new Composite(shell, SWT.EMBEDDED); 
#  
#          java.awt.Frame frame = SWT_AWT.new_Frame(composite); 
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics