Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-07-19
4
Medium Priority
?
308 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

876 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