Results 1 to 9 of 9
  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Can ImageObserver() be used in the same way I use LineListener

    Rats! I couldn't fit the whole question in the Title.

    I use LineListener on an AudioInputStream to let me know when a .wav file is finished playing.
    Can I use ImageObserver in the same manner to tell me when the last frame of an animated gif has been drawn?

    I wish to use the combination of the two (actually several animated gifs) to setEnabled() some JButtons. Right now I only need to know when the animated gifs are finished.

    It looks like I can do something like this:
    Java Code:
    leftIcon.getImageObserver();	
    ImageLeft.setIcon(leftIcon);
    
    Boolean imageUpdate(Image leftIcon, int flag, int x, int y, int width, int height) {
         if ((flag & ALLBITS) != 0) {
                 //Tell me about it so I can do something
               someVariable = 1
         }
    However, the documentation doesn't show an example, so I'm guessing as to what would work. Some help here would be appreciated.
    Last edited by NotTooOld; 04-12-2013 at 06:33 AM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Can ImageObserver() be used in the same way I use LineListener

    MediaTracker might be closer to what you're looking for: MediaTracker (Java Platform SE 7 )
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Can ImageObserver() be used in the same way I use LineListener

    Quote Originally Posted by KevinWorkman View Post
    MediaTracker might be closer to what you're looking for: MediaTracker (Java Platform SE 7 )
    This from MediaTracker makes me think it may not work for what I need. I also think it is far more versatile than I need.

    "Tracking an animated image might not always be useful due to the multi-part nature of animated image loading and painting, but it is supported. MediaTracker treats an animated image as completely loaded when the first frame is completely loaded. At that point, the MediaTracker signals any waiters that the image is completely loaded. If no ImageObservers are observing the image when the first frame has finished loading, the image might flush itself to conserve resources (see Image.flush()). "

    I'm using ImageIcons already in my code, and ImageIcon has a getter and setter for ImageObserver. This is why I'm thinking ImageObserver() would be best. It doesn't do a lot of other things, just "observes" the image. I only need to know when the animated-gif is completely finished. Can anyone provide me with some sample code how I might asign an image an ImageObserver, AND then check for completion? And where I need to put the checking? I've tried numerous tmes and get various different problems depending on where I try to set the ImageObserver and where I try to check the status.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Can ImageObserver() be used in the same way I use LineListener

    Quote Originally Posted by NotTooOld View Post
    I've tried numerous tmes and get various different problems depending on where I try to set the ImageObserver and where I try to check the status.
    I'd start by posting an SSCCE showing what you've tried. It'll be easier for people to work from that than to come up with example code from scratch.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Can ImageObserver() be used in the same way I use LineListener

    Quote Originally Posted by KevinWorkman View Post
    I'd start by posting an SSCCE showing what you've tried. It'll be easier for people to work from that than to come up with example code from scratch.
    I've never gotten anything close to working. I can't get around the compiler errors. I simply want to know when the animate gif has drawn it's last frame so I can reset some buttons. The ImageIcons are in this PlayAll method:
    Java Code:
    	public void PlayAll(String letter) {
    		String root = "/files/";
    		String folder = root + letter + "/";
    		String left = folder + letter + "L.gif";
    		String top = folder + letter + "T.gif";
    		String right = folder + letter + "R.gif";
    		String sound = folder + letter + ".wav";
    		URL leftURL = AlphaPlay.class.getResource(left);
    		URL topURL = AlphaPlay.class.getResource(top);
    		URL rightURL = AlphaPlay.class.getResource(right);
    		ImageIcon leftIcon = new ImageIcon(leftURL);
    		ImageIcon topIcon = new ImageIcon(topURL);
    		ImageIcon rightIcon = new ImageIcon(rightURL);
    		leftIcon.getImage().flush();
    		topIcon.getImage().flush();
    		rightIcon.getImage().flush();
    
    		try {
    			ImageLeft.setIcon(leftIcon);
    			ImageTop.setIcon(topIcon);
    			ImageRight.setIcon(rightIcon);
    		}
    		catch (Exception e){
    			e.printStackTrace();
    		}
    		try {
    			BufferedInputStream bufferedSound = new BufferedInputStream
    					(AlphaPlay.class.getResourceAsStream(sound));
                AudioInputStream audio = AudioSystem.getAudioInputStream(bufferedSound);
                Clip clip = AudioSystem.getClip();
                clip.addLineListener(this);
                clip.open(audio);
                clip.start();   
            }        
            catch(UnsupportedAudioFileException uae) {
                System.out.println(uae);
            }
            catch(IOException ioe) {
                System.out.println(ioe);
            }
            catch(LineUnavailableException lua) {
                System.out.println(lua);
            }						
    	}
    
    
    	public void update(LineEvent evt){
        	if (evt.getType()== LineEvent.Type.START){
        		DisableKeys();
           	}
        	if (evt.getType()== LineEvent.Type.STOP){
        		EnableKeys();
        	}
        }
    I want to incorporate ImageObserver for the three images into this last method (which works fine for the sound file) hmmm, I don't see ImageIcon in the list of classes that impliment ImageObserver. That may be the problem. I maybe need to set the Image observer on the JLabel I put the image into??? Then, if I can get that done, What code would I write inside this last method (public void update) ???
    Last edited by NotTooOld; 04-16-2013 at 09:36 AM.

  6. #6
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Can ImageObserver() be used in the same way I use LineListener

    Every example or tutorial I can find seem to use drawImage, which is where an ImageObserver is usually automatically assigned.
    "To use the ImageObserver interface, you implement the ImageObserver imageUpdate() method and make sure the implementing object is registered as the image observer. (Usually, this registration happens when you specify an ImageObserver to the drawImage method.)"
    If I use setIcon() is drawImage() being called in the background somewhere? Or can I set the ImageObserver on the JLabel?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Can ImageObserver() be used in the same way I use LineListener

    Sorry I don't have an answer, but I did a bit of googling... ImageIcon will display an animated gif, but the image observer reports the image as loaded when the *first* frame is (if I understand what I read correctly). And that's no help to you.

    One approach I saw (at JavaRanch) was to do the animation yourself. That way you know when the last image has been displayed.

  8. #8
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Can ImageObserver() be used in the same way I use LineListener

    Quote Originally Posted by pbrockway2 View Post
    Sorry I don't have an answer, but I did a bit of googling... ImageIcon will display an animated gif, but the image observer reports the image as loaded when the *first* frame is (if I understand what I read correctly). And that's no help to you.

    One approach I saw (at JavaRanch) was to do the animation yourself. That way you know when the last image has been displayed.
    That is an option, however, 3 animated gifs, times 27, (app open + 26 letters) times average 35 frames per animation is a lot of files.
    From what I've read, MediaTracker works as you describe, (calls finished after the first frame is loaded) but I've not seen anywhere that said this is how ImageObserver handles an animated gif.

  9. #9
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Can ImageObserver() be used in the same way I use LineListener

    Because I need the practice, I believe I'll just write this whole thing over again. This time I'll write a method to build an array of images from the frames of the gif, use standard naming conventions, and generally include the things I've learned so far in the new one. I'll concede that it may be impossible to get notification when the last frame of an animated gif has been displayed.

Similar Threads

  1. Accessing ImageObserver?
    By Peetahzee in forum New To Java
    Replies: 2
    Last Post: 12-12-2009, 05:29 AM
  2. Help: how to set imageObserver
    By xing217sg in forum AWT / Swing
    Replies: 3
    Last Post: 10-21-2009, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •