Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.font.*;
  3. import java.awt.geom.*;
  4. import java.math.*;
  5.  
  6. public class Clock {
  7. private double timeHour;
  8. private double timeMin;
  9.  
  10. public Clock(double inputTimeHour, double inputTimeMin)
  11. {
  12. timeHour = inputTimeHour;
  13. timeMin = inputTimeMin;
  14. }
  15.  
  16.  
  17.  
  18. public void draw (Graphics2D g2)
  19. {
  20.  
  21.  
  22. // All hands originate from middle
  23. int hourXFrom = 200, hourYFrom = 200, minXFrom = 200, minYFrom = 200;
  24.  
  25. // Sets all hands to 12:00 to start
  26. int hourXTo = 200, hourYTo = 50, minXTo = 200, minYTo = 50;
  27.  
  28. // Gets hourAngle by dividing 360 by the number of ticks in 12 hours, then multiplying the result by the hours and minutes.
  29. // Gets minAngle by dividing 360 the number of ticks in an hour, then multiplying the results by the minutes
  30. //This gives you the angle to rotate the hands to the correct time
  31. double hourAngle = ((timeHour * 30) + (timeMin * 0.5)), minAngle = (timeMin * 6);
  32.  
  33. // Draws the clock outline and centre
  34. Ellipse2D.Double clockOutline = new Ellipse2D.Double(10, 10, 380, 380 );
  35. Ellipse2D.Double clockCentre = new Ellipse2D.Double(190, 190, 20, 20);
  36. // Draws the hour ticks
  37. g2.setStroke(new BasicStroke(1.0f));
  38. for(int i=0; i<12; i++)
  39. {
  40. g2.drawLine(200, 350, 200, 390);
  41. g2.rotate(Math.toRadians(30), 200, 200);
  42. }
  43.  
  44. // Draws the minute ticks
  45. for(int i=0; i<60; i++)
  46. {
  47. g2.drawLine(200, 380, 200, 390);
  48. g2.rotate(Math.toRadians(6), 200, 200);
  49. }
  50.  
  51. // Draw the outline and centre
  52. g2.draw(clockOutline);
  53. g2.draw(clockCentre);
  54. g2.fill(clockCentre);
  55.  
  56. // Draws and rotates hour hand
  57. g2.setStroke(new BasicStroke(4.0f));
  58. g2.rotate(Math.toRadians(hourAngle), 200, 200);
  59. g2.draw(new Line2D.Double(hourXFrom, hourYFrom, hourXTo, hourYTo));
  60.  
  61.  
  62. // Draws and rotates minute hand
  63. g2.setStroke(new BasicStroke(2.0f));
  64. g2.rotate(Math.toRadians(minAngle - hourAngle), 200, 200);
  65. g2.draw(new Line2D.Double(minXFrom, minYFrom, minXTo, minYTo));
  66.  
  67. /* Debugging
  68. System.out.println("hour: " + timeHour + " mins: " + timeMin);
  69. System.out.println(hourAngle + " : " + minAngle );
  70. System.out.println("x1: " + hourXFrom+ " y1: " + hourXTo + " x2: " + hourYFrom + " y2: " + hourYTo);
  71. */
  72.  
  73. }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement