Turning a Java program into an applet

In summary, the conversation is about the desire to run an existing Java program in a browser without making any changes to the code. The code draws a clock based on user input for the time. However, the individual is confused about the need for additional methods in order to run the program in a browser.
  • #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!
 
Physics news on Phys.org
  • #2
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?
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 

1. How do I convert my Java program into an applet?

Converting a Java program into an applet involves a few steps. First, you need to make sure that your program follows certain guidelines for applets, such as implementing the Applet interface and using the standard applet lifecycle methods. Then, you will need to compile your program and create a HTML file that includes the necessary code to display the applet in a web browser.

2. Do I need any special tools or software to create an applet?

In order to create an applet, you will need a Java development kit (JDK), which can be downloaded for free from the Oracle website. This includes the necessary tools for compiling and running Java code. You will also need a text editor for writing your Java code and an HTML editor for creating the HTML file that will display your applet.

3. Can I use any Java program to create an applet?

Not all Java programs can be converted into applets. Applets have certain restrictions, such as not being able to access the local file system, that may prevent certain programs from functioning properly as an applet. Additionally, applets need to be able to run within a web browser, so any programs that require a standalone application may not be suitable for conversion.

4. Are there any security concerns when creating an applet?

Yes, applets can potentially pose security risks if not properly designed. Applets run within a web browser and have access to the user's system and network resources, so it is important to carefully consider the code and permissions given to the applet. It is recommended to digitally sign your applet to ensure its authenticity and minimize security risks.

5. Can I use an applet on any web browser?

Applets are designed to run on any web browser that supports Java. However, due to security concerns, some browsers may not allow applets to run by default. Users may need to adjust their browser settings or enable Java in order to run the applet. Additionally, some browsers may not support certain features of Java, so it is important to test your applet on different browsers to ensure compatibility.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
8
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
Back
Top