Advertisement
Stiepen

Analog Clock

Jul 3rd, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.util.Date;
  4.  
  5. public class Uhr {
  6.  
  7.     JFrame frame = new JFrame();
  8.     Graphics g;
  9.  
  10.     public Uhr() {
  11.         frame.setSize(350, 350);
  12.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.     }
  14.  
  15.     public void run() {
  16.         frame.setVisible(true);
  17.         g = frame.getGraphics();
  18.         int x = 20 + 280 / 2, y = 50 + 280 / 2;
  19.        
  20.         while (true) {
  21.             g.clearRect(0, 0, 350, 350);
  22.             g.drawOval(20, 50, 280, 280);
  23.             Date date = new Date();
  24.  
  25.             int min = date.getMinutes(), sec = date.getSeconds();
  26.             double hr = ((date.getHours() + 1) % 12) + (((double) min) / 60);
  27.             int hr_x = (int) (Math.cos((((2 * Math.PI) / 12) * hr) - 0.5 * Math.PI) / 2 * 140);
  28.             int hr_y = (int) (Math.sin((((2 * Math.PI) / 12) * hr) - 0.5 * Math.PI) / 2 * 140);
  29.             int min_x = (int) (Math.cos((2 * Math.PI / 60 * min) - 0.5 * Math.PI) * 140);
  30.             int min_y = (int) (Math.sin((2 * Math.PI / 60 * min) - 0.5 * Math.PI) * 140);
  31.             int sec_x = (int) (Math.cos((2 * Math.PI / 60 * sec) - 0.5 * Math.PI) * 140);
  32.             int sec_y = (int) (Math.sin((2 * Math.PI / 60 * sec) - 0.5 * Math.PI) * 140);
  33.  
  34.             //System.out.printf("Zeit: %s:%s:%s, Coords: hr: %s,%s; min: %s,%s%n", hr, min, sec, hr_x, hr_y, min_x, min_y);
  35.  
  36.             g.drawLine(x, y, x + hr_x, y + hr_y);
  37.             g.drawLine(x, y, x + min_x, y + min_y);
  38.             g.drawLine(x, y, x + sec_x, y + sec_y);
  39.             try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace(); return;}
  40.         }
  41.  
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement