Solved

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

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTPSessionBindingListernter not configured in deployment descriptor 2 53
web application structure 18 77
Java string replace 11 47
going to wrong jsp page 2 21
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

778 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