2

My JApplet is producing a error when DrawOvalInputs.html runs and calls the class file of DrawOvalInputs. So far, I have only been able to get it work as an actual application (Which is why the main is in a block quote).

My goal for this program is to be able to run a .html file to start up the JApplet with a medium security setting on the java console, yet no matter what I've done it simply won't function.

I have looked around on quite a few pages and searches before coming here. Regrettably, I just can't figure this JApplet out, so if someone could guide me in the right direction I'd be most thankful!

My Code below:

DrawOvalInputs.java

package drawovalapplet;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

/**
 * This applet inputs a number of values, and then computes the size of
 * an oval with those given values. 
 *
 * @author  [Redacted]
 * @version 2014-05-02, [Redacted]
 */
public class DrawOvalInputs extends JApplet
{
    private static int x;               //left edge of the oval
    private static int y;               //top edge of the oval
    private static int width;           //width of oval
    private static int height;          //height of oval
    private static int windowWidth;     //Holds necessary width of window.
    private static int windowHeight;    //Holds necessary height of window.
    private static int windowBox;       //Holds box form of window.


@Override
public void init()
{    
    try 
    {
        /** Collect the input values. */    
        Input.inputAll();

        /** creates dimensions for box */
        windowWidth = width + x + 50;
        windowHeight = height + y + 50;

        /** 
         * If... else... function to gather data to create
         * a fitting box for the oval
         */
        if(windowWidth > windowHeight)
            windowBox = windowWidth;
        else
            windowBox = windowHeight;

    } catch (Input.CanceledException ex) {
        System.exit(1);
    }
} 

@Override
public void paint(Graphics g)
{
    super.paint(g);      
    g.drawOval(x, y, width, height);
} // end method pain


/**
 * Main entry point.
 * <p> Execute:
 * <pre>java drawovalapplet.DrawOvalInputs</pre>
 *
 * @param args not used.
 */
/*public static void main(String args[])
{
Frame frame = new Frame("DrawOvalInputs");
DrawOvalInputs drawOval = new DrawOvalInputs();
drawOval.init();
drawOval.start();
frame.add(drawOval);
frame.setSize(drawOval.windowBox, drawOval.windowBox);
frame.setVisible(true);
}*/

//------------------------------- Nested Classes --------------------------------

/**
 * Enumeration of the name and value of the input values.
 */
private enum Input
{
/**
 * Message for the entering the x coordinate.
 */
XVALUE("Enter the argument for the x coordinate of the upper left corner of the oval to be drawn:"),

/**
 * Message for the entering the y coordinate.
 */
YVALUE("Enter the argument for the y coordinate of the upper left corner of the oval to be drawn:"),

/**
 * Message for entering the width.
 */
WIDTHVALUE("Enter the desired width of the oval to be drawn:"),

/**
 * Message for entering the height.
 */
HEIGHTVALUE("Enter the desired height of the oval:");

/**
 * String to use in messages (from the constructor).
 */
protected String invitation;

/**
 * String to use for error messages.
 */
protected String error = "Not an integer value--please re-enter:";

/**
 * Value of this {@literal <variable>}.
 */
protected int value;

/**
 * @param label string to use in messages
 */
Input(String invitation)
{
    this.invitation = invitation;
}

public static void inputAll() throws CanceledException
{
    /* Decide which input value is currently being used. */
    int count = 0; 

    /* Collect the input numbers. */
    for(Input input : Input.values())
    {
        /* Set up the invitation to enter each number. */
        String message = input.invitation;

        /* Loop until the user inputs an acceptably formatted number. */
        while(true)                         // repetition environment
        {
            String response;
            /* Null return from the JOptionPane indicates CANCEL was pressed. */
            if( (response = JOptionPane.showInputDialog(message)) == null)
                throw new CanceledException();
            message = input.error;              // just in case
            try
            {
                input.value = Integer.parseInt(response);
                break;                          // success in acquiring value
            }
            catch(NumberFormatException nfe) {}// ignore all, and try again
        }
        count++;
        if(count == 1)
            x = input.value;
        else if(count == 2)
            y = input.value;
        else if (count == 3)
            width = input.value;
        else if (count == 4)
            height = input.value;
        else
            System.out.println("Error. Revise.");   
    }
}

@SuppressWarnings("serial")
public static class CanceledException extends Exception {}
}
}

DrawOvalInputs.html I've run this with DrawOvalInputs.java and the one below, as well as with .class instead.

<html>
  <body>
      <applet code=drawovalapplet.DrawOvalInputs.java width=400 height=400>
      </applet>
  </body>
</html>

Thanks!

Luminesce
  • 37
  • 9
  • **UPDATE:** Upon reviewing the Netbeans page for applet creation(Below), I have attempted all formats of what was presented, in JApplet and Applet formats, with JDKs 1.8 and 1.7 in consideration that perhaps something was amiss or corrupted. Both JDKs were reinstalled just in case, yet still nothing has functioned. https://netbeans.org/kb/docs/web/applets.html – Luminesce May 04 '14 at 04:00
  • why have you commented `/*public static void main(String args[])` – moskito-x May 04 '14 at 19:22
  • The JApplet is supposed to be able to run in a browser, and be initialized as an JApplet and not as a regular desktop application, I believe. Or at least, from the two different forms that they are accessed and run. It's run through overridden methods stated as the following by the browser, if I understand correctly. `public void init();` `public void start();` `public void stop();` `public void init();` `public void destroy();` – Luminesce May 04 '14 at 19:25
  • `public void paint();` instead of the second init, sorry. – Luminesce May 04 '14 at 19:31
  • *"My goal for this program is to be able to run a .html file to start up the JApplet.."* For deploying Java desktop apps., the best option is usually to install the app. using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). JWS works on Windows, OS X & *nix. – Andrew Thompson May 05 '14 at 08:34
  • `} catch (Input.CanceledException ex) { System.exit(1);..` that is an `AccessControlException` waiting to happen. ;) – Andrew Thompson May 05 '14 at 08:45

2 Answers2

2

you must call it with the right path

if your path is like

DrawOvalApplet\build\classes\drawovalapplet\DrawOvalInputs.class

and your .html is in

DrawOvalApplet\build\DrawOvalInputs.html

call it

 ...
      <applet code=classes.drawovalapplet.DrawOvalInputs.class width=400 height=400>
      </applet>
 ...

much better your .html is in

DrawOvalApplet\build\classes\DrawOvalInputs.html

call it

 ...
      <applet code=drawovalapplet.DrawOvalInputs.class width=400 height=400>
      </applet>
 ...

result :

enter image description here

applet running

enter image description here

You can call htmlconverter and let do it for you

java -jar htmlconverter.jar -gui

enter image description here

Result DrawOvalInputs.html

<html>
  <body>
      <!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<object
    classid = "clsid:CAFEEFAC-0017-0000-0051-ABCDEFFEDCBA"
    codebase = "http://java.sun.com/update/1.7.0/jinstall-7u51-windows-i586.cab#Version=7,0,510,13"
    WIDTH = 400 HEIGHT = 400 >
    <PARAM NAME = CODE VALUE = drawovalapplet.DrawOvalInputs.class >
    <param name = "type" value = "application/x-java-applet;jpi-version=1.7.0_51">
    <param name = "scriptable" value = "false">

    <comment>
    <embed
            type = "application/x-java-applet;jpi-version=1.7.0_51" \
            CODE = drawovalapplet.DrawOvalInputs.class \
            WIDTH = 400 \
            HEIGHT = 400
        scriptable = false
        pluginspage = "http://java.sun.com/products/plugin/index.html#download">
        <noembed>

            </noembed>
    </embed>
    </comment>
</object>

<!--
<APPLET CODE = drawovalapplet.DrawOvalInputs.class WIDTH = 400 HEIGHT = 400>


</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->

  </body>
</html>
moskito-x
  • 11,832
  • 5
  • 47
  • 60
  • So it was the path argument for code, or in some formats, codebase and code. It astonishes me how simple these things can be. Thank you very much for your comprehensive answer, @moskito-x, I'm most appreciative =) – Luminesce May 04 '14 at 22:24
  • @Kelenth : You are welcome . I even often stumbled already on the wrong path assignment. – moskito-x May 05 '14 at 00:27
  • `` The best way to deploy a JWS app. or applet is to use the [Deployment Toolkit Script](http://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/depltoolkit_index.html). – Andrew Thompson May 05 '14 at 08:33
1

Use the class file and the actual file path and name. For example:

 <applet code=DrawOvalInputs.class width=400 height=400>
  </applet>

if the class file is located in the same folder as your HTML file.

fjc
  • 5,590
  • 17
  • 36
  • Thank you for the response. I just attempted to run it from the DrawOvalInputs.html file with the DrawOvalInputs.class, but chrome has a Major.minor 52 error, so I'll need to resolve that really quick! Do you know a way to do this directly from Netbeans IDE 8.0? I can only find my .java and Html in the sourcepackage, and not the classes. – Luminesce May 03 '14 at 20:07
  • I suggest you take a look at https://netbeans.org/kb/docs/web/applets.html, which pretty much covers the whole process. I think it will also give you a working HTML page. – fjc May 03 '14 at 20:08
  • I honestly don't know what's wrong. I did the instructions on the Netbeans IDE step by step and it still comes up with the same issue? Any ideas? – Luminesce May 03 '14 at 21:21
  • The Major/Minor problem is another problem. Check http://stackoverflow.com/questions/10382929/unsupported-major-minor-version-51-0 – fjc May 03 '14 at 21:23
  • Yeah, it took a tiny bit I resolved that issue, thanks =) I only resolved the Major.Minor problem, I'm still incapable of getting this JApplet to run without the error. – Luminesce May 03 '14 at 21:59
  • Can you just upload the entire thing somewhere? – fjc May 03 '14 at 22:00
  • Thank you for your efforts, @fjc, I appreciate it. – Luminesce May 04 '14 at 22:28