Endrju
Endrju

Reputation: 1

java applet display image from localhost

I have an image on Apache server that is oversaved 10 times in second by programm in cpp. I would like to display on website this image that looks like animation. I wrote this script and its works in applet viewer in netbeans, but when I place class on the website, then display image and doesn't change. I spent lot of time on this and it doesn't work. Could anyone help me ?

public class poprawki extends Applet implements Runnable {
    Thread thread1;
    boolean running = true;
    BufferedImage obraz = null;

    public void paint(Graphics g)     {
        g.drawImage(obraz, 10, 10, null);
    }

    public void wyswietlanie_obrazu() {
        try {
            obraz =ImageIO.read(new URL("http://localhost/obraz.jpg"));
            repaint();
        } catch (IOException e) {
        }
    }

    public void init() {
        setLayout(null);

        thread1 = new Thread(this);
        thread1.start();

        repaint();
    }

    public void destroy() {
        running = false;
        thread1 = null;
    }

    public void run() {
        while (running) {
            try {
                wyswietlanie_obrazu();
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println(e);
            }
        }
    }
}

Upvotes: 0

Views: 466

Answers (1)

JB Nizet
JB Nizet

Reputation: 691765

First, as noted in the comments, don't ignore exceptions. If you didn't ignore it, you would get an error message that would explain what the problem is. Instead, by ignoring it, you put yourself in the dark.

If you run this applet on a machine which is not the apache server, then obviously it won't work. localhost is... the local host, i.e. the host where the applet runs, and not the host of the web server.

Use Applet.getDocumentBase() to get an URL pointing to the web server where the applet comes from.

Upvotes: 2

Related Questions