Link to home
Start Free TrialLog in
Avatar of peparsons
peparsons

asked on

Painting GIF Images on to a GUI

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 )
            {
            }
           
    }
   
    }
}
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 );
    }
   
}
A small point - class names should start with upper case letters, variables with lower.

>>
catch (Exception ex) {
         }
>>

Tut, tut .. ;-)
>> 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" );
    }
>>Bad practice indeed: better specify which kind of Exception

I was thinking more of the empty handler block
Avatar of peparsons
peparsons

ASKER

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" );
        }
    }
}
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
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.
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 https://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Delete
Body: Please delete this question:
https://www.experts-exchange.com/questions/21160744/Painting-GIF-Images-on-to-a-GUI.html
>>2)

but not in this case
Hi CEHJ, you think (s)he should accept?
Yep
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.
>>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.
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.
>>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
8-)
Thanks for accepting