Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2003

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Marco - 23 Nov 2003 18:01 GMT
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?
Marco - 23 Nov 2003 18:03 GMT
> 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
VisionSet - 23 Nov 2003 19:56 GMT
> 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

ak - 23 Nov 2003 21:03 GMT
> 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);
}
Marco - 24 Nov 2003 14:55 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.