🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.jbuilder.java.api
7 messages
7 total messages Started by Andreas Koch Sun, 01 Feb 2004 14:05
Problems with loading & displaying a picture
#316
Author: Andreas Koch
Date: Sun, 01 Feb 2004 14:05
51 lines
2512 bytes
Hi,

i am doing my first steps in java by combining
snippets from the net.

Currently i have an expanded JB9 default application
with an extra frame that shows a clock (using
a BufferedImage re-painted several times a second).

Setting pixels in the BufferedImage works.
Writing text on the BufferedImage works too.

Now i loaded a small jpeg (from the local harddisk,
32x32 pixels) and want to draw it into the BufferedImage.

Unfortunately, drawImage allways returns false - the docs
state that this is the case if the picture isn't fully
loaded. But its on a local drive, and it keeps on return
false for minutes!

The (in my eyes) relevant code snippets:

class Clock extends JPanel {
...
     private static Image white_marble;
...
     public Clock() {
....
         Toolkit toolkit = Toolkit.getDefaultToolkit();
         white_marble = toolkit.getImage("0196.jpg");
     }//end constructor
...

         if (!gc.drawImage(white_marble,32,32,32,32,null))
           gc.drawString("<loading>",32,32);


result is "<loading>" displayed instead of the image.

"0196.jpg" is in the source folder, and added to the
project. After getImage, white_marble contains an
image object, but width.int and height.int are -1.

Any hints what to do, or how to find out whats wrong?
Do i need to use an ImageObserver or Mediatracker, and
if yes, how ?

                      thanks a lot,
-- 
                                   Andreas
He screamed: THIS IS SIG!
Re: Problems with loading & displaying a picture
#317
Author: marcin brzoza
Date: Mon, 02 Feb 2004 08:34
88 lines
3765 bytes
hi,

On Sun, 01 Feb 2004 15:05:34 +0100, Andreas Koch wrote:

> Hi,
> 
> i am doing my first steps in java by combining snippets from the net.
> 
> Currently i have an expanded JB9 default application with an extra frame
> that shows a clock (using a BufferedImage re-painted several times a
> second).
> 
> Setting pixels in the BufferedImage works. Writing text on the
> BufferedImage works too.
> 
> Now i loaded a small jpeg (from the local harddisk, 32x32 pixels) and want
> to draw it into the BufferedImage.
> 
> Unfortunately, drawImage allways returns false - the docs state that this
> is the case if the picture isn't fully loaded. But its on a local drive,
> and it keeps on return false for minutes!
> 
> The (in my eyes) relevant code snippets:
> 
> class Clock extends JPanel {
> ...
>      private static Image white_marble;
> ...
>      public Clock() {
> ....
>          Toolkit toolkit = Toolkit.getDefaultToolkit(); white_marble =
>          toolkit.getImage("0196.jpg");
>      }//end constructor
> ...
> 
>          if (!gc.drawImage(white_marble,32,32,32,32,null))
>            gc.drawString("<loading>",32,32);
> 
> 
> result is "<loading>" displayed instead of the image.
> 
> "0196.jpg" is in the source folder, and added to the project. After
> getImage, white_marble contains an image object, but width.int and
> height.int are -1.
> 
> Any hints what to do, or how to find out whats wrong? Do i need to use an
> ImageObserver or Mediatracker, and if yes, how ?

You can use ClassLoader.getResource or MediaTracker.

Example for using  ClassLoader.getResource:
  BufferedImage bufferedImage = null;
  ImageIcon image = null;
  try
  {
    ClassLoader loader = this.getClassLoader();
    image = new ImageIcon(loader.getResource("PATH_IN_CLASSPATH"+fileName));
    bufferedImage = new BufferedImage(w, h,BufferedImage.TYPE_4BYTE_ABGR);
    Graphics graphics = bufferedImage.getGraphics();
    graphics.drawImage(image, 0, 0, w, h, null);
  }
  catch (Exception ex)
  {
    ex.printStackTrace();
  }

Example for using MediaTracker:
  BufferedImage bufferedImage = null;
  Toolkit toolkit = Toolkit.getDefaultToolkit();
  Image image = toolkit.getImage("[ABSOLUTE_PATH]/BLSio1600x1200.jpg");
  MediaTracker mediaTracker = new MediaTracker(this);
  mediaTracker.addImage(image, 0);
  try
  {
    mediaTracker.waitForID(0);
    int w = image.getWidth(null);
    int h = image.getHeight(null);
    bufferedImage = new BufferedImage(w, h,BufferedImage.TYPE_4BYTE_ABGR);
    Graphics graphics = bufferedImage.getGraphics();
    graphics.drawImage(image, 0, 0, w, h, null);
  }
  catch (Exception ex)
  {
    ex.printStackTrace();
  }

good luck
marcin
Re: Problems with loading & displaying a picture
#318
Author: Andreas Koch
Date: Mon, 02 Feb 2004 19:00
28 lines
1992 bytes
Thanks for your answers!

Unfortunately i can't compile the method using Classloader.
Seems JPanel has no getClassLoader, and drawImage(I,int,int,
int,int,<null type>) doesn't take ImageIcons.


The second method compiles, and finaly works after i specify the full, 
absolute path and filename of the picture (as does my original version
modified to the full path)

Doesn't work:
        white_marble = toolkit.getImage("0196.jpg");

Works:
        white_marble = 
toolkit.getImage("c:/projects/test2/classes/test2/0196.jpg");

The images are in my src\test2 folder, and get copied to classes\test2 
on compilation.

What relative path would i have to use to get the images (so it
works on other systems and inside a .jar) ?

                         thanks a lot,
-- 
                                   Andreas
He screamed: THIS IS SIG!
Re: Problems with loading & displaying a picture
#319
Author: Paul Furbacher
Date: Mon, 02 Feb 2004 20:45
74 lines
3310 bytes
Andreas Koch wrote:

> Unfortunately i can't compile the method using Classloader.
> Seems JPanel has no getClassLoader, and drawImage(I,int,int,
> int,int,<null type>) doesn't take ImageIcons.

For some reason, this topic on how to load images
has come up a lot recently.  Please consult the
archives for a rather lengthy message on this issue.
Go to www.tamaracka.com and use the exact keywords
(including the circumflex):

   ^jbuilder furbacher images directories

That thread does indeed apply to your case.
Make sure that you adopt the proper method
of packaging your images.  They must be on
the classpath.  The best place to locate
them in your classpath/package structure is
relative to the package/directory of the
class which is going to load the images.

For instance, if you have the package and class

   org.myorg.views.MyPanel

and MyPanel uses some images (e.g., image1.png
and image2.png), the best packaging/structuring
of these images is

   // given a root folder for your project, MyProject

   .../MyProject
        /bak
        /doc
        /classes
        /src
          /org
            /myorg
               /views
                 - MyPanel.java
                 /images
                   - image1.png
                   - image2.png

Given this packaging/directory structure, you
would get your Image using

   URL url = MyPanel.class.getResource("images/image1.png");
   // with the URL, you can now get your ImageIcon.

Also, it might be a good idea to drill down into
the code for ImageIcon, flip to the "Doc" tab
in the Editor, and click on the "How to Use Icons"
link in the JavaDoc header comments.  Study very
thoroughly the "Loading Images Using getResource"
section of that tutorial.  Understanding this is
absolutely essential.


-- 


Paul Furbacher (TeamB)

   Save time, search the archives:
        http://www.borland.com/newsgroups/ngsearch.html

   Is it in Joi Ellis's Faq-O-Matic?
        http://www.visi.com/~gyles19/fom-serve/cache/1.html

   Finally, please send responses to the newsgroup only.
   That means, do not send email directly to me.
   Thank you.
Re: Problems with loading & displaying a picture
#320
Author: Andreas Koch
Date: Mon, 02 Feb 2004 21:29
94 lines
3334 bytes
Thanks a lot for your reply.

>   ^jbuilder furbacher images directories

Thanks, had a quick look on it.

> They must be on the classpath.

Forgive my stupidy. I have about 5 hours of java
experience. What exactly is the "classpath"?

/MyProject/classes ?


> For instance, if you have the package and class
> 
>   org.myorg.views.MyPanel

If have the project test2.jpx which has a class
named ClockAnalogBuf.java.
No idea what a package is :-)

> and MyPanel uses some images (e.g., image1.png
> and image2.png), 

0196.png etc in my case.

> the best packaging/structuring
> of these images is
> 
>   // given a root folder for your project, MyProject
> 
>   .../MyProject
>        /bak
>        /doc
>        /classes
>        /src
>          /org
>            /myorg
>               /views
>                 - MyPanel.java
>                 /images
>                   - image1.png
>                   - image2.png

Ok, i put my images in two (identical) images
subfolders:

/test2
   /bak
   /classes
     /package cache
       *test2.dep2
     /test2
       *ClockAnalogBuf.class
       ...
       /images
         *0196.png
         ...
   /doc
   /src
     /test2
       *ClockAnalogBuf.java
       ...
       /images
         *0196.png
         ...

> Given this packaging/directory structure, you
> would get your Image using
> 
>   URL url = MyPanel.class.getResource("images/image1.png");
>   // with the URL, you can now get your ImageIcon.

Doesn't work:
         TileGfx[0] = toolkit.getImage("/images/0196.png");
Doesn't work:
         TileGfx[0] = toolkit.getImage("images/0196.png");
Works:
         TileGfx[0] = 
toolkit.getImage("c:/projects/test2/classes/test2/images/0196.png");

Is there any difference in the file name handling of
getResource vs. toolkit.getImage ?

> Also, it might be a good idea to drill down into
> the code for ImageIcon

Quite lengthy. I'll take a look at it tomorrow :-D


-- 
                                   Andreas
He screamed: THIS IS SIG!
Re: Problems with loading & displaying a picture
#323
Author: marcin brzoza
Date: Tue, 03 Feb 2004 08:00
76 lines
3468 bytes
hi,

Errata:
WRONG: ClassLoader loader = this.getClassLoader();
OK: ClassLoader loader = this.getClass().getClassLoader();
sorry :)

On Mon, 02 Feb 2004 22:29:57 +0100, Andreas Koch wrote:

>> They must be on the classpath.
> Forgive my stupidy. I have about 5 hours of java experience. What exactly
> is the "classpath"?
Classpath is like path ;) More about (in german):
http://uebb.cs.tu-berlin.de/infet/classpath.html

> /MyProject/classes ?
No, it is output directory for JBuilder. You can set it, ex.:
SET CLASSPATH=c:\projects\test2\classes\;%CLASSPATH%
BTW: JBuilder do it (in Messages: 'java -classpath "..."'), if you runs
the application from IDE ;)

>> For instance, if you have the package and class
>>   org.myorg.views.MyPanel
> If have the project test2.jpx which has a class named
> ClockAnalogBuf.java. No idea what a package is :-)
Package is like Delphi Unit, is defined... as first in your class, ex.:
package test2;

> Ok, i put my images in two (identical) images subfolders:
> 
> /test2
>    /bak
>    /classes
>      /package cache
>        *test2.dep2
>      /test2
>        *ClockAnalogBuf.class
>        ...
>        /images
>          *0196.png
>          ...
>    /doc
>    /src
>      /test2
>        *ClockAnalogBuf.java
>        ...
>        /images
>          *0196.png
>          ...
> 
>> Given this packaging/directory structure, you would get your Image
>> using
>> 
>>   URL url = MyPanel.class.getResource("images/image1.png"); // with the
>>   URL, you can now get your ImageIcon.
> 
> Doesn't work:
>          TileGfx[0] = toolkit.getImage("/images/0196.png");
> Doesn't work:
>          TileGfx[0] = toolkit.getImage("images/0196.png");
The path to image should include the full name of package:
toolkit.getImage("test2/images/0196.png");

> Works:
>          TileGfx[0] =
> toolkit.getImage("c:/projects/test2/classes/test2/images/0196.png");
You are using absolute path.

> Is there any difference in the file name handling of getResource vs.
> toolkit.getImage ?
Yes. If you use toolkit, you must use Mediatracker. With
ClassLoader.getResource() you can read resources (like text files or
images) from classpath or jar files.

good luck
marcin
Re: Problems with loading & displaying a picture
#325
Author: Andreas Koch
Date: Tue, 03 Feb 2004 20:08
30 lines
2170 bytes
marcin brzoza wrote:
> Errata:
> WRONG: ClassLoader loader = this.getClassLoader();
> OK: ClassLoader loader = this.getClass().getClassLoader();
> sorry :)

Ah, perfect. And with

  TileGfx[0] = new 
ImageIcon(loader.getResource("test2/images/0196.png")).getImage();

i can even put it into my Image.


>>Is there any difference in the file name handling of getResource vs.
>>toolkit.getImage ?
> 
> Yes. If you use toolkit, you must use Mediatracker. With
> ClassLoader.getResource() you can read resources (like text files or
> images) from classpath or jar files.

Thanks a lot! The Classloader method works now, even from a JAR !

I think i have to meditate a bit about what the Mediatracker
does but waiting for the picture to load, and why the toolkit
seems to use paths not relative to the classpath ;-)

-- 
                                   Andreas
He screamed: THIS IS SIG!
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads