Solved

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

Posted on 2002-07-19
4
272 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 39
Apps blocked by Java 9 64
difference of if loops 23 43
How to convert String matching to regex in java 4 21
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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

862 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

25 Experts available now in Live!

Get 1:1 Help Now