
11-22-2009, 05:28 PM
|
|
Member
|
|
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
|
|
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);
}
} |
|
|

11-22-2009, 05:35 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
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
|
|

11-22-2009, 05:47 PM
|
|
Member
|
|
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
|
|
|
No it still doesn't work.
Hope it's not my PC!
|
|

11-22-2009, 05:54 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
|
How does it "not work"? What specifically goes wrong?
|
|

11-22-2009, 05:57 PM
|
|
Member
|
|
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
|
|
Originally Posted by Fubarable
|
|
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.
|
|

11-22-2009, 06:03 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
|
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.
|
|

11-22-2009, 06:12 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
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.
|
|
|

11-22-2009, 06:12 PM
|
|
Member
|
|
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
|
|
Originally Posted by Fubarable
|
|
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"?
|
|

11-22-2009, 06:17 PM
|
 |
Moderator
|
|
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
|
|
|
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
|
|

11-22-2009, 06:21 PM
|
|
Member
|
|
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
|
|
Originally Posted by Fubarable
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
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