1 | package test;
|
2 |
|
3 | import java.awt.Color;
|
4 | import java.awt.Graphics;
|
5 | import java.awt.Graphics2D;
|
6 | import java.awt.Rectangle;
|
7 | import java.awt.event.ActionEvent;
|
8 | import java.awt.event.ActionListener;
|
9 | import java.awt.geom.Ellipse2D;
|
10 | import java.awt.geom.Line2D;
|
11 |
|
12 | import javax.swing.JComponent;
|
13 | import javax.swing.JFrame;
|
14 | import javax.swing.Timer;
|
15 |
|
16 | public class Clock extends JComponent {
|
17 |
|
18 | private static final long serialVersionUID = 4229018579689301767L;
|
19 |
|
20 | public Clock() {
|
21 | new Timer(1000, new ActionListener() {
|
22 |
|
23 | @Override
|
24 | public void actionPerformed(ActionEvent e) {
|
25 | repaint();
|
26 | }
|
27 | }).start();
|
28 | }
|
29 |
|
30 | @Override
|
31 | protected void paintComponent(Graphics g) {
|
32 | Graphics2D g2 = (Graphics2D) g.create();
|
33 | try {
|
34 | Rectangle visRect = getVisibleRect();
|
35 | g2.setColor(super.getBackground());
|
36 | g2.fill(visRect);
|
37 | paintClock(g2, visRect.width, visRect.height);
|
38 | } finally {
|
39 | g2.dispose();
|
40 | }
|
41 | }
|
42 |
|
43 | private void paintClock(Graphics2D g, int width, int height) {
|
44 | System.out.println("Paint clock (w = " + width + ", h = " + height + ")");
|
45 | g.setColor(Color.BLACK);
|
46 | //Paint the clock...
|
47 | g.draw(new Ellipse2D.Double(0, 0, width, height));
|
48 | g.setColor(Color.RED);
|
49 | g.draw(new Line2D.Double(0, 0, width, height));
|
50 | }
|
51 |
|
52 | public static void main(String[] args) {
|
53 | JFrame frame = new JFrame("clocktest");
|
54 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
55 | frame.setSize(400, 400);
|
56 | frame.add(new Clock());
|
57 | frame.setLocationRelativeTo(null);
|
58 | frame.setVisible(true);
|
59 | }
|
60 | }
|