Ofek Ron
Ofek Ron

Reputation: 8578

Images in applets

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);
    }
}

Upvotes: 1

Views: 139

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

Try passing the applet context to the drawImage method, this allows the appet to be notified when the image is loaded and it will repaint itself

g2.drawImage(image,100,100,100, 100, this);

And, if you're really desperate, call invalidate() repaint() in the start method

UPDATE WITH EXAMPLE

This is the code that I used. I had no issue with loading the image:

public class TestApplet extends JApplet {

    public void init() {

        System.out.println("Init..." + EventQueue.isDispatchThread());

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createGUI();
            }
        });

    }

    public void start() {

        System.out.println("Start..." + EventQueue.isDispatchThread());

    }

    public void createGUI() {

        URL documentBase = getDocumentBase();

        System.out.println(documentBase);
        System.out.println(getCodeBase());

//        file:.../build/TestApplet.html
//        file:.../build/classes/        
        Image image = getImage(documentBase, "MT-vol-6-STOP-image-Kogumiko-megatokyo-11434773-1559-1852.jpg");

        System.out.println("image = " + image);

        getContentPane().add(new GUIThing(image));
    }

    public class GUIThing extends JPanel {

        Image image;

        public GUIThing(Image i2) {
            image = i2;
        }

        public void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g;
            g2.drawImage(image, 0, 0, this);

            g2.setColor(Color.RED);
            g2.drawLine(0, 0, getWidth(), getHeight());

        }
    }

}

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 1559x1852 pixels with little to no delay (locally)

Double check your applet tag in you HTML file as well. I was using

<APPLET codebase="classes" code="stack/TestApplet.class" width=350 height=200></APPLET>

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

Upvotes: 1

Related Questions