[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Painting GIF Images on to a GUI

Posted on 2004-10-08
19
Medium Priority
?
775 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 4
19 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 750 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 750 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

656 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