Java app displaying animated GIF image, on a Linux System?

Discussion in 'Java' started by JW, Dec 23, 2003.

  1. JW

    JW Guest

    Hi,

    I am trying to see if I can display an animated .gif file
    on a Java application (that is, I do not want to have
    to show a series of static images to simulate animateion;
    I just want to display a file that is of the animated .gif
    type).

    I was able run the following code (2 java classes) on a Windows 2000 machine
    using Java 1.4 and it works fine,
    but on Redhat linux 7.3 (KDE desktop), I just get a blank panel, without
    the image, and without error messages.

    Has anyone been able to successfully do what I am trying to do
    on a Redhat system (using Java 1.4)?

    Thank you

    ---
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class AnimatedPanel extends JPanel
    {

    final String animatedPic = "animated.gif";
    final String blankPic = "blank.gif";

    private ImagePanel imagePanel;

    private boolean animationStarted = false;
    public AnimatedPanel()
    {
    imagePanel = new ImagePanel(animatedPic);
    setLayout(new BorderLayout());
    //add(imagePanel,"Center");
    add(imagePanel);
    }

    public static void main(String[] args)
    {



    JFrame f2=new JFrame();
    f2.setSize(500,500);
    f2.setTitle("arc");
    f2.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e)
    {

    }
    });

    AnimatedPanel an=new AnimatedPanel();

    f2.getContentPane().add(an);


    f2.show();
    }
    }
    ---
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    import java.io.File;

    class ImagePanel extends JPanel
    {
    protected Image image;
    protected int x=0;
    protected int y=0;

    private MediaTracker tracker;


    public ImagePanel(String filename)
    {
    Toolkit toolkit=Toolkit.getDefaultToolkit();
    File file=new File(filename);
    if ( file.exists())
    image=toolkit.getImage(filename);

    try {

    tracker = new MediaTracker(this);
    tracker.addImage(image, 0);
    tracker.waitForID(0);
    } catch (Exception e) {}

    repaint();
    }


    public void setImage(String filename)
    {
    tracker.removeImage(image);
    Toolkit toolkit=Toolkit.getDefaultToolkit();
    File file=new File(filename);
    if ( file.exists())
    image=toolkit.getImage(filename);

    try {
    tracker.addImage(image, 0);
    tracker.waitForID(0);
    } catch (Exception e) {}
    }



    public void paintComponent(Graphics g)
    {
    super.paintComponent(g); //paint background

    if(image!=null)
    {
    try
    {
    if (tracker.statusID(0, false) == MediaTracker.COMPLETE)
    g.drawImage(image, x, y, this);
    }
    catch(Exception e)
    {
    System.out.println("ImagePanel Error: Can not render image!");
    }
    }
    }
    }
     
    JW, Dec 23, 2003
    #1
    1. Advertising

  2. JW

    The Joker Guest

    In article <>, you say...
    > Hi,
    >
    > I am trying to see if I can display an animated .gif file
    > on a Java application (that is, I do not want to have
    > to show a series of static images to simulate animateion;
    > I just want to display a file that is of the animated .gif
    > type).
    >
    > I was able run the following code (2 java classes) on a Windows 2000 machine
    > using Java 1.4 and it works fine,
    > but on Redhat linux 7.3 (KDE desktop), I just get a blank panel, without
    > the image, and without error messages.
    >
    > Has anyone been able to successfully do what I am trying to do
    > on a Redhat system (using Java 1.4)?
    >
    > Thank you


    The following code works on RedHat with Java 1.4.

    imageLocation = a string with the path + animated gif name (e.g.
    /usr/tmp/walkingMan.gif)
    size = the dimensions of the image

    import java.awt.*;
    import javax.swing.*;
    import java.awt.image.ImageObserver;

    public class AnimatedPanel extends JPanel implements ImageObserver {
    Image animatedImage;

    public AnimatedPanel (String imageLocation, Dimension size) {
    animatedImage = Toolkit.getDefaultToolkit().getImage
    (imageLocation);
    setMinimumSize(size);
    setPreferredSize(size);
    setMaximumSize(size);
    }

    public AnimatedPanel (Dimension size) {
    setMinimumSize(size);
    setPreferredSize(size);
    setMaximumSize(size);
    }

    public AnimatedPanel () {
    }

    public void setAnimatedImage(String imageLocation) {
    animatedImage = Toolkit.getDefaultToolkit().getImage
    (imageLocation);
    }

    public void setPanelSize(Dimension size) {
    setMinimumSize(size);
    setPreferredSize(size);
    setMaximumSize(size);
    }

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;

    try {
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(animatedImage, 0);
    mt.waitForID(0);
    }
    catch (Exception e) {};

    g2.drawImage(animatedImage, 0, 0, this);
    }

    public boolean imageUpdate(Image img, int infoflags, int x, int y,
    int width, int
    height) {
    if (isShowing() && (infoflags & ALLBITS) != 0)
    repaint();
    if (isShowing() && (infoflags & FRAMEBITS) != 0)
    repaint();
    return isShowing();
    }
     
    The Joker, Dec 27, 2003
    #2
    1. Advertising

  3. JW

    hiwa Guest

    The Joker <> wrote in message news:<>...

    Your solution indeed does work fine. You should be an old-timer Java
    guru! In an another context of discussion, however, this is clearly a
    bug on the part of the JDK for Linux implementation. No one want this
    complexity for displaying simple GIF files. Besides, javax.JPanel is a
    java.awt.Component is an ImageObserver. You don't need to explicitly
    implements the ImageObserver for your AnimatedPanel class.
     
    hiwa, Dec 28, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hb
    Replies:
    6
    Views:
    9,129
  2. christof hoeke

    save animated GIF in Python Image Library

    christof hoeke, Feb 5, 2004, in forum: Python
    Replies:
    1
    Views:
    2,945
    Fredrik Lundh
    Feb 24, 2004
  3. D i s h a n

    Resize Animated GIF Image

    D i s h a n, Feb 13, 2007, in forum: Java
    Replies:
    9
    Views:
    2,190
  4. Dave Kelly
    Replies:
    4
    Views:
    833
    natashab
    Nov 30, 2006
  5. David C

    Animated gif image

    David C, Dec 4, 2009, in forum: ASP .Net
    Replies:
    0
    Views:
    359
    David C
    Dec 4, 2009
Loading...

Share This Page