0

First of all I am making a minigame with the Five Nights At Freddy's graphics and jump scares. I already know how to import and draw a picture (png and etc). The only one I don't know how to import, are gifs.

Here Is My Code:

import java.awt.*; 
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Minigame extends JPanel
{   
/**
 * 
 */
private static final long serialVersionUID = 1L;

GameEvents gameEvents = new GameEvents();
Timer gameTimer = new Timer(1, gameEvents);
int i = 0; 
int horizontalposition = 500;
int verticalposition = 500;

BufferedImage Picture;
BufferedImage Picture2;
BufferedImage Picture3;
BufferedImage Picture4;
BufferedImage Picture5;
BufferedImage Picture6;

//Don't forget to declare your variables!


Minigame()
{
    gameTimer.start();
    this.addKeyListener(gameEvents);

    try 
    {
        Picture = ImageIO.read(getClass().getResource("Child.gif"));
        Picture2 = ImageIO.read(getClass().getResource("Freddycake_Gif.gif"));
        Picture3 = ImageIO.read(getClass().getResource("Purple_man.png"));
        Picture4 = ImageIO.read(getClass().getResource("Cake_Child_Idle.png"));
        Picture5 = ImageIO.read(getClass().getResource("Cake_Child.gif"));
        //The format for this is Picture = ImageIO.read(getClass().getResource("NameOfFile.typeoffile"));
    }
    catch (IOException e)
    {
        System.out.println("Pictures failed to load");
    }
}

@Override
protected void paintComponent(Graphics g)
{
    g.setColor(Color.black);
    g.fillRect(0,0,this.getWidth(), this.getHeight());

    ///g.drawImage(Picture, horizontalposition, verticalposition, 100, 150, null);

    g.drawImage(Picture, 200, 10, 70, 100, null);
    g.drawImage(Picture, 200, 100, 70, 100, null);
    g.drawImage(Picture, 200, 200, 70, 100, null);
    g.drawImage(Picture, 200, 300, 70, 100, null);
    g.drawImage(Picture, 200, 400, 70, 100, null);
    g.drawImage(Picture, 200, 500, 70, 100, null);
    g.drawImage(Picture2, horizontalposition, verticalposition, 100, 150, null);
    g.drawImage(Picture3, 1100, 50, 100, 150, null);
    g.drawImage(Picture4, 900, 50, 100, 150, null);
    //g.drawImage(Picture5, 900, 50, 100, 150, null);
}

public class GameEvents implements ActionListener, KeyListener
{
    @Override
    public void actionPerformed(ActionEvent arg0) 
    {
        repaint();
    }

    @Override
    public void keyPressed(KeyEvent key) //stuff inside here happens when a key is pressed
    {
        if(key.getKeyChar()=='d')
        {
            horizontalposition=horizontalposition+50;
        }
        if(key.getKeyChar()=='s')
        {
            verticalposition=verticalposition+50;
        }
        if(key.getKeyChar()=='w')
        {
            verticalposition=verticalposition-50;
        }
        if(key.getKeyChar()=='a')
        {
            horizontalposition=horizontalposition-50;
        }
        if(horizontalposition<0)
        {
            horizontalposition=0;
        }
        System.out.println(key.getKeyChar());
        System.out.println('d');
    }

    @Override
    public void keyReleased(KeyEvent arg0) {

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
    }

}
public static void main(String[] args)
{

    JFrame f = new JFrame("Java Graphics Example Project");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Minigame p = new Minigame();
    f.setSize(1500,700);
    f.add(p);
    f.setVisible(true);
    p.requestFocusInWindow();
    }

}

Any yes, I already know how to do g.drawImage();

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • The IDE (ie. Eclipse) is irrelevant to the Java code or usage of such images. I made the assumption that the issue is with *animated* GIFs (because if not.. why use them?). Make sure to clarify the question as appropriate. – user2864740 Mar 20 '15 at 02:55
  • possible duplicate of [Show animated gif in Java](http://stackoverflow.com/questions/2935232/show-animated-gif-in-java) – Harald K Mar 20 '15 at 10:08

1 Answers1

0

Look up this answer response: Show animated GIF If you don't feel like it, I'll just put the code of the person who replied here. However, they explain it there and really go in detail. THIS IS THE CODE FROM STACKER WHO EXPLAINED IT ON THE LINK I GAVE YOU

 public static void main(String[] args) throws MalformedURLException {

    URL url = new URL("<URL to your Animated GIF>");
    Icon icon = new ImageIcon(url);
    JLabel label = new JLabel(icon);

    JFrame f = new JFrame("Animation");
    f.getContentPane().add(label);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}
Community
  • 1
  • 1
Raheel138
  • 147
  • 10