1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Turning a Java program into an applet

  1. Jun 5, 2012 #1
    1. The problem statement, all variables and given/known data

    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 (Text):
    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 (Text):
    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");
    }
       
    }
    2. Relevant equations



    3. 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!
     
  2. jcsd
  3. Jun 5, 2012 #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?
     
  4. Jun 5, 2012 #3

    Mark44

    Staff: Mentor

    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 (Text):

    <!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.
     
  5. Jun 6, 2012 #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.
     
  6. Jun 6, 2012 #5

    Mark44

    Staff: Mentor

    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.
     
  7. Jun 6, 2012 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Turning a Java program into an applet
  1. Java applet help (Replies: 4)

  2. Java programming (Replies: 4)

  3. Java program (Replies: 3)

Loading...