Results 1 to 18 of 18
Like Tree4Likes
  • 1 Post By Norm
  • 1 Post By Norm
  • 1 Post By Norm
  • 1 Post By camickr

Thread: Applet just displays garbled text

  1. #1
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default 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

    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

    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>"

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

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default 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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Applet just displays garbled text

    Also, what do you think these 3 lines do?
    Java Code:
           hexBar.setText(Integer.toHexString(redSlide.getValue()));
           hexBar.setText(Integer.toHexString(greenSlide.getValue()));
           hexBar.setText(Integer.toHexString(blueSlide.getValue()));
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Re: Applet just displays garbled text

    Ok, based on the feedback you guys gave I now have:

    package firstapplet;

    Java 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)

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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

  8. #8
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default 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.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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.

  10. #10
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default 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!

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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. #12
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default 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.
    Norm likes this.

  13. #13
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default 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!

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Applet just displays garbled text

    Using packages requires that the class files be in the folder(s) that matches the package name

  15. #15
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default 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.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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?
    Yes. I think That was what camickr was talking about.

  17. #17
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default 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.

  18. #18
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Applet just displays garbled text

    I cound run the FirstApplet. In my project root bin I saved the html-file with this content

    <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.
    Last edited by j2me64; 12-11-2011 at 08:32 PM.

Similar Threads

  1. Java Applet that displays two pictures with a button...
    By yenujnassah in forum New To Java
    Replies: 0
    Last Post: 11-10-2011, 09:54 AM
  2. Need help making an applet that displays a pie chart.
    By jeskoston in forum New To Java
    Replies: 5
    Last Post: 05-14-2011, 12:33 AM
  3. Applet to change text color
    By sak_java in forum Java Applets
    Replies: 8
    Last Post: 02-19-2011, 06:56 PM
  4. Replies: 0
    Last Post: 12-09-2010, 05:56 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
  •