RickyS20_

Untitled

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