Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes drawImage() problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "drawImage() problem" Watch "drawImage() problem" New topic
Author

drawImage() problem

Edmond Corrola
Greenhorn

Joined: May 23, 2008
Posts: 15
Trying to load an image from file and draw it in a JPanel. Just seems to draw a black rectangle instead of the image.

It's a gray scale image, so I thought I could use BufferedImage.TYPE_BYTE_GRAY, i'm not going to be working in color anyway. I will just be lowering the gray scale value from 256 down and evaluating image quality.

Thanks in advance for any help.

[ November 06, 2008: Message edited by: Edmond Corrola ]
[ November 07, 2008: Message edited by: Edmond Corrola ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Why are you initializing the images in the paintComponent method?
Typically they are initialized in the constructor.
Also take a look at the MediaTracker class.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Edmond Corrola
Greenhorn

Joined: May 23, 2008
Posts: 15
Not really looking for anything complicated (media tracker class). Just want to load an image from file and display it on a Jpanel and be able to modify the buffer. As for the initializing, I can swap it into the constructor no problem. Still have the same issue though (I originally had it that way anyway). It's only a couple lines of code to display an image and I don't see what's wrong with it...Do you have any ideas why it won't display an image from file as it is now? Is it because I'm loading the image from file into a buffered image, should it be done this way instead:

Load image to "Image" object
Create BufferedImage same size as image
Create a graphics object from the BufferedImage
Draw Image onto graphics object (essentially populating my once empty buffered image)

Is this the logical way to do it?
[ November 07, 2008: Message edited by: Edmond Corrola ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

How big is the image?
Does it work if you paint the image directly instead of the buffered image?
Edmond Corrola
Greenhorn

Joined: May 23, 2008
Posts: 15
I know what's wrong now. I decided to make use of two graphics objects, one that belongs to the panel in order to display the image. And the other one I associated with my buffered image, so when I draw the image onto it, it gets stored in the buffered image as well. I just wasn't thinking clearly before. So two graphics objects with the image drawn onto it, while one graphics object has an associated buffered image.
 
wood burning stoves
 
subject: drawImage() problem
 
Similar Threads
Colour issue (only shows grey)
Question about drawimage
Rotating Squares in java
Basic "Magnifier"
Java Numbers