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
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);
}
}
}
and here is T_Applet.java
Code:
public class T_Applet {
public void init() {
//Creates instance of ColorViewer
Applet gui = new Applet();
//GUI setup.
gui.setSize(500,500);
}
}
Finally, here is the html I am using to display it:
"<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.
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.
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.
Re: Applet just displays garbled text
Also, what do you think these 3 lines do? Code:
hexBar.setText(Integer.toHexString(redSlide.getValue()));
hexBar.setText(Integer.toHexString(greenSlide.getValue()));
hexBar.setText(Integer.toHexString(blueSlide.getValue()));
db
Re: Applet just displays garbled text
Ok, based on the feedback you guys gave I now have:
package firstapplet;
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);
}
}
and
"<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)
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
Re: Applet just displays garbled text
Ok, I changed it, but I still get the same error.
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>"
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.
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!
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.
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.
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!
Re: Applet just displays garbled text
Using packages requires that the class files be in the folder(s) that matches the package name
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.
Re: Applet just displays garbled text
Quote:
If I didn't have a package would it have worked to have them all in the same folder?
Yes. I think That was what camickr was talking about.
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.
Re: Applet just displays garbled text
I cound run the FirstApplet. In my project root bin I saved the html-file with this content
Quote:
<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>
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.