For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2004 > How to load image file in Applet?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to load image file in Applet?
Jenny

2004-04-22, 1:37 am

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

2004-04-22, 2:38 am

On 21 Apr 2004 21:38:43 -0700, jennybabee111@yahoo.com.au (Jenny)
wrote or quoted :

>I need an applet loads and displays an image which is stored on server
>side.


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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Andrew Thompson

2004-04-22, 9:37 am

On 21 Apr 2004 21:38:43 -0700, Jenny wrote:

> 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

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Sam

2004-04-22, 12:41 pm

Roedy Green <see@mindprod.com.invalid> wrote in message news:<7hme8051eujjhj2v3qg9bs05tg3ar8h5bb@4ax.com>...
> On 21 Apr 2004 21:38:43 -0700, jennybabee111@yahoo.com.au (Jenny)
> wrote or quoted :
>
>
> 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
Andrew Thompson

2004-04-22, 1:34 pm

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

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Roedy Green

2004-04-22, 1:34 pm

On 22 Apr 2004 08:18:47 -0700, sam@edup.tudelft.nl (Sam) wrote or
quoted :

>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.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green

2004-04-22, 1:34 pm

On 22 Apr 2004 08:18:47 -0700, sam@edup.tudelft.nl (Sam) wrote or
quoted :

>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


See examples of fetching resources out of jars in
http://mindprod.com/jgloss/image.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Roedy Green

2004-04-22, 2:51 pm

On Thu, 22 Apr 2004 16:57:30 GMT, Andrew Thompson
<SeeMySites@www.invalid> wrote or quoted :

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


If we were face to face at a Java User Group and a newbie asked you
that question, would you respond that curtly?

This is the novice group. Please cut them some slack.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Andrew Thompson

2004-04-22, 2:51 pm

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.
Andrew Thompson

2004-04-22, 3:51 pm

On Thu, 22 Apr 2004 18:16:01 GMT, Roedy Green wrote:

> On Thu, 22 Apr 2004 16:57:30 GMT, Andrew Thompson

....
>
> If we were face to face at a Java User Group and a newbie asked you
> that question, would you respond that curtly?
>
> This is the novice group. Please cut them some slack.


(whince) Sorry!
Forgot where I was. :-(

[ But probably, yes, ..at least then
they could see the grin on my face
as I was saying it. ;-) ]

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com