Results 1 to 10 of 10
  1. #1
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Unhappy What's wrong with this applet?

    I just made my first applet. The purpose of it is to show a color map of red horizontally and blue vertically.
    It compiles, but it doesn't work on a html page or a browser.
    Anyone know what's wrong with it?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    class colors extends JApplet {
    	public void init() {
    		JPanel panel = new JPanel();
    		panel.setLayout(new GridLayout(64, 64));
    		for (short i = 0; i < 0x100; i += 4) {
    			for (short j = 0; j < 0x100; j += 4) {
    				JPanel subpanel = new JPanel();
    				subpanel.setBackground(new Color(j, 0, i));
    				panel.add(subpanel);
    			}
    		}
    		getContentPane().add(panel);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may have a Swing threading issue here. What if you use SwingUtiltities.invokeAndWait to try to create the applet on the EDT (event dispatch thread) as I believe is recommended in the tutorials?
    Java Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    
    public class Colors2 extends JApplet {
       public void init() {
          try {
             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                   createGUI();
                }
             });
          }
          catch (Exception e) {
             System.err.println("createGUI didn't successfully complete");
          }
       }
    
       private void createGUI() {
          JPanel panel = new JPanel();
          panel.setLayout(new GridLayout(64, 64));
          for (short i = 0; i < 0x100; i += 4) {
             for (short j = 0; j < 0x100; j += 4) {
                JPanel subpanel = new JPanel();
                subpanel.setBackground(new Color(j, 0, i));
                panel.add(subpanel);
             }
          }
          
          getContentPane().add(panel);
       }
    }

  3. #3
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Angry

    No it still doesn't work.

    Hope it's not my PC!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    How does it "not work"? What specifically goes wrong?

  5. #5
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    How does it "not work"? What specifically goes wrong?
    The java console on my browser gives the following message, but I can't decode it:
    Java Code:
    java.lang.reflect.InvocationTargetException
    	at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Caused by: java.lang.RuntimeException: java.lang.IllegalAccessException: Class sun.plugin2.applet.Plugin2Manager$12 can not access a member of class colors with modifiers ""
    	at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.lang.IllegalAccessException: Class sun.plugin2.applet.Plugin2Manager$12 can not access a member of class colors with modifiers ""
    	at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
    	at java.lang.Class.newInstance0(Unknown Source)
    	at java.lang.Class.newInstance(Unknown Source)
    	... 9 more
    Exception: java.lang.reflect.InvocationTargetException
    java.lang.NullPointerException
    	at sun.plugin2.applet.Plugin2Manager.findAppletJDKLevel(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Exception: java.lang.NullPointerException
    When I run AppletViewer, it simply does nothing.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are you absolutely 100% sure that this error is coming from my code above? That your HTML is requesting my class? Please double check since my changes solved this very error that I was getting with your code.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    For more information on why invokeAndWait is important here, please see this tutorial:
    Concurrency in Swing

    On the second page, you'll find this:
    In an applet, the GUI-creation task must be launched from the init method using invokeAndWait; otherwise, init may return before the GUI is created, which may cause problems for a web browser launching an applet. In any other kind of program, scheduling the GUI-creation task is usually the last thing the initial thread does, so it doesn't matter whether it uses invokeLater or invokeAndWait.

  8. #8
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Are you absolutely 100% sure that this error is coming from my code above? That your HTML is requesting my class? Please double check since my changes solved this very error that I was getting with your code.
    Yes, I deleted my old java file, then renamed the new version back to colors (was colors2). I also renamed the class then recompiled.
    Say, why does your code generate "colors$1.class"?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It shouldn't. It should create a Colors2$1.class instead. This is for the anonymous inner class that I create with the new Runnable().... statement.

  10. #10
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It shouldn't. It should create a Colors2$1.class instead. This is for the anonymous inner class that I create with the new Runnable().... statement.
    Not familiar with inner classes.
    When it was still a normal appication, it worked (not perfectly, but it did), until I converted to Applet.
    I'll try to take an example Applet from my book. If that doesn't work, I'll smash my PC.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Replies: 2
    Last Post: 09-29-2009, 03:33 PM
  3. What am I doing wrong??
    By NoNickName in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 11:04 PM
  4. Calling another applet on click of button in one applet
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 02-19-2009, 12:54 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •