JAnalogTime - Orologio.java
Cerca
 











Orologio.java

Caricato da: Paoloricciuti
Scarica il programma completo

  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Graphics;
  4. import java.util.GregorianCalendar;
  5. import javax.swing.JLabel;
  6.  
  7. /*
  8.  * To change this template, choose Tools | Templates
  9.  * and open the template in the editor.
  10.  */
  11. /**
  12.  *
  13.  * @author Paolo
  14.  */
  15. public class Orologio extends JLabel implements Runnable {
  16.  
  17.     private Thread start;
  18.  
  19.     public Orologio() {
  20.         this.start();
  21.         this.setVisible(true);
  22.         this.setLocation(0, 0);
  23.         this.setSize(100, 100);
  24.     }
  25.  
  26.     public void start() {
  27.         this.start = new Thread(this);
  28.         this.start.start();
  29.     }
  30.  
  31.     public void stop() {
  32.         this.start.stop();
  33.     }
  34.  
  35.     @Override
  36.     protected void paintComponent(Graphics g) {
  37.         int angolo = 360 / 12;
  38.         int angolosecmin = 360 / 60;
  39.         GregorianCalendar cal = new GregorianCalendar();
  40.         int sec = cal.get(GregorianCalendar.SECOND);
  41.         int min = cal.get(GregorianCalendar.MINUTE);
  42.         int ore = cal.get(GregorianCalendar.HOUR);
  43.         int angolosec = sec * angolosecmin - 90;
  44.         int angolomin = min * angolosecmin - 90;
  45.         int angoloore = ore * angolo - 90;
  46.         Angle secondi = new Angle(angolosec);
  47.         Angle minuti = new Angle(angolomin);
  48.         Angle hour = new Angle(angoloore);
  49.         int lunghezza = this.getSize().width / 2;
  50.         int secX = (int) (secondi.getX() * lunghezza) + lunghezza;
  51.         int secY = (int) (secondi.getY() * lunghezza) + lunghezza;
  52.         int minX = (int) (minuti.getX() * lunghezza) + lunghezza;
  53.         int minY = (int) (minuti.getY() * lunghezza) + lunghezza;
  54.         int oreX = (int) (hour.getX() * (lunghezza - (lunghezza / 3))) + lunghezza;
  55.         int oreY = (int) (hour.getY() * (lunghezza - (lunghezza / 3))) + lunghezza;
  56.         g.setColor(Color.WHITE);
  57.         g.fillOval(0, 0, lunghezza * 2, lunghezza * 2);
  58.         g.setColor(Color.BLACK);
  59.         g.drawOval(0, 0, lunghezza * 2, lunghezza * 2);
  60.         g.setColor(Color.RED);
  61.         g.drawLine(lunghezza, lunghezza, secX, secY);
  62.         g.setColor(Color.BLACK);
  63.         g.drawLine(lunghezza, lunghezza, minX, minY);
  64.         g.drawLine(lunghezza, lunghezza, oreX, oreY);
  65.         super.paintComponent(g);
  66.     }
  67.  
  68.     @Override
  69.     public void setSize(int width, int height) {
  70.         if (width > height) {
  71.             width = height;
  72.         } else if (height > width) {
  73.             height = width;
  74.         }
  75.         super.setSize(width, height);
  76.     }
  77.  
  78.     @Override
  79.     public void setSize(Dimension dimension) {
  80.         this.setSize(dimension.width, dimension.height);
  81.     }
  82.  
  83.     public void run() {
  84.         while (true) {
  85.             this.repaint();
  86.             try {
  87.                 Thread.sleep(1000);
  88.             } catch (InterruptedException ex) {
  89.             }
  90.         }
  91.     }
  92. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.