- #1
stripes
- 266
- 0
Homework Statement
Very simple: I want to allow my existing Java program to run in a browser. Very simple. I don't want to change any code around, I just want to open it in a browser instead. I have read many tutorials online, and they require that I add weird methods like destroy, init, etc., and I don't want nor need these methods. My application works perfectly fine as it is. I'm very confused as to why I would require any more. My application takes in the time from the user, and then draws a clock that displays the inputted time. Here is my code:
Clock.java
Code:
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
import javax.swing.*;
/**
* Clock class.
*
*/
public class Clock extends JApplet
{
/**two variables used to draw the clock*/
public int minute;
public int hour;
/**Constructor here for clock object. Input is read from the constructor, and then the paint
method uses the public variables that are assigned with the constructor.*/
public Clock(int min, int hr)
{
/**Information is passed from ClockTest class.*/
minute = min;
hour = hr;
}
/**paint method to draw the clock itself*/
public void paint(Graphics g)
{
/**Specifies the dimensions of the circle around the clock, helps determine where
numbers are placed, and centers the clock.*/
final int CIRCLE_START_WIDTH = 50;
final int CIRCLE_START_HEIGHT = 50;
final int CIRCLE_END_WIDTH = 300;
final int CIRCLE_END_HEIGHT = 300;
final int CIRCLE_CENTER_X = CIRCLE_END_WIDTH - (2 * CIRCLE_START_WIDTH);
final int CIRCLE_CENTER_Y = CIRCLE_END_HEIGHT - (2 * CIRCLE_START_HEIGHT);
/**Variables will be used to determine the coordinates of the endpoints of the minute and
hour hands. Start points will be the origin, determined above.*/
int xHourHand, yHourHand, xMinuteHand, yMinuteHand;
/**Set position of the ends of the hands, based on trigonometric mathematical formula*/
xMinuteHand = (int)(Math.cos(minute * Math.PI / 30 - Math.PI / 2)*160
+ CIRCLE_CENTER_X);
yMinuteHand = (int)(Math.sin(minute * Math.PI / 30 - Math.PI / 2)*160
+ CIRCLE_CENTER_Y);
xHourHand = (int)(Math.cos((hour * 30 + minute / 2) * Math.PI / 180 - Math.PI / 2)
* 110 + CIRCLE_CENTER_X);
yHourHand = (int)(Math.sin((hour * 30 + minute / 2) * Math.PI / 180 - Math.PI / 2)
* 110 + CIRCLE_CENTER_Y);
/**Draws the circle. Coordinates were increased slightly to center the circle, due to the
nature of drawing the numbers around the clock.*/
g.drawOval(CIRCLE_START_WIDTH + 4, CIRCLE_START_HEIGHT - 4,
CIRCLE_END_WIDTH, CIRCLE_END_HEIGHT);
/**Draws numbers around the clock*/
g.drawString("1", (int)(Math.cos(5 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(5 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("2", (int)(Math.cos(10 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(10 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
//y-coordinate increased slightly to center the number 3
g.drawString("3", (int)(Math.cos(15 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(15 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) + 4);
g.drawString("4", (int)(Math.cos(20 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(20 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("5", (int)(Math.cos(25 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(25 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
//x-coordinate decreased slightly to center the number 6
g.drawString("6", (int)(Math.cos(30 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) - 2,
(int)(Math.sin(30 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("7", (int)(Math.cos(35 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(35 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("8", (int)(Math.cos(40 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(40 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
//y-coordinate increased slightly to center the number 9
g.drawString("9", (int)(Math.cos(45 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(45 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) + 4);
g.drawString("10", (int)(Math.cos(50 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(50 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
g.drawString("11", (int)(Math.cos(55 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(55 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
//x-coordinate decreased slightly to center the number 12
g.drawString("12", (int)(Math.cos(60 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) - 4,
(int)(Math.sin(60 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
/**Lines are below. Note that extra numbers were added to the coordinates again to center the lines,
due to the nature of the 3, 6, 9, 12 taking up space*/
/**Line for 03:00*/
g.drawLine((int)(Math.cos(15 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) - 35,
(int)(Math.sin(15 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y),
(int)(Math.cos(15 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(15 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
/**Line for 06:00*/
g.drawLine((int)(Math.cos(30 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) ,
(int)(Math.sin(30 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) -45,
(int)(Math.cos(30 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(30 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) - 10);
/**Line for 09:00*/
g.drawLine((int)(Math.cos(45 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) + 8,
(int)(Math.sin(45 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y),
(int)(Math.cos(45 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X) + 42,
(int)(Math.sin(45 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
/**Line for 12:00*/
g.drawLine((int)(Math.cos(60 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(60 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y) + 35,
(int)(Math.cos(60 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_X),
(int)(Math.sin(60 * Math.PI / 30 - Math.PI / 2) * 160 + CIRCLE_CENTER_Y));
/**Hour and minute hands are now drawn*/
g.drawLine(CIRCLE_CENTER_X, CIRCLE_CENTER_Y, xMinuteHand, yMinuteHand);
g.drawLine(CIRCLE_CENTER_X, CIRCLE_CENTER_Y, xHourHand, yHourHand);
}
}
ClockTest.java
Code:
import java.util.*;
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
/**
* ClockTest class.
*
*/
public class ClockTest extends JApplet
{
public static void main(String[] args)
//public ClockTest()
{
/**Creates frame object; will display clock object*/
JFrame frame = new JFrame();
/**Specifies the dimensions of the frame object*/
final int FRAME_WIDTH = 450;
final int FRAME_HEIGHT = 450;
/**Variables here are used for determining what time is to be displayed*/
String time;
int inputHour;
int inputMinute;
/**Sets the frame size, title, and initiates the close operation*/
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Clock Applet");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/**Information window is displayed here, telling the user how the program works.
The next window asks for the time from the user, in the correct format.*/
JOptionPane.showMessageDialog(frame, "This program will display a "
+ "clock with the time you choose to input.");
time = JOptionPane.showInputDialog(frame, "Please enter the time "
+ "(hh:mm):");
/**Exits the program if the user does not enter information. It follows that if the user
presses cancel, the program will exit, since pressing cancel inherently sets the string to
null.*/
if(time == null)
System.exit(0);
/**Creates an error message and allows the user the chance to re-enter the correct information.*/
while(time.charAt(2) != ':' || !(Integer.parseInt(time.substring(0, 1)) >= 0)
|| !(Integer.parseInt(time.substring(0, 2)) < 24)
|| !(Integer.parseInt(time.substring(3)) >= 0)
|| !(Integer.parseInt(time.substring(3)) <= 59)
|| time.length() != 5)
{
/**while loop repeats until correct information is entered, or until the user exits.*/
time = JOptionPane.showInputDialog(frame, "Please enter the time in the correct format(hh:mm):");
}
/**Breaks the input string down into substrings, and then turns each into integer values.*/
inputHour = Integer.parseInt(time.substring(0, 2));
inputMinute = Integer.parseInt(time.substring(3));
/**This if statement is used to simplify calculations and coordinates for the Clock class. It simply
turns the time from 24 hour time to 12 hour time.*/
if(inputHour > 12)
inputHour = inputHour - 12;
/**Clock object is now created, with the desired hour and minute.*/
Clock clock = new Clock(inputMinute, inputHour);
/**Adds the clock to the frame that was created earlier in this class, then makes the frame visible.*/
frame.add(clock);
frame.setVisible(true);
}
public void init()
{
System.out.println("Applet initializing");
display d = new display();
getContentPane().add(d);
}
public void start() {
System.out.println("Applet starting");
}
public void stop() {
System.out.println("Applet stopping");
}
public void destroy()
{
System.out.println("Applet destroyed");
}
}
Homework Equations
The Attempt at a Solution
After reading complicated tutorials, I've concluded that I don't want to complicate my code any further. It is fine the way it is. I just want it to run in a browser. My assignment is due tonight, so I'm not interested in changing anything substantially. If you compile and run ClockTest.java, it works great.
Thanks in advance for your help!