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.
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:
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.
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.
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.
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.
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?
Re: JApplet doesn't load in browser?
When I load my applet, I use the .class file...
This is the HTML code I used.
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.