import javax.swing.*; import java.awt.*; import java.awt.event.*; class Four { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(400, 400); frame.setTitle("How are you?"); Container c = frame.getContentPane(); c.setLayout(null); Clock c1 = new Clock(50, 1, 33, 100, 200); c1.jumpTo(100, 200); c.add(c1); Five listener = new Five(c1); Timer timer = new Timer(1000, listener); timer.start(); frame.addMouseMotionListener(new Six(c1)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class Six implements MouseMotionListener { Clock c1; Six(Clock c1) { this.c1 = c1; } public void mouseDragged(MouseEvent event) { int x = event.getX(); int y = event.getY(); System.out.println("Mouse dragged at (" + x + ", " + y + ")"); c1.jumpTo(x, y); } public void mouseMoved(MouseEvent event) { } } class Five implements ActionListener { Clock c1; Five (Clock c1) { this.c1 = c1; } public void actionPerformed(ActionEvent event) { c1.tick(); c1.repaint(); } } class Clock extends JComponent { int x, y; int r; int h, m; Clock(int radius, int h, int m, int x, int y) { this.r = radius; this.h = h; this.m = m; this.x = x; this.y = y; } void jumpTo(int x, int y) { this.x = x; this.y = y; this.setBounds(this.x, this.y, 2*this.r+1, 2*this.r+1); } void tick() { this.m += 1; if (this.m == 60) { this.m = 0; this.h += 1; if (this.h == 12) this.h = 0; } } public void paintComponent(Graphics g) { g.setColor(new Color(0.87f, 0.87f, 1.0f)); g.fillOval(0, 0, 2*r, 2*r); g.setColor(Color.blue); g.drawOval(0, 0, 2*r, 2*r); double dx_h, dx_m, dy_h, dy_m; double a_h = ((double)h * 60 + m) / (12 * 60) * 2 * Math.PI; dx_h = 0.66 * r * Math.sin(a_h); dy_h = 0.66 * r * Math.cos(a_h); g.drawLine(r, r, (int)(r+dx_h), (int)(r-dy_h)); double a_m = (double)m / 60 * 2 * Math.PI; dx_m = 0.90 * r * Math.sin(a_m); dy_m = 0.90 * r * Math.cos(a_m); g.drawLine(r, r, (int)(r+dx_m), (int)(r-dy_m)); } }