- jiasudu1649
- 等级: 初级会员
- 性别:
- 文章: 41
- 积分: 90
- 来自: 上海
|
一个awt做的模拟时钟的例子(代码来源Java2程序设计150例)
很简单,应该很容易看懂,
第二个例子在第一例子的基础上进行了小修改在swt上运行awt,swing。
java 代码
- import java.awt.*;
- import java.awt.event.*;
- 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(3, 3, 94, 94);
-
- protected static GeneralPath tick = new GeneralPath();
- static
- {
- tick.moveTo(100, 100);
- tick.moveTo(49, 0);
- tick.lineTo(51, 0);
- tick.lineTo(51, 6);
- tick.lineTo(49, 6);
- tick.lineTo(49, 0);
- }
-
- protected static GeneralPath hourHand = new GeneralPath();
- static
- {
- hourHand.moveTo(50, 15);
- hourHand.lineTo(53, 50);
- hourHand.lineTo(50, 53);
- hourHand.lineTo(47, 50);
- hourHand.lineTo(50, 15);
- }
-
- protected static GeneralPath minuteHand = new GeneralPath();
- static
- {
- minuteHand.moveTo(50, 2);
- minuteHand.lineTo(53, 50);
- minuteHand.lineTo(50, 58);
- minuteHand.lineTo(47, 50);
- minuteHand.lineTo(50, 2);
- }
-
- protected static GeneralPath secondHand = new GeneralPath();
- static
- {
- secondHand.moveTo(49, 5);
- secondHand.lineTo(51, 5);
- secondHand.lineTo(51, 62);
- secondHand.lineTo(49, 62);
- secondHand.lineTo(49, 5);
- }
-
- protected static Color faceColor = new Color(220, 220, 220);
- protected static Color hourColor = Color.red.darker();
- protected static Color minuteColor = Color.blue.darker();
- protected static Color secondColor = new Color(180, 180, 0);
- protected static Color pinColor = Color.gray.brighter();
-
- protected Ellipse2D pivot = new Ellipse2D.Float(47, 47, 6, 6);
- protected Ellipse2D centerPin = new Ellipse2D.Float(49, 49, 2, 2);
-
-
- protected AffineTransform hourTransform =
- AffineTransform.getRotateInstance(0, 50, 50);
- protected AffineTransform minuteTransform =
- AffineTransform.getRotateInstance(0, 50, 50);
- protected AffineTransform secondTransform =
- AffineTransform.getRotateInstance(0, 50, 50);
-
- protected Timer timer = new Timer(1000, this);
- protected Calendar calendar = Calendar.getInstance();
-
- public Clock()
- {
- setPreferredSize(new Dimension(100, 100));
- }
-
- public void addNotify()
- {
- super.addNotify();
- timer.start();
- }
-
- public void removeNotify()
- {
- timer.stop();
- super.removeNotify();
- }
-
- public void actionPerformed(ActionEvent event)
- {
-
- 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), 50, 50);
- minuteTransform.setToRotation(((double) minutes) *
- (Math.PI / 30.0), 50, 50);
- secondTransform.setToRotation(((double) seconds) *
- (Math.PI / 30.0), 50, 50);
- 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);
-
-
- for (double p = 0.0; p < 12.0; p += 1.0)
- {
-
- g2.fill(tick.createTransformedShape(
- AffineTransform.getRotateInstance((Math.PI / 6.0) * p,
- 50, 50)));
- }
- 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(700, 400);
- 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(3, 3, 94, 94);
-
- protected static GeneralPath tick = new GeneralPath();
- static
- {
- tick.moveTo(100, 100);
- tick.moveTo(49, 0);
- tick.lineTo(51, 0);
- tick.lineTo(51, 6);
- tick.lineTo(49, 6);
- tick.lineTo(49, 0);
- }
-
- protected static GeneralPath hourHand = new GeneralPath();
- static
- {
- hourHand.moveTo(50, 15);
- hourHand.lineTo(53, 50);
- hourHand.lineTo(50, 53);
- hourHand.lineTo(47, 50);
- hourHand.lineTo(50, 15);
- }
-
- protected static GeneralPath minuteHand = new GeneralPath();
- static
- {
- minuteHand.moveTo(50, 2);
- minuteHand.lineTo(53, 50);
- minuteHand.lineTo(50, 58);
- minuteHand.lineTo(47, 50);
- minuteHand.lineTo(50, 2);
- }
-
- protected static GeneralPath secondHand = new GeneralPath();
- static
- {
- secondHand.moveTo(49, 5);
- secondHand.lineTo(51, 5);
- secondHand.lineTo(51, 62);
- secondHand.lineTo(49, 62);
- secondHand.lineTo(49, 5);
- }
-
- protected static Color faceColor = new Color(220, 220, 220);
- protected static Color hourColor = Color.red.darker();
- protected static Color minuteColor = Color.blue.darker();
- protected static Color secondColor = new Color(180, 180, 0);
- protected static Color pinColor = Color.gray.brighter();
-
- protected Ellipse2D pivot = new Ellipse2D.Float(47, 47, 6, 6);
- protected Ellipse2D centerPin = new Ellipse2D.Float(49, 49, 2, 2);
-
-
- protected AffineTransform hourTransform =
- AffineTransform.getRotateInstance(0, 50, 50);
- protected AffineTransform minuteTransform =
- AffineTransform.getRotateInstance(0, 50, 50);
- protected AffineTransform secondTransform =
- AffineTransform.getRotateInstance(0, 50, 50);
-
- protected Timer timer = new Timer(1000, this);
- protected Calendar calendar = Calendar.getInstance();
-
- public Swt_awtClock()
- {
- setPreferredSize(new Dimension(100, 100));
- }
-
- public void addNotify()
- {
- super.addNotify();
- timer.start();
- }
-
- public void removeNotify()
- {
- timer.stop();
- super.removeNotify();
- }
-
- public void actionPerformed(ActionEvent event)
- {
-
- 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), 50, 50);
- minuteTransform.setToRotation(((double) minutes) *
- (Math.PI / 30.0), 50, 50);
- secondTransform.setToRotation(((double) seconds) *
- (Math.PI / 30.0), 50, 50);
- 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);
-
-
- for (double p = 0.0; p < 12.0; p += 1.0)
- {
-
- g2.fill(tick.createTransformedShape(
- AffineTransform.getRotateInstance((Math.PI / 6.0) * p,
- 50, 50)));
- }
- 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(135, 145);
- shell.setLayout(new FillLayout());
-
- Composite composite = new Composite(shell, SWT.EMBEDDED);
-
- java.awt.Frame frame = SWT_AWT.new_Frame(composite);
-
- frame.setLocation(700, 400);
-
-
- 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( );
- }
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|