Comp Sci Turning a Java program into an applet

Click For Summary
To run a Java program as an applet in a browser, the existing code needs to be modified to include HTML for embedding the applet. The user expresses frustration with the requirement to add methods like init and destroy, preferring to keep the code unchanged. They have successfully compiled and run the program but struggle with displaying the clock in the browser instead of a new frame. The conversation highlights the need for proper HTML code to embed the applet, indicating a lack of clarity on how to achieve this. Ultimately, the user seeks a straightforward solution to display their clock applet directly in the browser.
stripes
Messages
262
Reaction score
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!
 
Physics news on Phys.org
Okay, so I have since figured out how to get some of it working in a browser. My question now is: how do i get the CLOCK to show up in the browser? Like it keeps showing up in a new frame that opens up. Instead of frame.add, what is the browser.add method that I want? I don't want to add the clock to the frame, but rather, to the BROWSER. What is the short code for this?
 
I'm going by some memories of coding Java applets about 17 years ago, so caveat emptor. To have your applet show up in your browser, you need some HTML code for the applet.

The code below comes from the wiki page on Java applets, http://en.wikipedia.org/wiki/Java_applet.
Code:
<!DOCTYPE HTML PUBLIC 
  "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<HTML>
<HEAD>
<TITLE>HelloWorld_example.html</TITLE>
</HEAD>
<BODY>
<H1>A Java applet example</H1>
<P>Here it is: <APPLET code="HelloWorld.class" WIDTH="200" HEIGHT="40">
This is where HelloWorld.class runs.</APPLET></P>
</BODY>
</HTML>

Hope that helps.
 
I've gotten that far. Unfortunately I cannot seem to find a code that will display everything in a browser. Very frustrating.

This is the exact reason why I'll never do this for a living. One minute my code works, the next, it doesn't. Hilarious.

Thanks anyways.
 
I would take a close look at the documentation for JFrame. Possibly if you do your drawing in the root pane, that will make your code do what you want.
 
Yeah, that's where I think the issue is. I don't even think I needed any JFrame at all, as I didn't want the applet to open in a new frame. I just couldn't figure out how to put the clock in the browser window, instead of opening a new frame. Oh well, I believe it was only one mark for correctly implementing the HTML component, and since I did implement it in HTML correctly aside from the new frame, I might even get .5 mark for that. Thanks for your help anyways.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 0 ·
Replies
0
Views
617
  • · Replies 20 ·
Replies
20
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K