Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.font.*;
- import java.awt.geom.*;
- import java.math.*;
- public class Clock {
- private double timeHour;
- private double timeMin;
- public Clock(double inputTimeHour, double inputTimeMin)
- {
- timeHour = inputTimeHour;
- timeMin = inputTimeMin;
- }
- public void draw (Graphics2D g2)
- {
- // All hands originate from middle
- int hourXFrom = 200, hourYFrom = 200, minXFrom = 200, minYFrom = 200;
- // Sets all hands to 12:00 to start
- int hourXTo = 200, hourYTo = 50, minXTo = 200, minYTo = 50;
- // Gets hourAngle by dividing 360 by the number of ticks in 12 hours, then multiplying the result by the hours and minutes.
- // Gets minAngle by dividing 360 the number of ticks in an hour, then multiplying the results by the minutes
- //This gives you the angle to rotate the hands to the correct time
- double hourAngle = ((timeHour * 30) + (timeMin * 0.5)), minAngle = (timeMin * 6);
- // Draws the clock outline and centre
- Ellipse2D.Double clockOutline = new Ellipse2D.Double(10, 10, 380, 380 );
- Ellipse2D.Double clockCentre = new Ellipse2D.Double(190, 190, 20, 20);
- // Draws the hour ticks
- g2.setStroke(new BasicStroke(1.0f));
- for(int i=0; i<12; i++)
- {
- g2.drawLine(200, 350, 200, 390);
- g2.rotate(Math.toRadians(30), 200, 200);
- }
- // Draws the minute ticks
- for(int i=0; i<60; i++)
- {
- g2.drawLine(200, 380, 200, 390);
- g2.rotate(Math.toRadians(6), 200, 200);
- }
- // Draw the outline and centre
- g2.draw(clockOutline);
- g2.draw(clockCentre);
- g2.fill(clockCentre);
- // Draws and rotates hour hand
- g2.setStroke(new BasicStroke(4.0f));
- g2.rotate(Math.toRadians(hourAngle), 200, 200);
- g2.draw(new Line2D.Double(hourXFrom, hourYFrom, hourXTo, hourYTo));
- // Draws and rotates minute hand
- g2.setStroke(new BasicStroke(2.0f));
- g2.rotate(Math.toRadians(minAngle - hourAngle), 200, 200);
- g2.draw(new Line2D.Double(minXFrom, minYFrom, minXTo, minYTo));
- /* Debugging
- System.out.println("hour: " + timeHour + " mins: " + timeMin);
- System.out.println(hourAngle + " : " + minAngle );
- System.out.println("x1: " + hourXFrom+ " y1: " + hourXTo + " x2: " + hourYFrom + " y2: " + hourYTo);
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement