Dwinanda

Clock (Java Applet)

Dec 14th, 2020 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.35 KB | None | 0 0
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.applet.*;
  4. import java.text.*;
  5. /**
  6. * Program Jam analog
  7. *
  8. * @author Dwinanda Bagoes Ansori
  9. * @version 15 December 2020
  10. */
  11. public class Clock extends Applet implements Runnable {
  12.     private volatile Thread timer;
  13.     private int lastxs, lastys, lastxm, lastym, lastxh, lastyh;
  14.     private SimpleDateFormat formatter;
  15.     private String lastdate;
  16.     private Font clockFaceFont;
  17.     private Date currentDate;
  18.     private Color handColor;
  19.     private Color numberColor;
  20.     private int xcenter = 80, ycenter = 55;
  21.  
  22.     public void init()
  23.     {
  24.         int x,y;
  25.         lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
  26.         formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy",
  27.         Locale.getDefault());
  28.         currentDate = new Date();
  29.         lastdate = formatter.format(currentDate);
  30.         clockFaceFont = new Font("Serif", Font.PLAIN, 14);
  31.         handColor = Color.green;
  32.         numberColor = Color.black;
  33.  
  34.         try
  35.         {
  36.             setBackground(new Color(Integer.parseInt(getParameter("bgcolor"), 16)));
  37.         }
  38.         catch (NullPointerException e) { }
  39.         catch (NumberFormatException e) { }
  40.         try
  41.         {
  42.             handColor = new Color(Integer.parseInt(getParameter("fgcolor1"), 16));
  43.         }
  44.         catch (NullPointerException e) { }
  45.         catch (NumberFormatException e) { }
  46.         try
  47.         {
  48.             numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"), 16));
  49.         }
  50.         catch (NullPointerException e) { }
  51.         catch (NumberFormatException e) { }
  52.         resize(300,300); // Set clock window size
  53.     }
  54.     public void update(Graphics g)
  55.     {
  56.         int xh, yh, xm, ym, xs, ys;
  57.         int s = 0, m = 10, h = 10;
  58.         String today;
  59.      
  60.         currentDate = new Date();
  61.      
  62.         formatter.applyPattern("s");
  63.         try
  64.         {
  65.             s = Integer.parseInt(formatter.format(currentDate));
  66.         }
  67.         catch (NumberFormatException n)
  68.         {
  69.             s = 0;
  70.         }
  71.         formatter.applyPattern("m");
  72.         try
  73.         {
  74.             m = Integer.parseInt(formatter.format(currentDate));
  75.         }
  76.         catch (NumberFormatException n)
  77.         {
  78.             m = 10;
  79.         }
  80.         formatter.applyPattern("h");
  81.         try
  82.         {
  83.             h = Integer.parseInt(formatter.format(currentDate));
  84.         }
  85.         catch (NumberFormatException n)
  86.         {
  87.             h = 10;
  88.         }
  89.      
  90.         // Set position of the ends of the hands
  91.         xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
  92.         ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
  93.         xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
  94.         ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
  95.         xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + xcenter);
  96.         yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + ycenter);
  97.      
  98.         // Get the date to print at the bottom
  99.         formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
  100.         today = formatter.format(currentDate);
  101.      
  102.         g.setFont(clockFaceFont);
  103.         // Erase if necessary
  104.         g.setColor(getBackground());
  105.         if (xs != lastxs || ys != lastys)
  106.         {
  107.             g.drawLine(xcenter, ycenter, lastxs, lastys);
  108.             g.drawString(lastdate, 5, 125);
  109.         }
  110.         if (xm != lastxm || ym != lastym)
  111.         {
  112.             g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  113.             g.drawLine(xcenter-1, ycenter, lastxm, lastym);
  114.         }
  115.         if (xh != lastxh || yh != lastyh)
  116.         {
  117.             g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  118.             g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
  119.         }
  120.      
  121.         // Draw date and hands
  122.         g.setColor(numberColor);
  123.         g.drawString(today, 5, 125);
  124.         g.drawLine(xcenter, ycenter, xs, ys);
  125.         g.setColor(handColor);
  126.         g.drawLine(xcenter, ycenter-1, xm, ym);
  127.         g.drawLine(xcenter-1, ycenter, xm, ym);
  128.         g.drawLine(xcenter, ycenter-1, xh, yh);
  129.         g.drawLine(xcenter-1, ycenter, xh, yh);
  130.         lastxs = xs; lastys = ys;
  131.         lastxm = xm; lastym = ym;
  132.         lastxh = xh; lastyh = yh;
  133.         lastdate = today;
  134.         currentDate = null;
  135.     }
  136.     // Paint is the main part of the program
  137.     public void paint(Graphics g)
  138.     {
  139.         g.setFont(clockFaceFont);
  140.         // Draw the circle and numbers
  141.         g.setColor(handColor);
  142.         g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);
  143.         g.setColor(numberColor);
  144.         g.drawString("9", xcenter-45, ycenter+3);
  145.         g.drawString("3", xcenter+40, ycenter+3);
  146.         g.drawString("12", xcenter-5, ycenter-37);
  147.         g.drawString("6", xcenter-3, ycenter+45);
  148.      
  149.         // Draw date and hands
  150.         g.setColor(numberColor);
  151.         g.drawString(lastdate, 5, 125);
  152.         g.drawLine(xcenter, ycenter, lastxs, lastys);
  153.         g.setColor(handColor);
  154.         g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  155.         g.drawLine(xcenter-1, ycenter, lastxm, lastym);
  156.         g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  157.         g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
  158.     }
  159.     public void start()
  160.     {
  161.         timer = new Thread(this);
  162.         timer.start();
  163.     }
  164.     public void stop()
  165.     {
  166.         timer = null;
  167.     }
  168.     public void run()
  169.     {
  170.         Thread me = Thread.currentThread();
  171.         while (timer == me)
  172.         {
  173.             try
  174.             {
  175.                 Thread.currentThread().sleep(100);
  176.             }
  177.             catch (InterruptedException e)
  178.             {
  179.             }
  180.             repaint();
  181.         }
  182.     }
  183.     public String getAppletInfo()
  184.     {
  185.         return "Title: Jam \n" + "Author: Dwinanda Bagoes Ansori, 2020 \n" + "menampilkan jam analog";
  186.     }
  187.     public String[][] getParameterInfo()
  188.     {
  189.         String[][] info =
  190.         {
  191.             {"bgcolor", "hexadecimal RGB number", "Warna background adalah warna dari default browser."},
  192.             {"fgcolor1", "hexadecimal RGB number", "Warna jarum pada jam adalah hijau."},
  193.             {"fgcolor2", "hexadecimal RGB number", "Warna jarum detik dan angka pada jam adalah hitam."}
  194.         };
  195.         return info;
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment