Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 793
  • Last Modified:

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 )
            {
            }
           
    }
   
    }
}
0
peparsons
Asked:
peparsons
  • 9
  • 6
  • 4
2 Solutions
 
zzynxSoftware engineerCommented:
Remark:
Don't load your image in paintComponent() !
Do that once and forever in the constructor
0
 
CEHJCommented:
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
 
zzynxSoftware engineerCommented:
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
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.

 
CEHJCommented:
A small point - class names should start with upper case letters, variables with lower.

>>
catch (Exception ex) {
         }
>>

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

I was thinking more of the empty handler block
0
 
peparsonsAuthor Commented:
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
 
CEHJCommented:
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
 
peparsonsAuthor Commented:
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
 
zzynxSoftware engineerCommented:
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
 
CEHJCommented:
>>2)

but not in this case
0
 
zzynxSoftware engineerCommented:
Hi CEHJ, you think (s)he should accept?
0
 
CEHJCommented:
Yep
0
 
peparsonsAuthor Commented:
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
 
CEHJCommented:
>>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
 
peparsonsAuthor Commented:
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
 
CEHJCommented:
>>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
 
CEHJCommented:
8-)
0
 
zzynxSoftware engineerCommented:
Thanks for accepting
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now