AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

to insert an image in a Label (awt, not swing)

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
Marco
Guest





PostPosted: Sun Nov 23, 2003 6:01 pm    Post subject: to insert an image in a Label (awt, not swing) Reply with quote



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
don't support it).

This is a part of my "class finestra extends Frame":

Image img = vuota.getToolkit().getImage(new
URL("http://www.pippo.it/pluto.jpg"));
ImageProducer im = img.getSource();
vuota.createImage(im); // but the image is still not visible

Graphics g = vuota.getGraphics();
g.drawImage(img,0,0,this);

// appletviewer reports "java.lang.NullPointerException": g is null.

And now? How I must proceed?




Back to top
Marco
Guest





PostPosted: Sun Nov 23, 2003 6:03 pm    Post subject: Re: to insert an image in a Label (awt, not swing) Reply with quote



Quote:
Image img = vuota.getToolkit().getImage(new
URL("http://www.pippo.it/pluto.jpg"));

P.S. "vuota" is a Label.



Back to top
VisionSet
Guest





PostPosted: Sun Nov 23, 2003 7:56 pm    Post subject: Re: to insert an image in a Label (awt, not swing) Reply with quote





"Marco" <mNmOeScPgAfMl (AT) excite (DOT) it> wrote


Quote:
Image img = vuota.getToolkit().getImage(new
URL("http://www.pippo.it/pluto.jpg"));
ImageProducer im = img.getSource();
vuota.createImage(im); // but the image is still not visible

Graphics g = vuota.getGraphics();
g.drawImage(img,0,0,this);

// appletviewer reports "java.lang.NullPointerException": g is null.

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.

--
Mike W



Back to top
Andrew Thompson
Guest





PostPosted: Sun Nov 23, 2003 8:39 pm    Post subject: Re: to insert an image in a Label (awt, not swing) Reply with quote

A complete, self contained, compilable example would
be great, whereas 'code snippets' are ..not

"Marco" <mNmOeScPgAfMl (AT) excite (DOT) it> wrote

Quote:
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



Back to top
ak
Guest





PostPosted: Sun Nov 23, 2003 9:03 pm    Post subject: Re: to insert an image in a Label (awt, not swing) Reply with quote


"Marco" <mNmOeScPgAfMl (AT) excite (DOT) it> schrieb im Newsbeitrag
news:bpqsmp$o27$1 (AT) lacerta (DOT) tiscalinet.it...
Quote:
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
don't support it).

This is a part of my "class finestra extends Frame":

Image img = vuota.getToolkit().getImage(new
URL("http://www.pippo.it/pluto.jpg"));
ImageProducer im = img.getSource();
vuota.createImage(im); // but the image is still not visible

Graphics g = vuota.getGraphics();
g.drawImage(img,0,0,this);

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



Back to top
Marco
Guest





PostPosted: Mon Nov 24, 2003 2:55 pm    Post subject: Re: to insert an image in a Label (awt, not swing) Reply with quote

Quote:
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?

Quote:
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).

Quote:
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)




Back to top
Adam
Guest





PostPosted: Wed Nov 26, 2003 8:07 am    Post subject: Re: to insert an image in a Label (awt, not swing) Reply with quote


Quote:
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

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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.