Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.util.Date;
- public class Uhr {
- JFrame frame = new JFrame();
- Graphics g;
- public Uhr() {
- frame.setSize(350, 350);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void run() {
- frame.setVisible(true);
- g = frame.getGraphics();
- int x = 20 + 280 / 2, y = 50 + 280 / 2;
- while (true) {
- g.clearRect(0, 0, 350, 350);
- g.drawOval(20, 50, 280, 280);
- Date date = new Date();
- int min = date.getMinutes(), sec = date.getSeconds();
- double hr = ((date.getHours() + 1) % 12) + (((double) min) / 60);
- int hr_x = (int) (Math.cos((((2 * Math.PI) / 12) * hr) - 0.5 * Math.PI) / 2 * 140);
- int hr_y = (int) (Math.sin((((2 * Math.PI) / 12) * hr) - 0.5 * Math.PI) / 2 * 140);
- int min_x = (int) (Math.cos((2 * Math.PI / 60 * min) - 0.5 * Math.PI) * 140);
- int min_y = (int) (Math.sin((2 * Math.PI / 60 * min) - 0.5 * Math.PI) * 140);
- int sec_x = (int) (Math.cos((2 * Math.PI / 60 * sec) - 0.5 * Math.PI) * 140);
- int sec_y = (int) (Math.sin((2 * Math.PI / 60 * sec) - 0.5 * Math.PI) * 140);
- //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);
- g.drawLine(x, y, x + hr_x, y + hr_y);
- g.drawLine(x, y, x + min_x, y + min_y);
- g.drawLine(x, y, x + sec_x, y + sec_y);
- try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace(); return;}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement