Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*******I actually have a lot of code to this which can be located at the bottom

ID: 3623275 • Letter: #

Question

*******I actually have a lot of code to this which can be located at the bottom of the question. I have gotten most of it. I need help getting the hands which represent the sec min and hrs. This is supposed to be an intro to Java.


* The applet should display the circular dial of a analog clock, with a second hand, minute hand, and an hour hand in the correct position. The hour and minute hands should be shown in proportion to the actual time. For example, if the time is 3:30:45, the hour hand should be about halfway between 3pm and 4pm. Similarly for the minute hand.

* The dial should be centered on the panel and should scale in size with the panel. The hour, minute and second hands should also scale with the size of the dial.

* The dial should have markers for hours at 3, 6 , 9 and 12.

You will need to find the angles of the hours, minutes and seconds hand. The seconds and minutes hand travels 360 degrees in 60 minutes. The hours hand travels 360 degrees in 12 hours. Once you have the angles (in the appropriate trigonometric units), then you will have to calculate the coordinates of the endpoints of the seconds, minutes and hours hands. Knowing the endpoints and the center of the dial allows you to draw the hands as lines.

Useful methods:

* Math class: Math.sin(..), Math.cos(..), Math.toRadians(...), Math.round(...).
* Graphics class: drawLine, drawOval, fillOval, setColor

Note that applet is animated. So after you set the time, it will start ticking once per second. The code to animate the applet is also being provided to you in the template.

The following image shows an example clock face. The hours hand is in red, the minutes hand is in blue and the seconds hand is in pink.


// MY CODE


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.JApplet;


/**
An animated analog clock.

@author
*/

public class Clock extends JApplet
{
private int hour;
private int minute;
private int second;
private final int delay = 1000; //milliseconds

/**
Ask for time input via a dialog box and parse the time input.
@param none
@return void
*/
public void init()
{
hour = 0;
minute = 0;
second = 0;


getTimeFromUser();
startAnimation();
}

/**
Draw the face of the analog clock for current hour and minute.
@param g Graphics context
@return none
*/
public void paint(Graphics g)
{

int width = getWidth();
int height = getHeight();

int midx = width/2;
int midy = height/2;
int midx2 = width/3;
int midy2 = width/3;

// set the radius to be 1/4th of the smaller of width and height.
int radius = Math.min(width, height)/4;


g.setColor(Color.black);
g.drawOval(midx - radius, midy - radius, 2*radius + 1, 2*radius + 1);
g.drawOval(midx - radius, midy - radius, 2*radius + 2, 2*radius + 2);
g.drawOval(midx - radius, midy - radius, 2*radius + 3, 2*radius + 3);
g.drawOval((midx - radius) -1, (midy - radius) -1, 2*radius + 4, 2*radius + 4);
g.drawOval((midx - radius) - 2, (midy - radius) -2 , 2*radius + 5, 2*radius + 5);
g.drawOval((midx - radius) - 2, (midy - radius) -2, 2*radius + 6, 2*radius + 6);
g.drawOval((midx - radius) -3, (midy - radius) -3, 2*radius + 6, 2*radius + 6);
g.drawString(hour + ":" + minute + ":" + second, midx, midy);


String twelve = "12";
int fontWidth = g.getFontMetrics().stringWidth(twelve);
// center the label on the top
g.drawString(twelve, (width - fontWidth)/2, midy - radius + 15);

String six = "6";
int fontWidthSix = g.getFontMetrics().stringWidth(six);
// center the label on the top
g.drawString(six, (width - fontWidthSix)/2, midy + radius - 5);

String three = "3";
// center the label on the top
g.drawString(three, midx - radius + 5, midy);

String nine = "9";
// center the label on the top
g.drawString(nine, midx + radius - 10, midy);

//doing this to make the numbers thicker
//just slightly moving the position of the number

String twelve2 = "12";
int fontWidth2 = g.getFontMetrics().stringWidth(twelve);
// center the label on the top
g.drawString(twelve2, (width - fontWidth2)/2, midy - radius + 14);

String six2 = "6";
int fontWidthSix2 = g.getFontMetrics().stringWidth(six);
// center the label on the top
g.drawString(six2, (width - fontWidthSix2)/2, midy + radius - 4);

String three2 = "3";
// center the label on the top
g.drawString(three2, midx - radius + 4, midy);

String nine2 = "9";
// center the label on the top
g.drawString(nine2, midx + radius - 9, midy);


}


/**
* Create an animation thread that runs once per second
*/
private void startAnimation()
{
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ticktock();
repaint();
}
};
new Timer(delay, taskPerformer).start();
}

/**
* Increment time by one second and adjust time accordingly.
*/
private void ticktock()
{
second = second + 1;
if (second == 60) {
second = 0;
minute = minute + 1;
if (minute == 60) {
minute = 0;
hour = hour + 1;
if (hour == 12)
hour = 0;
}
}
}

/**
* Ask user to input time to set the clock by a pop-up dialog box.
* Assume that hours are in the range 0..11, minutes in the range 0..59
* and seconds in the range 0..59.
*/
private void getTimeFromUser()
{
String input;

input = JOptionPane.showInputDialog("time (hh:mm:ss):");
if (input == null)
System.out.println("No input");
else
{
try
{
int index1 = input.indexOf(":");
hour = Integer.parseInt(input.substring(0,index1));
int index2 = input.indexOf(":",index1+1);
minute = Integer.parseInt(input.substring(index1+1,index2));
second = Integer.parseInt(input.substring(index2+1,input.length()));
}
catch (NumberFormatException e)
{
System.out.println(e);
}
}
}

}

Explanation / Answer

Well, I just think, for starters, the government has things they can't tell us, and truthishly, we should just accept that.