8 Replies Latest reply on Jan 6, 2004 3:01 AM by 807595

    Getting Applet to Work in an HTML Document

    807595
      I have written an Applet which works perfectly in the Applet Viewer, but crashes when I attempt to put it on my web page. The crash gives no errors, no exceptions, no nothing. Just a grey square with the words "Applet Crashed". I tried it both with the files in a .jar,and with all the files uncompressed, and used the W3C's code validator at http://validator.w3.org/ to verify that my HTML is correct.

      The HTML code using the .jar file is:

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <a bunch of HTML code>

      <object data="ComicTemplate.class" type="applet/java" archive="AICS.jar" WIDTH=620 HEIGHT=650>

      If you can read this, your broswer doesn't support Java. Go to Sun's website to get the latest version of Java. You may also need to turn on Java in your browser's preferences.

      </object>

      <more HTML>
      </html>

      The code without the .jar is the same, except that the archive lists the 8 .class files and 18 images used individually.

      Any advice?
        • 1. Re: Getting Applet to Work in an HTML Document
          807595
          In the AppletViewer are you using the .class files directly or the .jar ?
          Perhaps it's the difference between the AppletViewer and the Browser. Try in the same condition.

          Happy new year,
          Jean-Paul
          • 2. Re: Getting Applet to Work in an HTML Document
            807595
            I tried it both ways. It works using either method in the applet viewer, but niether works in the browser.

            In the good news, however, I found a hidden Java console inmy browser, which did have some error messages. Cut-n-pasted:

            -- Opera Java Console --


            Java vendor: Sun Microsystems Inc.

            Java version: 1.4.1_02


            type 'h' for help

            --

            java.security.AccessControlException: access denied (java.io.FilePermission Hunter.jpg read)

                 at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270)

                 at java.security.AccessController.checkPermission(AccessController.java:401)

                 at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)

                 at java.lang.SecurityManager.checkRead(SecurityManager.java:887)

                 at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:486)

                 at sun.awt.SunToolkit.getImage(SunToolkit.java:500)

                 at javax.swing.ImageIcon.<init>(ImageIcon.java:81)

                 at javax.swing.ImageIcon.<init>(ImageIcon.java:107)

                 at Panel1.<init>(Panel1.java:24)

                 at Comic.<init>(Comic.java:22)

                 at ComicTemplate.<init>(ComicTemplate.java:26)

                 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

                 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

                 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

                 at java.lang.reflect.Constructor.newInstance(Constructor.java:274)

                 at java.lang.Class.newInstance0(Class.java:306)

                 at java.lang.Class.newInstance(Class.java:259)

                 at com.opera.PluginPanel.run(PluginPanel.java:412)

                 at java.lang.Thread.run(Thread.java:536)

            • 3. Re: Getting Applet to Work in an HTML Document
              807595
              The java.security.AccessControlException: access denied (java.io.FilePermission Hunter.jpg read) means your are trying to read an image from your applet, but the JVM hasn't the security permission to do it.

              In a Browser, for security reason, the operations allowed are limited. It's forbidden to open a socket to another web site or to access to local file of the customer by example.
              You could modify your java.policy file but it sounds dangerous. The best way is to fix how to open your file.

              How are you opening the file Hunter.jpg ? You have to use getRessource() to read it from the jar file.
              It should solve the problem.

              Jean-Paul
              • 4. Re: Getting Applet to Work in an HTML Document
                807595
                Thanks for your help.

                I declare it as

                private ImageIcon imgHunter = new ImageIcon("images/Hunter.jpg");

                and call it using

                comic.pnlPanel1.lblLeftPic1.setIcon(imgHunterLeft);

                I'll try using getResource(), but there are 17 other images used in this applet, all declared and used the same way, and that's the only one that's giving an error message.
                • 5. Re: Getting Applet to Work in an HTML Document
                  807595
                  I tried it, and ran into a problem.

                  getResource() creates an image, but images can't be used directly in the situation I'm using.

                  What I am building is an applet that will let users build their own comic strip. There are a series of JComboBoxes from which the user can pick which character appears in which panel, and a series of JTextAreas where the user can write dialog. When the user clicks the create button, the comic is created in a second panel. Trying to use paint() for images got very messy, so I used JLabels, and changed the icons using the code I showed you.

                  Unfortunately, images can't be directly changed into imageIcons, so if I do that, I have no way to make the characters show upin the comic. ImageIcon() can't reference getResource(), so I can't directly put it there, either.
                  • 6. Re: Getting Applet to Work in an HTML Document
                    807595
                    Hi,

                    You could try something like this:
                        public ImageIcon getImageIcon(String path) {
                          URL url = null;
                          ImageIcon icon = null;
                            url = getClass().getResource(relativePath);
                          if(url != null) {
                            icon = new ImageIcon(url);
                          }
                          else {
                            System.err.println("ERROR: Image '" + path + "' not found.");
                          }
                          return icon;
                       }
                    And I think you will get the same security execption for all your images if you don't use getResource(). But your applet stop at the first one ....

                    Good luck,
                    Jean-Paul GUIGUI
                    • 7. Re: Getting Applet to Work in an HTML Document
                      807595
                      Thanks for the help.

                      What you suggested didn't work, but it did point me in the right direction, so I was able to figure it out...sort of.

                      My compiler wan't happy with the idea of initializing the icon to null, so I got around it by initializing it to a 1x1 clear image, like so:

                      public ImageIcon getImageIconHunter() {
                      URL AsIf = null;
                      URL AsIf0 = null;
                      AsIf0 = getClass().getResource("images/null.jpg");
                      ImageIcon icon = new ImageIcon(AsIf0);
                      AsIf = getClass().getResource("images/Hunter.jpg");
                      if(AsIf != null) {
                      icon = new ImageIcon(AsIf);
                      }
                      else {
                      System.err.println("ERROR: Image '" + AsIf + "' not found.");
                      }
                      return icon;

                      My applet now works on web browsers and the net. However, this has allowed me to find another case wherein the applet viewer doesn't match the browser.

                      For reasons I haven't figured out yet, my browser is ignoring size definitions on JTextAreas and JTextFields. They were defined in my code as :

                      JTextField txtRecall = new JTextField("Enter Recall data here", 20);

                      and

                      public JTextArea txtPan1Left = new JTextArea(2, 20);

                      The JTextAreas were also placed in a scrollpane like so:

                      JScrollPane scrollPane1 = new JScrollPane(txtPan1Left,
                      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

                      In the applet viewer, this puts both at 20 characters. Viewed in the web browser, however, they are only half a character wide. Is this a coding error on my part, or does my web browser (Opera v.7.23) have issues? Since it now is working, you can see it at http://www.siece.net/AsIf/ComicDone.html if you'd like to test it in your browser of choice.

                      Keep in mind that not all of the features have been written yet, so you can't save or recall data. I'll be coding that part once the rest of it is working right.
                      • 8. Re: Getting Applet to Work in an HTML Document
                        807595
                        Update:

                        I tried it on another computer, using a different browser, and the problem was still there. It's definitely a coding problem.