Results 1 to 18 of 18
- 12-10-2011, 11:17 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Applet just displays garbled text
Hi,
I am converting my simple application into an applet and seem to have made a mistake. Whenever I go to the webpage on which I am hosting the applet, it just displays garbled text. Please help! :)
Here is my Applet.java
and here is T_Applet.javaJava Code:import java.awt.*; import javax.swing.*; import javax.swing.event.*; public class Applet extends JApplet{ JSlider redSlide; JSlider greenSlide; JSlider blueSlide; JPanel colorPanel; JPanel sliders; JPanel labels; JLabel hexBar; public Applet() { redSlide = new JSlider (JSlider.HORIZONTAL, 0, 255, 0); greenSlide = new JSlider (JSlider.HORIZONTAL, 0, 255, 0); blueSlide = new JSlider (JSlider.HORIZONTAL, 0, 255, 0); event e = new event(); redSlide.addChangeListener(e); greenSlide.addChangeListener(e); blueSlide.addChangeListener(e); colorPanel = new JPanel(); colorPanel.setBackground(Color.BLACK); Container panel = this.getContentPane(); panel.setLayout(new GridLayout(6,2,15,0)); sliders = new JPanel(); hexBar = new JLabel("RGB Value"); hexBar.setBackground(Color.white); hexBar.setForeground(Color.black); panel.add(sliders); panel.add(colorPanel); panel.add(hexBar); sliders.setLayout(new GridLayout(3,1,2,2)); sliders.add(redSlide); sliders.add(greenSlide); sliders.add(blueSlide); hexBar.setText(Integer.toHexString(redSlide.getValue())); hexBar.setText(Integer.toHexString(greenSlide.getValue())); hexBar.setText(Integer.toHexString(blueSlide.getValue())); } public class event implements ChangeListener{ public void stateChanged(ChangeEvent e){ int r = redSlide.getValue(); int g = greenSlide.getValue(); int b = blueSlide.getValue(); colorPanel.setBackground(new Color(r,g,b)); hexBar.setText( zeroPad(redSlide.getValue(), 2) + zeroPad(greenSlide.getValue(), 2) + zeroPad(blueSlide.getValue(), 2)); } private String zeroPad(int value, int size) { String s = "00"+Integer.toHexString(value); return s.substring(s.length() - size); } } }
Finally, here is the html I am using to display it:Java Code:public class T_Applet { public void init() { //Creates instance of ColorViewer Applet gui = new Applet(); //GUI setup. gui.setSize(500,500); } }
"<html>
<head>
<title>Color Slider</title>
</head>
<body>
<applet code="T_Applet.class" height="400" width="400">
This program requires a Java compliant browser.
</applet>
</body>
</html>"
-
Re: Applet just displays garbled text
You'll want to tell us what gibberish you're seeing and when.
- 12-11-2011, 12:32 AM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
Re: Applet just displays garbled text
Don't call your class Applet. There is an AWT class by that name which is confusing
You don't need two classes. The class that extends JApplet should be the class with the init() method.
See: How to Make Applets (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for more information.
- 12-11-2011, 02:00 AM #4
Re: Applet just displays garbled text
I wouldn't expect the code to execute at all. The code= attribute does NOT point at a class that extends Applet class.
- 12-11-2011, 02:55 AM #5
Re: Applet just displays garbled text
Also, what do you think these 3 lines do?
dbJava Code:hexBar.setText(Integer.toHexString(redSlide.getValue())); hexBar.setText(Integer.toHexString(greenSlide.getValue())); hexBar.setText(Integer.toHexString(blueSlide.getValue()));Why do they call it rush hour when nothing moves? - Robin Williams
- 12-11-2011, 03:06 AM #6
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Re: Applet just displays garbled text
Ok, based on the feedback you guys gave I now have:
package firstapplet;
andJava Code:import java.awt.*; import javax.swing.*; import javax.swing.event.*; public class FirstApplet extends JApplet{ JSlider redSlide; JSlider greenSlide; JSlider blueSlide; JPanel colorPanel; JPanel sliders; JPanel labels; JLabel hexBar; public FirstApplet() { redSlide = new JSlider (JSlider.HORIZONTAL, 0, 255, 0); greenSlide = new JSlider (JSlider.HORIZONTAL, 0, 255, 0); blueSlide = new JSlider (JSlider.HORIZONTAL, 0, 255, 0); event e = new event(); redSlide.addChangeListener(e); greenSlide.addChangeListener(e); blueSlide.addChangeListener(e); colorPanel = new JPanel(); colorPanel.setBackground(Color.BLACK); Container panel = this.getContentPane(); panel.setLayout(new GridLayout(6,2,15,0)); sliders = new JPanel(); hexBar = new JLabel("RGB Value"); hexBar.setBackground(Color.white); hexBar.setForeground(Color.black); panel.add(sliders); panel.add(colorPanel); panel.add(hexBar); sliders.setLayout(new GridLayout(3,1,2,2)); sliders.add(redSlide); sliders.add(greenSlide); sliders.add(blueSlide); hexBar.setText(Integer.toHexString(redSlide.getValue())); hexBar.setText(Integer.toHexString(greenSlide.getValue())); hexBar.setText(Integer.toHexString(blueSlide.getValue())); } public class event implements ChangeListener{ public void stateChanged(ChangeEvent e){ int r = redSlide.getValue(); int g = greenSlide.getValue(); int b = blueSlide.getValue(); colorPanel.setBackground(new Color(r,g,b)); hexBar.setText( zeroPad(redSlide.getValue(), 2) + zeroPad(greenSlide.getValue(), 2) + zeroPad(blueSlide.getValue(), 2)); } private String zeroPad(int value, int size) { String s = "00"+Integer.toHexString(value); return s.substring(s.length() - size); } } public void init() { //Creates instance of ColorViewer FirstApplet gui = new FirstApplet(); //GUI setup. gui.setSize(500,500); } }
"<html>
<head>
<title>Color Slider</title>
</head>
<body>
<applet code="FirstApplet.class" height="400" width="400">
This program requires a Java compliant browser.
</applet>
</body>
</html>"
When I go to the page I get a java error.
java.lang.NoClassDefFoundError: FirstApplet (wrong name: firstapplet/FirstApplet)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NoClassDefFoundError: FirstApplet (wrong name: firstapplet/FirstApplet)
- 12-11-2011, 03:09 AM #7
Re: Applet just displays garbled text
Did you post all of the code? Is the class in a package?
Ok I just saw it outside of the code tags.
You need to include the package name in the code= attribute
code=firstapplet.FirstApplet
- 12-11-2011, 03:20 AM #8
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Re: Applet just displays garbled text
Ok, I changed it, but I still get the same error.
XML Code:"<html> <head> <title>Color Slider</title> </head> <body> <applet code="firstapplet.FirstApplet" height="400" width="400"> This program requires a Java compliant browser. </applet> </body> </html>"
Last edited by acopernicanrevolution; 12-11-2011 at 03:22 AM.
- 12-11-2011, 03:39 AM #9
Re: Applet just displays garbled text
Please post the full text of the error message. I think it should be different if you look closely.
- 12-11-2011, 03:45 AM #10
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Re: Applet just displays garbled text
Sure.
load: class firstapplet.FirstApplet not found.
java.lang.ClassNotFoundException: firstapplet.FirstApplet
at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassNotFoundException: firstapplet.FirstApplet
Oh, you are right! It is different! Sorry about that!
- 12-11-2011, 01:15 PM #11
Re: Applet just displays garbled text
Where is the class file that is not found? It needs to be in the firstapplet folder.
Where is the html file? It should be in the folder that CONTAINS the firstapplet folder.
- 12-11-2011, 04:47 PM #12
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
Re: Applet just displays garbled text
Why do people always try to run before learning to walk?
Keep your code simple. Don't use packages. Just get it working first and then move on. Read the tutorial. Your code looks nothing like the code from the tutorial. The tutorial has a simple example of displaying a label in an applet in about 10 lines of code. That is all the code you need to create an applet. Get that code working first. Once you understand the simple structure, then modify the code to support the components you wish to use.
- 12-11-2011, 05:09 PM #13
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Re: Applet just displays garbled text
It works now!
Norm: Thank you soooooo much! I didn't realize that I would need a firstapplet folder. I had all of the files (the html, the class file, and the event class file) all in the same directory. I created a subdirectory named firstapplet and moved the class files into there and now it works!
- 12-11-2011, 05:13 PM #14
Re: Applet just displays garbled text
Using packages requires that the class files be in the folder(s) that matches the package name
- 12-11-2011, 05:14 PM #15
Member
- Join Date
- Dec 2011
- Posts
- 6
- Rep Power
- 0
Re: Applet just displays garbled text
If I didn't have a package would it have worked to have them all in the same folder? I think I'm going to read up a bit more on packages. I never gave them much thought and have always just used them because netbeans creates them by default.
- 12-11-2011, 05:31 PM #16
Re: Applet just displays garbled text
Yes. I think That was what camickr was talking about.If I didn't have a package would it have worked to have them all in the same folder?
- 12-11-2011, 05:53 PM #17
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
Re: Applet just displays garbled text
I was talking about many things, the package name being just one of them.
Unless you've changed your posted code, the code is still wrong. You should not be creating an instance of FirstApplet in your code. That is the job of the browswer. The code to create the GUI should be in the init() method. Creation of the components should be done on the EDT etc. The tutorial shows a simple structure that is easy to follow.
- 12-11-2011, 08:30 PM #18
Re: Applet just displays garbled text
I cound run the FirstApplet. In my project root bin I saved the html-file with this content
and in the bin root i created a new folder firstapplet where the FirstApplet.class is saved. After a doubleclick on the html-file the applet was displayed in the browser. If you don't use a package in your class then you can put the html- and the class-file in the same directory.<html>
<head>
<title>Color Slider</title>
</head>
<body>
<applet code="firstapplet.FirstApplet" height="400" width="400">
This program requires a Java compliant browser.
</applet>
</body>
</html>Last edited by j2me64; 12-11-2011 at 08:32 PM.
Similar Threads
-
Java Applet that displays two pictures with a button...
By yenujnassah in forum New To JavaReplies: 0Last Post: 11-10-2011, 09:54 AM -
Need help making an applet that displays a pie chart.
By jeskoston in forum New To JavaReplies: 5Last Post: 05-14-2011, 12:33 AM -
Applet to change text color
By sak_java in forum Java AppletsReplies: 8Last Post: 02-19-2011, 06:56 PM -
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 06:15 AM


4Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks