How to load image file in Applet?

J

Jenny

Hi,

I need an applet loads and displays an image which is stored on server
side.
My applet works fine if I open the html file in my browser by "Open
File", but it doesn't load the image if my browser accesses it by URL
(such as http://127.0.0.1/test.html).
I think it is the problem of assigning the image file path, I am
frustrated by this problem. Can anyone help me? Thanks a lot.

This is the way my applet loads an image file:

private DisplayJAI display = null;
private PlanarImage image = null;
private JScrollPane scroll = null;
// assign fileName with absolute file path
String fileName="/var/www/localhost/htdocs/shape/006_2.jpg";
// I need JAI for image proccessing later
image = JAI.create("fileload", fileName);
scroll = new JScrollPane();
display = new DisplayJAI(image);
scroll.setViewportView(display);

This is my entire code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import com.sun.media.jai.widget.DisplayJAI;
import java.io.File;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

public class AppletROI extends JApplet {
private JButton
bFlower = new JButton("Flower"),
bLeaf = new JButton("Leaf"),
bFruit = new JButton("Fruit"),
bReset = new JButton("Reset ROI");

private ActionListener bl = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = ((JButton)e.getSource()).getText();

}
};
private DisplayJAI display = null;
private PlanarImage image = null;
private JScrollPane scroll = null;
String fileName="/var/www/localhost/htdocs/shape/006_2.jpg";

public void init() {
image = JAI.create("fileload", fileName);
scroll = new JScrollPane();
display = new DisplayJAI(image);
scroll.setViewportView(display);


bFlower.addActionListener(bl);
bLeaf.addActionListener(bl);
bFruit.addActionListener(bl);
bReset.addActionListener(bl);

Container cp = getContentPane();
cp.setLayout(new BorderLayout());
JPanel cpFlower = new JPanel();
cpFlower.setLayout(new BorderLayout());
JPanel cpLeaf = new JPanel();
cpLeaf.setLayout(new BorderLayout());
JPanel cpFruit = new JPanel();
cpFruit.setLayout(new BorderLayout());

cpFlower.add(BorderLayout.NORTH, bFlower);
cpLeaf.add(BorderLayout.NORTH, bLeaf);
cpFruit.add(BorderLayout.NORTH, bFruit);

JPanel cpButtons = new JPanel();
cpButtons.setLayout(new FlowLayout());
cpButtons.add(cpFlower);
cpButtons.add(cpLeaf);
cpButtons.add(cpFruit);
cpButtons.add(bReset);

cp.add(BorderLayout.SOUTH, cpButtons);
cp.add(BorderLayout.CENTER, scroll);
}
}
 
A

Andrew Thompson

I need an applet loads and displays an image which is stored on server
side.
My applet works fine if I open the html file in my browser by "Open
File", but it doesn't load the image if my browser accesses it by URL
(such as http://127.0.0.1/test.html).

What is in the Java console?
I think it is the problem of assigning the image file path,

Stop guessing, look at the console
for stacktraces.
..I am
frustrated by this problem. Can anyone help me? Thanks a lot.

It may be a SecurityAccessException
since you seem to be dealing with files,
are you aware of the security sandbox
that applets run in?

BTW - it seems you are going to a
lot of effort to display an image in an
applet, why not just use a JLabel?

And, while we are here, please do not cross-post..
<http://www.physci.org/codes/javafaq.jsp#xpost>

F'Up's set to c.l.j.help
 
S

Sam

Roedy Green said:
See http://mindprod.com/jgloss/image.html

The ImageViewer class will help you display an Image in an Applet.

Usually you put the image in the jar along with the Applet and get it
with getResource.

If you would want your applet to run an executable that lives in the
server, would you do this in the same way? Because I've been trying to
achieve the same type of thing, but I can't get it to work.

My problem is a bit different, I'm trying to build a webservice which
needs to run this executable, but I don't know how to tell the
webservice where the exe lives. Just for testing purposes I tried to
locate a txtfile with soemthing like the following

URL url = this.getClass().getResource("testfile.txt");
String sam = url.toString();
return sam;

but I get an error... I don't know where to put the testfile.txt file
in order for the webservice to find it though
 
A

Andrew Thompson

On 22 Apr 2004 08:18:47 -0700, Sam wrote:
....
If you would want your applet to run an executable that lives in the
server, would you do this in the same way?

No. But..

That is a completely separate thread and probably
should not be tacked on this thread.
....Because I've been trying to
achieve the same type of thing, but I can't get it to work.

I very much doubt you will.
My problem is a bit different,

Hence 'different thread'..
..I'm trying to build a webservice which
needs to run this executable,

Stop there. An applet?

You would need to send a signal to your
server, and, only with it's co-operation,
could you launch an exe on the server..

Alternatively, you might look to RMI
or such, but that would require a
signed applet with all permissions.
..but I don't know how to tell the
webservice where the exe lives. Just for testing purposes I tried to
locate a txtfile with soemthing like the following


Don't waste our bandwidth telling us
'soemthing like' what you did.

Use copy/paste
URL url = this.getClass().getResource("testfile.txt");
String sam = url.toString();
return sam;

but I get an error...

A NotEnoughMuchiesException?,
...KittenOverflowException? What?
<http://www.physci.org/codes/javafaq.jsp#exact>

But do NOT answer this - take those
things into account for YOUR thread.

F'Ups set to c.l.j.help
 
R

Roedy Green

If you would want your applet to run an executable that lives in the
server, would you do this in the same way? Because I've been trying to
achieve the same type of thing, but I can't get it to work.

Your question has several possible interpretations.

Which of these are you trying to do?

1. Have your applet run on the client. The class files for the Applet
live on the server. In this case bundling everything up as a jar is by
far the easiest route. See http://mindprod.com/jgloss/applet.html and
follow links.

2. If you want your applet to span a native exe file that both lives
and runs on the server, you will need to write a servlet that does an
exec.

3. If you want your applet to run a native exe file that lives on the
server but will run on the client, you first of all have to get it
installed on the client. Using Java Web Start will handle this
automatically for you. With Applets, you must unpack the jar, find a
place to put the exe, and then exec. This requires signed Applets and
a full head of hair to pull out.
 
A

Andrew Thompson

On Thu, 22 Apr 2004 16:58:46 GMT, Roedy Green wrote:

(applet run native exe)
...This requires signed Applets and
a full head of hair to pull out.

;-) Good one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top