Solved

Painting GIF Images on to a GUI

Posted on 2004-10-08
19
735 Views
Last Modified: 2008-02-01
I'm trying to paint a GIF image on to a JPanel in a JDialog.

When I first load the dialog it isn't there. However if I click on another application in Windows and then click back on the Dialog then the image appears. Its as if the image can't load fast enough before it tried to paint it on to the screen.

Has anyone come across this before and know the solution? Here is my code:

public class TestDialog_RLP extends JDialog
{
    public TestDialog_RLP (final MainFrame parentFrame) throws Exception
    {
        super ( parentFrame, "TestDialog", true );
       
        try
        {

            testPanel myTestPanel = new testPanel ();
            this.getContentPane().add (myTestPanel);
           
            /////////////////////
            // Display the dialog
            /////////////////////
            setSize ( 800, 600 ); // Make the dialogue a sensible size
            setLocationRelativeTo ( parentFrame );
            setVisible ( true ); // Make the dialogue visible
        }
        catch ( Exception e )
        {
            throw new Exception ( "TestDialog: " + e.getMessage() );
        }
       
    }
   
    public class testPanel extends JPanel
    {
   
    public void paintComponent ( Graphics g )
    {
            super.paintComponent ( g );
           
            Graphics2D g2 = (Graphics2D)g;
           
            try
            {
                Toolkit kit = Toolkit.getDefaultToolkit();
                Image myImage = kit.getImage ( "D:\\Development\\QuickConvey\\QuickConvey\\images\\arrow.gif" );
               
                boolean result = g2.drawImage ( myImage , 50, 50, null );
                System.out.println ("The result of trying to do the image is: " + result );
            }
            catch ( Exception e )
            {
            }
           
    }
   
    }
}
0
Comment
Question by:peparsons
  • 9
  • 6
  • 4
19 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 12257102
Remark:
Don't load your image in paintComponent() !
Do that once and forever in the constructor
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 12257139
You can pass the image path to the ctor of testPanel:

public class TestPanel {
    private String imagePath;
    private  Image image;
    public TestPanel(String imagePath) {
        this.imagePath = imagePath;
        image = ......... // create it as before
    }
    ....

}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12257143
Try something like this:

public class testPanel extends JPanel {
   
    private Image myImage;

    public testPanel() {
         super();
         try  {
            myImage = Toolkit.getDefaultToolkit().getImage ( "D:\\Development\\QuickConvey\\QuickConvey\\images\\arrow.gif" );
         } catch (Exception ex) {
         }
    }

    public void paintComponent ( Graphics g )
    {
            super.paintComponent ( g );
            Graphics2D g2 = (Graphics2D)g;
            g2.drawImage ( myImage , 50, 50, null );
    }
   
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12257190
A small point - class names should start with upper case letters, variables with lower.

>>
catch (Exception ex) {
         }
>>

Tut, tut .. ;-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12257220
>> Tut, tut .. ;-)
;°D

1) Bad practice indeed: better specify which kind of Exception you catch e.g. IOException.
2) Not needed in this case.

So, please read as:

     public testPanel() {
         super();
         myImage = Toolkit.getDefaultToolkit().getImage ( "D:\\Development\\QuickConvey\\QuickConvey\\images\\arrow.gif" );
    }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12257347
>>Bad practice indeed: better specify which kind of Exception

I was thinking more of the empty handler block
0
 

Author Comment

by:peparsons
ID: 12263360
Thanks for all your suggestions. While they were good ideas, unfortunately when I tried them, I still couldn't make it work.

I have however, managed to get something to work. The essential changes are:
1. Using a Graphics instead of a Graphics2D.
2. Extending a JDialog rather than a JPanel.
3. Using paint() instead of paintComponent().
4. Setting the 4th argument (the ImageObserver) of drawImage() to "this" instead of "null".

If what I wanted was an image in a dialog, I'd be very happy. But unfortunately, what I want is to put an image into a JPanel with various other graphics and the JPanel into a JDialog. Any ideas please?

The interesting part of the code that works is below.
(Note that when it's executed, the "drawImage() failed" message is printed 9 times - but who cares? It works.)

public class Picture extends JDialog
{
    private Image image;

    public Picture ( String imagePath )
    {
        Toolkit kit = Toolkit.getDefaultToolkit();
        image = kit.createImage ( imagePath );
        setTitle ( "Test Dialog" );
        setBounds ( 0, 0, 420, 420 );
        setVisible ( true );
    }
      
    public void paint ( Graphics g )
    {
        super.paint ( g );
        boolean succeeded = g.drawImage ( image, 50, 50, this );
        if ( !succeeded )
        {
            System.out.println ( "drawImage() failed" );
        }
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12263437
Your paint method will be called more than once and the image is still loading, it'll return false. This is normal.

Please post the code you were trying before
0
 

Author Comment

by:peparsons
ID: 12266754
OK - I've fixed it. Thanks for your help everybody.
I think the solution was to make sure I specified an ImageObserver. In the original code I just used null.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 37

Expert Comment

by:zzynx
ID: 12267524
Don't leave questions open like that.

Or
1) You appreciate our help that much that you accept one or more comments as (assisted) answers
or
2) you ask to delete this question by posting a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Delete
Body: Please delete this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21160744.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12267535
>>2)

but not in this case
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12267567
Hi CEHJ, you think (s)he should accept?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12267580
Yep
0
 

Author Comment

by:peparsons
ID: 12269419
I'm quite new to this. Sorry if I've not followed some accepted protocol.

However...

Which comment would you like me to accept? None of them fixed the problem.

It was a valid comment that loading the image in the constructor was better, but it didn't fix the problem. None of the other comments fixed the problem either.

As I said in my previous post, I think the answer to the problem is to make sure that the ImageObserver is set correctly. So far as I'm concerned, this is the "Accepted" answer. But unfortunately, I can't see a way to accept my own comments.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12270599
>>Which comment would you like me to accept?

Remember that we here are essentially giving you free assistance. Even if you've paid, *we* don't earn anything.

Your first code to handle the creation of the image was wrong if not fatally so. zzynx pointed this out and gave you a solution for that error.

Other contributions, mine included, (by your own admission), were helpful and should be acknowledged.

>>I think the answer to the problem is to make sure that the ImageObserver is set correctly

It isn't actually. The ImageObserver argument, unless you're making active use of it, can quite simply be null.
0
 

Author Comment

by:peparsons
ID: 12270780
OK - this is the situation:
1. Several people have been kind enough to post responses to my question. I am grateful. Most of their comments were useful.
2. The code is now working as I wanted it to (although it is still under development), but I don't know specifically what change I made to make it work. It's part of a larger piece of code which I could post but I think it would be quite boring for most people to read.

As I mentioned before, I'm fairly new to this site and totally new to the Java section of it. So, please tell me what you think I should do.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12271939
>>but I don't know specifically what change I made to make it work.

That's a pity but can be the case somtimes

>>
It's part of a larger piece of code which I could post but I think it would be quite boring for most people to read.
>>

And probably not useful now if 'before' can't be seen.

>>So, please tell me what you think I should do.

Lower the points somewhat (if you want) and split them. I'll send a page editor to help
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12274825
8-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12274829
Thanks for accepting
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This video teaches viewers about errors in exception handling.

743 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

14 Experts available now in Live!

Get 1:1 Help Now