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

J

JW

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!");
}
}
}
}
 
T

The Joker

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();
}
 
H

hiwa

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top