Results 1 to 10 of 10
- 10-02-2011, 03:27 PM #1
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
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:
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? ;-)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); } }
kind regards,
Jos
ps. I'll upload a copy of the image (let's hope it moves)When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-02-2011, 05:23 PM #2
Senior Member
- Join Date
- Jul 2010
- Posts
- 125
- Rep Power
- 0
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.
replace:Java Code:imageIcon.getImageObserver();
with:Java Code:public void paintComponent(Graphics g) { super.paintComponent(g); // g.drawImage(image, 0, 0, this); g.drawImage(imageIcon.getImage(), 0, 0, this); }
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.
- 10-02-2011, 05:40 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Curiosity killed my moving .gif image
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-03-2011, 02:55 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Curiosity killed my moving .gif image
I think a *bump* is appropriate now ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-03-2011, 04:44 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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.
- 10-03-2011, 04:57 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Curiosity killed my moving .gif image
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-03-2011, 05:42 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Re: Curiosity killed my moving .gif image
Thats the only way I know how to do it.Am I right guessing that animated gifs can only be displayed (animated) by ImageIcons (without writing any code myself)?
Not sure, I've never tried it.And what is 'carried over' if I display an image retrieved from an ImageIcon so that it still is animated?
- 10-03-2011, 05:56 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Curiosity killed my moving .gif image
[ 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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-03-2011, 06:10 PM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Re: Curiosity killed my moving .gif image
Then I guess you would have to copy the bytes of the image, not attempt to draw the image onto a BufferedImage.Basically I'm searching for a way to preserve the animation property for a copy of an (Buffered?)Image.
- 10-03-2011, 07:38 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Curiosity killed my moving .gif image
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Make a clickable moving image?
By scheffetz in forum New To JavaReplies: 1Last Post: 04-08-2011, 07:54 PM -
Moving an image on a JFrame
By jinkazama in forum AWT / SwingReplies: 3Last Post: 06-04-2010, 12:05 AM -
[SOLVED] Graceful Exit after being killed
By nwboy74 in forum Threads and SynchronizationReplies: 1Last Post: 11-18-2008, 12:46 AM -
[SOLVED] curiosity about String type variable
By monir6464 in forum Advanced JavaReplies: 1Last Post: 04-08-2008, 11:13 AM -
moving image - PROBLEM
By Triss in forum New To JavaReplies: 3Last Post: 01-17-2008, 06:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks