> Image img = vuota.getToolkit().getImage(new
> URL("http://www.pippo.it/pluto.jpg"));
P.S. "vuota" is a Label.
Andrew Thompson - 23 Nov 2003 20:39 GMT
A complete, self contained, compilable example would
be great, whereas 'code snippets' are ..not
> > Image img = vuota.getToolkit().getImage(new
> > URL("http://www.pippo.it/pluto.jpg"));
>
> P.S. "vuota" is a Label.
My understanding is that JLabels can accept images,
but Labels cannot.
I realise what you are doing is not quite standard,
but if your are not using a Label as a Label, you
may as well _not_ use it, but instead draw your
graphics directly to a Panel or some other more
appropriate component.
[ At this point I am hoping the GUI gurus can
jump in to clarify, this is not one of my main
areas.. ]
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
> Image img = vuota.getToolkit().getImage(new
> URL("http://www.pippo.it/pluto.jpg"));
[quoted text clipped - 7 lines]
>
> And now? How I must proceed?
If I remember correctly createImage returns immediately so it won't have
loaded the image when it returns and will therefore have a null graphics
context.
You can use MediaTracker (example of useage in API) to wait for the image to
load.

Signature
Mike W
> I want insert a JPG (or jpg?) image in a Label (the Label is a Component of
> a Frame; I don't want to use swing for compatibility with browsers that
[quoted text clipped - 11 lines]
>
> // appletviewer reports "java.lang.NullPointerException": g is null.
g is null means that your Panel is not displayable!
use instead paint(Graphics g) method to draw your image:
public void paint(Graphics g) {
super.paint(g);
g.drawImage(img,0,0,this);
}
> My understanding is that JLabels can accept images,
> but Labels cannot.
It is possible, I don't know well, but I read in
http://programmazione.html.it/java/java_30.htm that
"volendo disegnare una immagine in un componente Gui, si pu? usare il metodo
createImage() della classe Component" (for drawing an image in a Gui
component, it is possible to use the method createImage() of the class
Component). A Label is a Component. But how to use createImage?
> draw your graphics directly to a Panel
I tested this, but the result is the same: the graphic context g is null,
and the image is not visible (or it is visible only a moment, after the
second display of the Frame).
> If I remember correctly createImage returns immediately so it won't have
> loaded the image when it returns and will therefore have a null graphics
> context.
> You can use MediaTracker (example of useage in API) to wait for the image
to
> load.
I have used MediaTracker too, but the image is visible only for a moment,
from the second display of the frame. With Canvas, the graphic context is
always null.
FOR DISPLAYING THE IMAGE IN A COMPONENT OF A FRAME (NON A JFRAME), WHAT IS
THE PROCEDURE? HOW CAN I KNOW THE GRAPHIC CONTEXT g OF THE COMPONENT FOR
USING g.drawImage? OR THERE IS AN ANOTHER PROCEDURE?
Thank you for your help.
Marco
(remove NOSPAM or reply in this newsgroup)
Adam - 26 Nov 2003 08:07 GMT
> FOR DISPLAYING THE IMAGE IN A COMPONENT OF A FRAME (NON A JFRAME), WHAT IS
> THE PROCEDURE? HOW CAN I KNOW THE GRAPHIC CONTEXT g OF THE COMPONENT FOR
[quoted text clipped - 3 lines]
>
> Marco
Poor Marco... I don't think anyone will help you after that...
And you were given specific instructions to
render your image in paint method.
You just try to render it once, even when it can't be rendered.
But to keep your image on screen you need to render
it during every repaint.
So:
1. Subclass the component on which you want to render the image
2. override its paint method as you were told
3. Load the image in the constructor of your component
Oh, one more thing. createImage is used to create offscreen images for
double buffering (in general).
For loading external image use Toolkit.getImage and MediaTracker
regs,
Adam