🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #317

Re: Problems with loading & displaying a picture

#317
From: 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
Technical Details
Message-ID: <pan.2004.02.02.08.34.02.603265@yahoo.de>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <401d06eb$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,cc9d3c5ba1462852
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2004-02-02 00:39:30 PST
From: marcin brzoza <marcin4@yahoo.de>
Subject: Re: Problems with loading & displaying a picture
Date: Mon, 02 Feb 2004 09:34:03 +0100
User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity. (Debian GNU/Linux))
Message-ID: <pan.2004.02.02.08.34.02.603265@yahoo.de>
Newsgroups: borland.public.jbuilder.java.api
References: <401d06eb$1@newsgroups.borland.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: 80.141.58.96
X-Trace: newsgroups.borland.com 1075710914 80.141.58.96 (2 Feb 2004 00:35:14 -0800)
Lines: 88
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail