Results 1 to 10 of 10
  1. #1
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,009
    Blog Entries
    7
    Rep Power
    20

    Default Curiosity killed my moving .gif image

    Greetings,

    first of all: I'm doing this all for fun and it's Sunday so you'll understand that my question is extremly urgent with a tight deadline ;-)

    Java can draw moving .gif images (see the code below), but, if I make a copy of the Image retrieved from an ImageIcon and draw the copy of the Image, nothing moves anymore; see my SSCCE:

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class T {
    
    	static class TPanel extends JPanel {
    		
    		ImageIcon imageIcon;
    		Image image;
    		
    		public TPanel(String imageName) throws IOException {
    			
    			imageIcon= new ImageIcon(imageName);
    			image= new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    			
    			Graphics g= image.getGraphics();
    			g.drawImage(imageIcon.getImage(), 0, 0, this);
    		}
    		
    		public void paintComponent(Graphics g) {
    			
    			super.paintComponent(g);
    			// g.drawImage(image, 0, 0, this);
    			g.drawImage(imageIcon.getImage(), 0, 0, this);
    		}
    	}
    	
    	static public void main(String[] args) throws IOException {
    		JFrame frame= new JFrame("image");
    		
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(new TPanel("/tmp/images/cubes.gif"));		
    		frame.pack();
    		frame.setSize(160, 160);
    		frame.setVisible(true);
    	}
    }
    As you can see, in the constructor of the TPanel class I create a copy of the Image retrieved from the ImageIcon that was just loaded. If you comment the last line in the paintComponent( ... ) method and uncomment the previous line the copy of the Image will be drawn but it doesn't move! I don't understand the 'mechanics' behind this ... can anyone shed some light on this extremely urgent and important problem? ;-)

    kind regards,

    Jos

    ps. I'll upload a copy of the image (let's hope it moves)
    Attached Thumbnails Attached Thumbnails Curiosity killed my moving .gif image-cubes.gif  
    cenosillicaphobia: the fear for an empty beer glass

  2. #2
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default Re: Curiosity killed my moving .gif image

    If you place that ImageIcon onto a JButton, it should animate. Additionaly, you can get the ImageObserver from imageIcon.

    Java Code:
    imageIcon.getImageObserver();
    replace:

    Java Code:
    public void paintComponent(Graphics g) {
                 
                super.paintComponent(g);
                // g.drawImage(image, 0, 0, this);
                g.drawImage(imageIcon.getImage(), 0, 0, this);
            }
    with:

    Java Code:
    public void paintComponent(Graphics g) {
                 
                super.paintComponent(g);
                // g.drawImage(image, 0, 0, this);
                g.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
            }
    Last edited by crikey; 10-02-2011 at 05:30 PM.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,009
    Blog Entries
    7
    Rep Power
    20

    Default Re: Curiosity killed my moving .gif image

    Quote Originally Posted by crikey View Post
    If you place that ImageIcon onto a JButton, it should animate. Additionaly, you can get the ImageObserver from imageIcon.

    Java Code:
    imageIcon.getImageObserver();
    replace:

    Java Code:
    public void paintComponent(Graphics g) {
                 
                super.paintComponent(g);
                // g.drawImage(image, 0, 0, this);
                g.drawImage(imageIcon.getImage(), 0, 0, this);
            }
    with:

    Java Code:
    public void paintComponent(Graphics g) {
                 
                super.paintComponent(g);
                // g.drawImage(image, 0, 0, this);
                g.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
            }
    That is not needed, a JPanel is a fine ImageObserver too (see my code); I know that I can make an ImageIcon move, my question was that, if I copy its Image and draw the Image, the drawn copy doesn't move. There is also no need to use a JButton (or a JLabel) to make an ImageIcon move; drawing its image is fine (see my code again).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,009
    Blog Entries
    7
    Rep Power
    20

    Default Re: Curiosity killed my moving .gif image

    I think a *bump* is appropriate now ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default Re: Curiosity killed my moving .gif image

    Are you talking about an animated gif?

    If so then your copied image is only a copy of one frame of the gif.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,009
    Blog Entries
    7
    Rep Power
    20

    Default Re: Curiosity killed my moving .gif image

    Quote Originally Posted by camickr View Post
    Are you talking about an animated gif?

    If so then your copied image is only a copy of one frame of the gif.
    Yep, an animated gif, that's the word; not 'moving' gif; sorry about that. Am I right guessing that animated gifs can only be displayed (animated) by ImageIcons (without writing any code myself)? And what is 'carried over' if I display an image retrieved from an ImageIcon so that it still is animated? (see my original SSCCE).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default Re: Curiosity killed my moving .gif image

    Am I right guessing that animated gifs can only be displayed (animated) by ImageIcons (without writing any code myself)?
    Thats the only way I know how to do it.

    And what is 'carried over' if I display an image retrieved from an ImageIcon so that it still is animated?
    Not sure, I've never tried it.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,009
    Blog Entries
    7
    Rep Power
    20

    Default Re: Curiosity killed my moving .gif image

    Quote Originally Posted by camickr View Post
    Thats the only way I know how to do it.



    Not sure, I've never tried it.
    [ the forum software removed my question, see reply #7] Drawing an Image retrieved from ImageIcon.getImage() keeps it animated; see my original post. If I make a copy of the image it is not animated anymore (also see my original code). Your answer to this is that only the first frame is copied which makes sense. Basically I'm searching for a way to preserve the animation property for a copy of an (Buffered?)Image.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default Re: Curiosity killed my moving .gif image

    Basically I'm searching for a way to preserve the animation property for a copy of an (Buffered?)Image.
    Then I guess you would have to copy the bytes of the image, not attempt to draw the image onto a BufferedImage.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,009
    Blog Entries
    7
    Rep Power
    20

    Default Re: Curiosity killed my moving .gif image

    Quote Originally Posted by camickr View Post
    Then I guess you would have to copy the bytes of the image, not attempt to draw the image onto a BufferedImage.
    Hm, that's an ImageProducer and ImageConsumer thingy, isn't it? I'll read that (vague) API documentation section again (and hopefully I don't get lost again ;-) and see what I can do ...
    Thanks for the tip and

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Make a clickable moving image?
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 04-08-2011, 07:54 PM
  2. Moving an image on a JFrame
    By jinkazama in forum AWT / Swing
    Replies: 3
    Last Post: 06-04-2010, 12:05 AM
  3. [SOLVED] Graceful Exit after being killed
    By nwboy74 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-18-2008, 12:46 AM
  4. [SOLVED] curiosity about String type variable
    By monir6464 in forum Advanced Java
    Replies: 1
    Last Post: 04-08-2008, 11:13 AM
  5. moving image - PROBLEM
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-17-2008, 06:52 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
  •