Turning a Java program into an applet

  • Context: Comp Sci 
  • Thread starter Thread starter stripes
  • Start date Start date
  • Tags Tags
    Java Program Turning
Click For Summary

Discussion Overview

The discussion revolves around the challenge of converting an existing Java program into a browser-compatible applet without modifying the original code structure. Participants explore the requirements for applet functionality, particularly the necessity of specific methods like init and destroy, which the original poster wishes to avoid.

Discussion Character

  • Homework-related
  • Debate/contested

Main Points Raised

  • The original poster expresses confusion about the need for additional methods in their Java program to run as an applet, stating that their application works perfectly as is.
  • Some participants suggest that applets typically require lifecycle methods such as init and destroy to manage their execution within a browser environment.
  • Others argue that it might be possible to run the program without these methods, depending on how the applet is structured and the specific requirements of the browser.
  • A later reply questions whether the applet can function correctly without the standard methods, emphasizing the importance of adhering to applet conventions.

Areas of Agreement / Disagreement

Participants do not reach a consensus on whether the original Java program can be run as an applet without modifying it to include the required methods. Multiple competing views remain regarding the necessity of these methods.

Contextual Notes

There is an implicit assumption that the applet must conform to certain standards to function correctly in a browser, but the specifics of these requirements are not fully explored. The discussion also highlights the potential limitations of the original code's design in relation to applet functionality.

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
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 20 ·
Replies
20
Views
5K
Replies
8
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K