Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2009, 05:28 PM
Member
 
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
Arnold is on a distinguished road
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?

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);
	}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2009, 05:35 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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?
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);
   }
}
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2009, 05:47 PM
Member
 
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
Arnold is on a distinguished road
Angry
No it still doesn't work.

Hope it's not my PC!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-22-2009, 05:54 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
Default
How does it "not work"? What specifically goes wrong?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-22-2009, 05:57 PM
Member
 
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
Arnold is on a distinguished road
Default
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:
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-22-2009, 06:03 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-22-2009, 06:12 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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:
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-22-2009, 06:12 PM
Member
 
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
Arnold is on a distinguished road
Default
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"?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-22-2009, 06:17 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-22-2009, 06:21 PM
Member
 
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
Arnold is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wrong output (well.. the one who's wrong is probably me ;) ) shacht1 New To Java 2 11-22-2009 04:48 PM
Pass parameter from one applet to another applet in different webpages... anubhavranjan Java Applets 2 09-29-2009 04:33 PM
What am I doing wrong?? NoNickName New To Java 3 04-24-2009 12:04 AM
Calling another applet on click of button in one applet niteshwar.bhardwaj Java 2D 1 02-19-2009 01:54 PM
Applet, To center text and To open I engage in a dialog in an Applet Marcus Java Applets 4 06-08-2007 07:15 AM


All times are GMT +2. The time now is 05:27 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org