i wrote this code for showing some simple picture on the applet screen, but the picture doesnt show untill i resize the applet window, what can i do to overcome it?
public class Test extends JApplet {
public void init () {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
public void start() {
}
public void createGUI() {
getContentPane().add(new GUIThing(getImage(getCodeBase(), "gladiator.gif")));
}
}
public class GUIThing extends JPanel {
Image image;
public GUIThing(Image i2) {
image=i2;
}
public void paintComponent(Graphics g) {
Graphics2D g2=(Graphics2D) g;
g2.drawImage(image,100,100,100, 100, null);
}
}
Try passing the applet context to the
drawImagemethod, this allows the appet to be notified when the image is loaded and it will repaint itselfAnd, if you’re really desperate, call
invalidate()repaint()in thestartmethodUPDATE WITH EXAMPLE
This is the code that I used. I had no issue with loading the image:
Wow, it’s been a long time since I played with applets. I changed your code from using the code base to the document base it seems to have worked. I was loading an image of 1559×1852 pixels with little to no delay (locally)
Double check your applet tag in you HTML file as well. I was using
Through the applet viewer. Note the code base is different from the document base!!
My image was in the same location as the HTML file