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