Solved

drawImage method only paints partial image (when image is loaded from Jar)

Posted on 2002-07-19
4
260 Views
Last Modified: 2010-05-18
I have a Java application that loads gif images and paints them in a JPanel.  It works great until I deploy that application in a Jar, then it only paints the top 25% of the image.  I've tried using both a compressed jar and an uncompressed jar, and the results are the same.

I am using MediaTracker to ensure that the image is completely loaded with no errors before I paint the image.  But the image still doens't display correctly.



0
Comment
Question by:AGE_Nicolls
  • 2
4 Comments
 
LVL 1

Author Comment

by:AGE_Nicolls
ID: 7166024
Never mind.  I've figured it out.  For those interested, here is what I've found:

I was loading images as follows:

==========================================================
Image img;
InputStream in = getClass().getResourceAsStream("x.gif");
byte[] bytes = new byte[in.available()];
in.read(bytes);
img = Toolkit.getDefaultToolkit().createImage(bytes);
==========================================================                

When I changed the code to use getResource() instead of getResourceAsStream - it not only fixed the prblem, but simplified my code as well.


==========================================================
URL resUrl = getClass().getResource(resourceName);
img = Toolkit.getDefaultToolkit().createImage(resUrl);
==========================================================

I'd like to delete this question, but I'm not sure how...

-Matt
0
 
LVL 92

Expert Comment

by:objects
ID: 7166187
in.read(bytes);

Your problem was that the line above does not necessarily read all the bytes.
0
 
LVL 1

Author Comment

by:AGE_Nicolls
ID: 7169669
That's interesting...  When I checked the size of the byte array, it matched the size of the file.  I'll keep that in mind in the future though.

-Matt
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 7170537
points refunded and placed in PAQ

Computer101
E-E Moderator
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now