Results 1 to 5 of 5
  1. #1
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default JApplet doesn't load in browser?

    Hello, I'm trying to load an applet in a browser. I have done this many times using the applet tag. But this time, I'm getting an error.
    The error reads

    Application error
    RuntimeException
    java.lang.reflect.InvocationTargetException

    I've used Eclipse, readytoprogram and NetBeans to run it and none of them showed me that there is anything wrong with my code. Yet, I'm unable to load it with a browser.

    I have done research but for now, I'll post the codes that I believe is related to it.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Example extends JApplet{
    
            ImageIcon BG = new ImageIcon("BG.png"); //adding the Image I want into an ImageIcon
           
            JLabel LBG; //JLabel so I can put the ImageIcon into it later
     
                private JLayeredPane lpane = new JLayeredPane(); //The Layeredpane so I can add images on top of each other
    
                
                    public void init()//Initialize 
                    {
                            super.setSize(760,530);
                            super.getContentPane().setLayout(new BorderLayout());
                    super.getContentPane().add(lpane);
      
                    //I will Store the imageIcon into the JLabel while changing the size of the Image
                    LBG = new JLabel(BG){
    
                            public void paintComponent (Graphics g) {
                            if (BG != null) {
                                       g.drawImage (BG.getImage(), 0, 0, 760, 530, this);
                                                }
                                           }
                                        };     
      
                    LBG.setBounds(0, 0, 760, 530);
                    LBG.setOpaque(false);
                    
               
                    lpane.add(LBG, new Integer(0), 0);
                    
     
                    }                
    }
    So a bit of playing around with the codes and research made me conclude the problem occurred when I added the ImageIcon variable. I believe that I'm supposed to have this code:
    Java Code:
    try{
    ImageIcon BG = new ImageIcon("BG.png");
    }
    catch(Exception e){}
    But the problem is that I'm unable to do that where the code is. And I can't move it into init() because then there will be errors.
    Well anyways, if my conclusion is correct, I don't know what to do.
    and if it is wrong... I'll still need help. X.x
    Thanks in advance.

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

    Default Re: JApplet doesn't load in browser?

    You're again asking volunteers to put in the effort to help you and yet you've not put in the effort to post well-formatted code. As before, please correct your code's formatting. I can't speak for others, but I plan to wait for easy to read code before addressing any of your code issues.

  3. #3
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: JApplet doesn't load in browser?

    I'm sorry for not satisfying you with an easy to read code. I was sure that it was better than the formatted code yesterday. And yes, I have added effort. Anyways, I'll try again.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Example extends JApplet
    {
        ImageIcon BG = new ImageIcon ("BG.png"); //adding the Image I want into an ImageIcon
        JLabel LBG; //JLabel so I can put the ImageIcon into it later
        JLayeredPane lpane = new JLayeredPane (); //The Layeredpane so I can add images on top of each other
    
        public void init ()  //Initialize
        {
            super.setSize (760, 530);
            super.getContentPane ().setLayout (new BorderLayout ());
            super.getContentPane ().add (lpane);
    
            //I will Store the imageIcon into the JLabel while changing the size of the Image
            LBG = new JLabel (BG)
            {
                public void paintComponent (Graphics g)
                {
                    if (BG != null)
                    {
                        g.drawImage (BG.getImage (), 0, 0, 760, 530, this);
                    }
                }
            };
    
            LBG.setBounds (0, 0, 760, 530);
            LBG.setOpaque (false);
            lpane.add (LBG, new Integer (0), 0);
    
        }
    }
    I hope this is better.

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

    Default Re: JApplet doesn't load in browser?

    When you load the applet, do you use the .class file or a .jar file? Can you show the HTML code you use to display the applet? You seem to be double-drawing your ImageIcon still, I'm curious why you're doing this?

  5. #5
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: JApplet doesn't load in browser?

    When I load my applet, I use the .class file...
    This is the HTML code I used.
    Java Code:
    <html>
        <body>
            <applet width="760" height="530" code="Test.class">
            </applet>
        </body>
    </html>

    And I seem to be double drawing again? I thought I fixed that problem. Well now that I added System.out.println(), it does seem that it goes through the paintComponent twice. But it only paints once.
    I kinda played around with the code again and it seems that it goes through the paintComponent once but does nothing, then goes through it again and paints what I want.

Similar Threads

  1. Converting JFrame to JApplet is not showing on browser.
    By ganesh.gothi@gmail.com in forum Java Applets
    Replies: 4
    Last Post: 06-12-2012, 06:05 PM
  2. Replies: 5
    Last Post: 03-11-2012, 03:13 PM
  3. JApplet connecting to the database in browser
    By pinxrick in forum Java Applets
    Replies: 3
    Last Post: 03-22-2011, 05:12 AM
  4. japplet - jtable - browser
    By jasonnuigi in forum Java Applets
    Replies: 1
    Last Post: 12-31-2010, 03:16 PM
  5. JApplet.getGraphics() draws but JFrame doesn't
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 07-27-2009, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •