Using Swing and AWT in Swt

一个awt做的模拟时钟的例子 (代码来源Java2程序设计150例)
很简单,应该很容易看懂,
第二个例子在第一例子的基础上进行了小修改在swt上运行awt,swing。
java 代码
 
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.awt.geom.*;  
import java.util.Calendar;   import javax.swing.*;      public class Clock extends JPanel implements ActionListener   {       // 创建时钟的外形       protected static Ellipse2D face = new Ellipse2D.Float(339494);       // 创建时钟的标记       protected static GeneralPath tick = new GeneralPath();       static       {           tick.moveTo(100100);           tick.moveTo(490);           tick.lineTo(510);           tick.lineTo(516);           tick.lineTo(496);           tick.lineTo(490);       }       // 创建时针       protected static GeneralPath hourHand = new GeneralPath();       static       {           hourHand.moveTo(5015);           hourHand.lineTo(5350);           hourHand.lineTo(5053);           hourHand.lineTo(4750);           hourHand.lineTo(5015);       }       // 创建分针       protected static GeneralPath minuteHand = new GeneralPath();       static       {           minuteHand.moveTo(502);           minuteHand.lineTo(5350);           minuteHand.lineTo(5058);           minuteHand.lineTo(4750);           minuteHand.lineTo(502);       }       // 创建秒针       protected static GeneralPath secondHand = new GeneralPath();       static       {           secondHand.moveTo(495);           secondHand.lineTo(515);           secondHand.lineTo(5162);           secondHand.lineTo(4962);           secondHand.lineTo(495);       }       // 设置时钟的颜色       protected static Color faceColor = new Color(220220220);       protected static Color hourColor = Color.red.darker();       protected static Color minuteColor = Color.blue.darker();       protected static Color secondColor = new Color(1801800);       protected static Color pinColor = Color.gray.brighter();       // 创建时钟的枢纽       protected Ellipse2D pivot = new Ellipse2D.Float(474766);       protected Ellipse2D centerPin = new Ellipse2D.Float(494922);          // 创建绕时钟枢纽转的变换       protected AffineTransform hourTransform =                       AffineTransform.getRotateInstance(05050);       protected AffineTransform minuteTransform =                       AffineTransform.getRotateInstance(05050);       protected AffineTransform secondTransform =                       AffineTransform.getRotateInstance(05050);       // 创建每秒触发一次的Timer       protected Timer timer = new Timer(1000this);       protected Calendar calendar = Calendar.getInstance();          public Clock()       {           setPreferredSize(new Dimension(100100));       }       // 当plane加入container中时发生       public void addNotify()       {           super.addNotify();           timer.start();       }       // 当plane从container中移处时发生       public void removeNotify()       {           timer.stop();           super.removeNotify();       }          public void actionPerformed(ActionEvent event)       {           // 更新calender的时间           this.calendar.setTime(new java.util.Date());           int hours = this.calendar.get(Calendar.HOUR);           int minutes = this.calendar.get(Calendar.MINUTE);           int seconds = this.calendar.get(Calendar.SECOND);              //设置变换,使得时针、分针、秒针各自按绕枢纽旋转一定的角度           hourTransform.setToRotation(((double) hours) *                                               (Math.PI / 6.0), 5050);           minuteTransform.setToRotation(((double) minutes) *                                               (Math.PI / 30.0), 5050);           secondTransform.setToRotation(((double) seconds) *                                               (Math.PI / 30.0), 5050);           repaint();       }          public void paint(Graphics g)       {           super.paint(g);              // 得到图形上下文和抗锯齿处理           Graphics2D g2 = (Graphics2D) g;           g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,                               RenderingHints.VALUE_ANTIALIAS_ON);           g2.setPaint(faceColor);           g2.fill(face);           g2.setPaint(Color.black);           g2.draw(face);              // 产生钟面上12个滴答位置           for (double p = 0.0; p < 12.0; p += 1.0)           {               //利用变换画出同心的滴答的标线               g2.fill(tick.createTransformedShape(                   AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,                           5050)));           }           g2.setPaint(hourColor);           g2.fill(hourHand.createTransformedShape(hourTransform));           g2.setPaint(minuteColor);           g2.fill(minuteHand.createTransformedShape(minuteTransform));           g2.setPaint(secondColor);           g2.fill(secondHand.createTransformedShape(secondTransform));           g2.fill(pivot);           g2.setPaint(pinColor);           g2.fill(centerPin);       }          public static void main(String[] args)       {           JFrame frame = new JFrame();           frame.setLocation(700400);           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           frame.getContentPane().add(new Clock());           frame.pack();           frame.show();       }   }  


我把例子做了一些修改让他在swt的基础上运行。
java 代码
 
import java.awt.Color;   import java.awt.Dimension;   import java.awt.Graphics;   import java.awt.Graphics2D;   import java.awt.RenderingHints;   import java.awt.event.ActionEvent;   import java.awt.event.ActionListener;   import java.awt.geom.AffineTransform;   import java.awt.geom.Ellipse2D;   import java.awt.geom.GeneralPath;   import java.util.Calendar;      import javax.swing.JPanel;   import javax.swing.Timer;      import org.eclipse.swt.SWT;   import org.eclipse.swt.awt.SWT_AWT;   import org.eclipse.swt.layout.FillLayout;   import org.eclipse.swt.widgets.Composite;   import org.eclipse.swt.widgets.Display;   import org.eclipse.swt.widgets.Shell;      public class Swt_awtClock extends JPanel implements ActionListener{              // 创建时钟的外形           protected static Ellipse2D face = new Ellipse2D.Float(339494);           // 创建时钟的标记           protected static GeneralPath tick = new GeneralPath();           static           {               tick.moveTo(100100);               tick.moveTo(490);               tick.lineTo(510);               tick.lineTo(516);               tick.lineTo(496);               tick.lineTo(490);           }           // 创建时针           protected static GeneralPath hourHand = new GeneralPath();           static           {               hourHand.moveTo(5015);               hourHand.lineTo(5350);               hourHand.lineTo(5053);               hourHand.lineTo(4750);               hourHand.lineTo(5015);           }           // 创建分针           protected static GeneralPath minuteHand = new GeneralPath();           static           {               minuteHand.moveTo(502);               minuteHand.lineTo(5350);               minuteHand.lineTo(5058);               minuteHand.lineTo(4750);               minuteHand.lineTo(502);           }           // 创建秒针           protected static GeneralPath secondHand = new GeneralPath();           static           {               secondHand.moveTo(495);               secondHand.lineTo(515);               secondHand.lineTo(5162);               secondHand.lineTo(4962);               secondHand.lineTo(495);           }           // 设置时钟的颜色           protected static Color faceColor = new Color(220220220);           protected static Color hourColor = Color.red.darker();           protected static Color minuteColor = Color.blue.darker();           protected static Color secondColor = new Color(1801800);           protected static Color pinColor = Color.gray.brighter();           // 创建时钟的枢纽           protected Ellipse2D pivot = new Ellipse2D.Float(474766);           protected Ellipse2D centerPin = new Ellipse2D.Float(494922);              // 创建绕时钟枢纽转的变换           protected AffineTransform hourTransform =                           AffineTransform.getRotateInstance(05050);           protected AffineTransform minuteTransform =                           AffineTransform.getRotateInstance(05050);           protected AffineTransform secondTransform =                           AffineTransform.getRotateInstance(05050);           // 创建每秒触发一次的Timer           protected Timer timer = new Timer(1000this);           protected Calendar calendar = Calendar.getInstance();              public Swt_awtClock()           {               setPreferredSize(new Dimension(100100));           }           // 当plane加入container中时发生           public void addNotify()           {               super.addNotify();               timer.start();           }           // 当plane从container中移处时发生           public void removeNotify()           {               timer.stop();               super.removeNotify();           }              public void actionPerformed(ActionEvent event)           {               // 更新calender的时间               this.calendar.setTime(new java.util.Date());               int hours = this.calendar.get(Calendar.HOUR);               int minutes = this.calendar.get(Calendar.MINUTE);               int seconds = this.calendar.get(Calendar.SECOND);                  //设置变换,使得时针、分针、秒针各自按绕枢纽旋转一定的角度               hourTransform.setToRotation(((double) hours) *                                                   (Math.PI / 6.0), 5050);               minuteTransform.setToRotation(((double) minutes) *                                                   (Math.PI / 30.0), 5050);               secondTransform.setToRotation(((double) seconds) *                                                   (Math.PI / 30.0), 5050);               repaint();           }              public void paint(Graphics g)           {               super.paint(g);                  // 得到图形上下文和抗锯齿处理               Graphics2D g2 = (Graphics2D) g;               g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,                                   RenderingHints.VALUE_ANTIALIAS_ON);               g2.setPaint(faceColor);               g2.fill(face);               g2.setPaint(Color.black);               g2.draw(face);                  // 产生钟面上12个滴答位置               for (double p = 0.0; p < 12.0; p += 1.0)               {                   //利用变换画出同心的滴答的标线                   g2.fill(tick.createTransformedShape(                       AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,                               5050)));               }               g2.setPaint(hourColor);               g2.fill(hourHand.createTransformedShape(hourTransform));               g2.setPaint(minuteColor);               g2.fill(minuteHand.createTransformedShape(minuteTransform));               g2.setPaint(secondColor);               g2.fill(secondHand.createTransformedShape(secondTransform));               g2.fill(pivot);               g2.setPaint(pinColor);               g2.fill(centerPin);           }          public static void main(String[] args) {           final Display display = new Display( );           final Shell shell = new Shell(display);           shell.setText("Using Swing and AWT in Swt");              shell.setSize(135145);           shell.setLayout(new FillLayout());              Composite composite = new Composite(shell, SWT.EMBEDDED);               java.awt.Frame frame = SWT_AWT.new_Frame(composite);              frame.setLocation(700400);      //        frame.getContentPane().add(new Swt_awtClock());           java.awt.Panel panel = new java.awt.Panel( );           frame.add(panel);           panel.add(new Swt_awtClock());           frame.pack();           frame.show();              shell.open( );           while(!shell.isDisposed( )) {               if (!display.readAndDispatch( )) display.sleep( );           }           display.dispose( );       }   }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值