Gif not playing in JFrame - java

Hello, I have a gif in a JFrame. It all works fine except the gif is frozen on the first time as though it is an jpeg or png.
Also, the stackoverflow is telling me I need to add more details even though I have added all the details required in order to state my problem, so feel free to ignore this.
Here is the code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class LoadImageApp extends Component {
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
public LoadImageApp() {
try {
img = ImageIO.read(new File("spooky.gif"));
} catch (IOException e) {
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new LoadImageApp());
f.pack();
f.setVisible(true);
}
}

I'm pretty sure ImageIO has a few problems with animated gifs.
Try using a JLabel and and Icon, like so:
Icon icon = new ImageIcon(filename);
JLabel gif = new JLabel(icon);
JFrame f = new JFrame("Load Image Sample");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.getContentPane().add(gif);
f.pack();
f.setVisible(true);

As mentioned, ImageIO will not correctly load all the frames of an animated GIF. But one of the Toolkit methods will. E.G.
import java.awt.*;
import javax.swing.*;
import java.net.URL;
// Ach AWT!
//public class LoadImageApp extends Component {
public class LoadImageApp extends JPanel {
Image img;
// should be paintComponent for a JComponent
//public void paint(Graphics g) {
public void paintComponent(Graphics g) {
super.paintComponent(g); // call super method first
g.drawImage(img, 0, 0, this);
}
public LoadImageApp() {
setBackground(Color.BLUE.darker().darker());
try {
URL url = new URL("http://i.stack.imgur.com/OtTIY.gif");
// as mentioned, ImageIO will not load animated GIFs correctly.
//img = ImageIO.read(url));
// but the toolkit method will, OTOH..
img = Toolkit.getDefaultToolkit().createImage(url);
// ..we need a MediaTracker
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 0);
mt.waitForAll();
} catch (Exception e) {
e.printStackTrace();
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100, 100);
} else {
return new Dimension(img.getWidth(this), img.getHeight(this));
}
}
public static void main(String[] args) {
// should be donw on the EDT - BNI
JFrame f = new JFrame("Load Image Sample");
f.add(new LoadImageApp());
f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
For further discussion, see Show an animated BG in Swing.

Related

How would I add an action Listener to the button to refresh the image? [duplicate]

I have been trying to figure this out why not the next picture showing on the same panel after click the button. I want to separate those classes not into one class and used repaint() to re-invoke paintComponent() with the new pic.
Please help me. I am almost dying :(
when I run this, the first picture appears well. when the button is clicked to change the first picture to the second one, the Panel just keep on showing the first picture.
Thank you.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
class drawImage extends JPanel {
BufferedImage[] b = new BufferedImage[2];
public drawImage() {
try {
b[0] = ImageIO.read(new File("img/gameOn.png"));
b[1] = ImageIO.read(new File("img/gameOff.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(b[0], 0, 0, null);
}
public void setNextImage(BufferedImage image) {
b[0] = image;
repaint();
}
public BufferedImage getB0() {
return b[0];
}
public BufferedImage getB1() {
return b[1];
}
}// end drawImage
class clickedListener implements ActionListener {
BufferedImage pre = new drawImage().getB0();
BufferedImage next = new drawImage().getB1();
#Override
public void actionPerformed(ActionEvent e) {
new drawImage().setNextImage(next);
}
}
public class buttonFrame {
public static void main(String[] args) throws IOException {
JFrame jf = new JFrame("Button & Frame");
JButton btn = new JButton("Click");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.setLayout(new GridLayout(2, 0));
jf.add(new drawImage());
jf.add(btn);
jf.setSize(200, 250);
btn.addActionListener(new clickedListener());
}
}
Why not change your approach and make use of a JLabel instead? Set your image as an icon on the label and add it to your JPanel:
BufferedImage image = ImageIO.read(new File("image-path"));
JLabel label = new JLabel(new ImageIcon(image));
panel.add(label);
You can then make subsequent calls to JLabel#setIcon(...) each time you want the image to change.
You can also use ImageIcon like this
image = new ImageIcon(imageList[1]);
and when each time button is clicked you can change image like this
label.setIcon(image);

Showing Image with extendend JFrame

I've made some code for my tile, where I draw 2 objects, but I can't load a background in the tile as it extends JFrame.
What am I doing wrong?
I used buffered image to read the picture, made it suitable for any screen size, set it as a JLabel, but still isn't working.
Square1 and obstacle are graphics of rectangles imported, one acting as the player moving and the other as an obstacle respectively.
public class BasicTwoPlayer extends JFrame implements Runnable {
static Square1 p1 = new Square1();
static Square2 p2 = new Square2();
static obstacle o1 = new obstacle();
static Thread p1t;
static Thread p2t;
static Thread o1t;
KeyADAPT a = new KeyADAPT();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final int widthScreen = screenSize.width;
final int heightScreen = screenSize.height;
public BasicTwoPlayer() {
try {
BufferedImage backgroundImage = ImageIO.read(new File("P://My Pictures//background1.jpg"));
JLabel background = new JLabel(new ImageIcon(backgroundImage));
Image scaleBackground = backgroundImage.getScaledInstance(widthScreen, heightScreen, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(scaleBackground);
setContentPane(new JLabel(imageIcon));
addKeyListener(a);
setSize(new Dimension(widthScreen, heightScreen));
setUndecorated(true);
setBackground(Color.WHITE);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p1t = new Thread(p1);
p2t = new Thread(p2);
o1t = new Thread(o1);
p1t.start();
p2t.start();
} catch (IOException ex) {
Logger.getLogger(BasicTwoPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
background.paint(g);
}
public void draw(Graphics g) {
p1.draw(g);
o1.draw(g);
repaint();
}
public static void main(String[] args) {
BasicTwoPlayer sf = new BasicTwoPlayer();
Thread mt = new Thread(sf);
mt.start();
t1.start();
}
I just want an answer like everyone else :/
You'd be surprised how much I'd prefer to learn how to answer my own questions ;)
You will need to understand:
Performing Custom Painting
Painting in AWT and Swing
It wouldn't hurt to understand:
Reading/Loading an Image
Laying Out Components Within a Container
"How to add a background image" ...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setContentPane(new BackgroundPane());
frame.add(new OverlayPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class OverlayPane extends JPanel {
public OverlayPane() {
setOpaque(false);
setLayout(new GridBagLayout());
JLabel label = new JLabel("You'd be suprised who far a little knowledge will take you");
label.setForeground(Color.WHITE);
add(label);
}
}
public class BackgroundPane extends JPanel {
private BufferedImage backgroundImage;
public BackgroundPane() {
setLayout(new BorderLayout());
try {
backgroundImage = ImageIO.read(BackgroundPane.class.getResource("background.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return backgroundImage == null ? new Dimension(200, 200) : new Dimension(backgroundImage.getWidth(), backgroundImage.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImage == null) { return; }
int x = (getWidth() - backgroundImage.getWidth()) / 2;
int y = (getHeight() - backgroundImage.getHeight()) / 2;
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(backgroundImage, x, y, this);
g2d.dispose();
}
}
}
You are going to want to learn about:
Concurrency in Swing
How to Use Swing Timers
How to Use Key Bindings
based on you the direction of this and all your other questions to date. They will help you avoid common pitfalls.
I don't doubt you'll have issues coming to grips with these concepts and I encourage you to have a play and if and when, post a question out them, specifically.

how do i make visible other component visible while setting background image of frame?

i am setting frame's background image when i run program my other components are invisible only image is visible in frame
class ImagePanel extends JComponent {
private Image image;
public ImagePanel(Image image) {
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
In the main class I call the above class as shown below:
BufferedImage myImage = ImageIO.read(new File("cal.jpg"));
frame.setContentPane(new ImagePanel(myImage));
You have this code:
BufferedImage myImage = ImageIO.read(new File("cal.jpg"));
frame.setContentPane(new ImagePanel(myImage));
but you appear to be creating the ImagePanel instance inline, and don't appear to be adding any components to this ImagePanel instance, so I'm not surprised that you're not seeing any components. You also don't seem to be adding any components to it in the ImagePanel constructor.
Consider adding components to the ImagePanel class within its constructor, or in the class that uses it, create an ImagePanel instance, assign it to a variable, add components to it, and then place it into the JFrame's contentPane.
Side recommendations:
Consider getting your image as a Jar resource and not as a File, since likely you will Jar the classes at some point, and if you continue using File, your image might not be reachable.
Make sure to give your ImagePanel a decent layout manager. I believe that JComponents use null layouts by default, something that you don't want to use.
For example, this worked for me:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TestImagePanel {
private static void createAndShowGui() {
String resource = "/imgFolder/PlanetEarth.jpg";
Image image = null;
try {
image = ImageIO.read(TestImagePanel.class.getResource(resource));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
ImagePanel mainPanel = new ImagePanel(image);
mainPanel.setLayout(new FlowLayout());
mainPanel.add(new JButton("Fubars Rule!"));
JFrame frame = new JFrame("TestImagePanel");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class ImagePanel extends JComponent {
private Image image;
public ImagePanel(Image image) {
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
#Override
public Dimension getPreferredSize() {
Dimension superSize = super.getPreferredSize();
int w = image == null ? superSize.width : Math.max(superSize.width, image.getWidth(null));
int h = image == null ? superSize.height : Math.max(superSize.height, image.getHeight(null));
Dimension d = new Dimension(w, h);
return d;
}
}
and showed this GUI:

How to change jdesktoppane default background image?

How to change jdesktoppane background image in MDI (Multiple Documents interface) using java netbeans? Means I added the jdesktoppane to java MDI so now I want to change default background image of that jdesktoppane which I'm using in java MDI. Any easy way?
Check attached snapshot link may be you will better understand my question what I want.
http://i50.tinypic.com/iml1e9.jpg
+1 to MadProgrammers comment.
Simply override JDesktopPane paintComponent(..) and call drawImage(Image img,int x,int y,ImageObserver io) to draw an image.
Dont forget to honor the paint chain and call super.paintComponent(g) as first call in overridden paintComponent(..) method
Here is an example:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JInternalFrameDemo {
private JDesktopPane jdpDesktop;
private static int openFrameCount = 0;
private BufferedImage img;
public JInternalFrameDemo() {
JFrame frame = new JFrame("JInternalFrame Usage Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
img = ImageIO.read(new URL("http://images1.wikia.nocookie.net/__cb20120817224359/villains/images/6/6a/Nine-Tailed_Fox_(Naruto).jpg"));
} catch (Exception ex) {
ex.printStackTrace();
}
// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
#Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.drawImage(img, 0, 0, null);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(), img.getHeight());
}
};
createFrame(); // Create first window
frame.setContentPane(jdpDesktop);
frame.setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
frame.pack();
frame.setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
return menuBar;
}
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new JInternalFrameDemo();
}
});
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}
I resolve it in a separate function to create a desktop object.
Code as below
private JDesktopPane intializeDesktop(JDesktopPane mydesktop,String imagePath,int scalx,int scaly) {
// A specialized layered pane to be used with JInternalFrames
mydesktop = new JDesktopPane() {
ImageIcon icon = new ImageIcon(imagePath);
Image image = icon.getImage();
Image newimage = image.getScaledInstance(scalx, scaly, Image.SCALE_SMOOTH);
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(newimage, 0, 0, this);
}
};
return mydesktop;
}

Image Drawing Works for JFrame, But Not JPanel

I'm working through some simple applications to get familiar with Swing and running into problems.
I'm attempting to have a frame containing an image (in a panel) along with buttons to zoom in/out from the image.
I have been able to make a frame with the added image work fine (albeit with some frame sizing issues, but that is another story), however, when I call the same component class to add it to a panel, nothing appears. I'm hoping one of you can help shed light on the situation.
CODE:
Image Frame - Is working as shown
class ImageFrame extends JFrame{
public ImageFrame(){
setTitle("Java Image Machine");
init();
pack();
}
public final void init(){
//ZoomPanel zoomPanel = new ZoomPanel();
//ImagePanel imagePanel = new ImagePanel();
ImageComponent component = new ImageComponent();
//this.add(zoomPanel, BorderLayout.CENTER);
this.add(component);
//this.add(imagePanel, BorderLayout.SOUTH);
}
}
However, using the ImagePanel or adding the ZoomPanel simultaneously with the direct ImageComponent call, does not:
class ImagePanel extends JPanel{
public ImagePanel(){
//setBorder(BorderFactory.createLineBorder(Color.black));
ImageComponent component = new ImageComponent();
add(component);
}
}
Component class:
class ImageComponent extends JComponent{
public ImageComponent(){
try{
image = ImageIO.read(new File("test1.bmp"));
}
catch ( IOException e ){
e.printStackTrace();
}
System.out.println("W: " + image.getWidth(this) + " H: " + image.getHeight(this));
}
public void paintComponent( Graphics g ){
super.paintComponent(g);
if (image == null)
return;
width = image.getWidth(this);
height = image.getHeight(this);
//System.out.println("Image should be painted");
g.drawImage(image, 0, 0, null);
}
private Image image;
public int width;
public int height;
}
It works fine for me ( I just tested the ImageComponent class):
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Test {
/**
* Default constructor Test.class
*/
public Test() {
initComponents();
}
public static void main(String[] args) {
/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Test test = new Test();
}
});
}
/**
* Initialize GUI and components (including ActionListeners etc)
*/
private void initComponents() {
JFrame jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add ImageComponent to JFrame instance
jFrame.add(new ImageComponent());
//pack frame (size JFrame to match preferred sizes of added components and set visible
jFrame.pack();
jFrame.setVisible(true);
}
}
class ImageComponent extends JComponent {
private Image image;
public int width;
public int height;
public ImageComponent() {
try {
image = ImageUtils.scaleImage(300, 300, ImageIO.read(new URL("http://harmful.cat-v.org/software/_java/java-evil-edition.png")));
//image = ImageIO.read(new URL("http://harmful.cat-v.org/software/_java/java-evil-edition.png"));//uses images scale
} catch (Exception e) {
e.printStackTrace();
}
//so we can set the JPanel preferred size to the image width and height
ImageIcon ii = new ImageIcon(image);
width = ii.getIconWidth();
height = ii.getIconHeight();
}
//so our panel is the same size as image
#Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image == null) {
return;
}
width = image.getWidth(this);
height = image.getHeight(this);
g.drawImage(image, 0, 0, null);
}
}
//class used for scaling images
class ImageUtils {
static Image scaleImage(int width, int height, BufferedImage filename) {
BufferedImage bi;
try {
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(filename, 0, 0, width, height, null);
} catch (Exception e) {
return null;
}
return bi;
}
}
problem might be the path to your file, or the fact that JPanel width and height will not be the same as the pictures thus we override getPrefferedSize(...) of JPanel and return correct size according to Image
Have you tried adding repaint() calls after adding the appropriate components in?
i.e.
class ImagePanel extends JPanel{
public ImagePanel(){
//setBorder(BorderFactory.createLineBorder(Color.black));
ImageComponent component = new ImageComponent();
add(component);
repaint();
}
}
Also double check that you are adding the ImagePanel (containing the ImageComponent) to the ImageFrame, and calling .setVisible(True).

Categories

Resources