1

I'm currently working on a small ZigZag game project. I am done with coding everything essential to make the code work however I would like to change the background of the game itself to an animated .gif. I tried several ways but none of them worked since the gif ended up on top of my graphics or simply stopped cycling. I did find a few helpful links but I did not quite understand the concept behind it.

Here are the links:

Setting Loaded GIF as a Background

I also attempted to closely follow the example (courtesy of kschneid Show an animated BG in Swing) But the gif, again, covers all of my graphics.

How may I approach setting an animated gif so that it does not lose its cycling features but that does not cover my graphics?

Here's my code (Note: I've only included the methods that I think are relevant to the question)

   /**
   * main() is needed to initialize the window.<br>
   */
   public static void main(String[] args) throws MalformedURLException{
   final URL url=new URL("http://i.imgur.com/aUhcShY.gif");
       EventQueue.invokeLater(new Runnable(){
           @Override
           public void run(){

            JFrame window = new JFrame("JZigZag");
            window.setBounds(100, 100, BORDER.width + 7, BORDER.height + 30);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //window.setResizable(false);
            window.setLocationByPlatform(true);

            Image image=window.getToolkit().createImage(url);
            ImagePanel imagePanel=new ImagePanel(image);
            imagePanel.setLayout(new GridLayout(5,10,10,10));
            imagePanel.setBorder(new EmptyBorder(20,20,20,20));

            JZigZag game = new JZigZag();
            window.getContentPane().add(game);
            window.setContentPane(imagePanel);
            window.pack();
            window.setVisible(true);
            game.init();
            window.addKeyListener(game);}  
  });
}

  /**
   * init method needed to initialize certain fields and load resources
   */
  public void init() {
    offScreenBuffer = createImage(getWidth(), getHeight());// should be 800x400
    offScreenGraphics = offScreenBuffer.getGraphics();
    timer = new Timer(30, this);
    // timer fires every 30 milliseconds.. invokes method actionPerformed()

    try {
      ballImg = ImageIO.read(getClass().getResource("ball.png"));
      trackImg=ImageIO.read(getClass().getResource("track.png"));//Task 2--Track Image



    } catch (IOException ex) {
      ex.printStackTrace();
    }

    initRound();
  }


  /**
   * Called automatically after a repaint request<br>
   */
  public void paint(Graphics g) {
    draw((Graphics2D) offScreenGraphics);
    g.drawImage(offScreenBuffer, 0, 0, this);
  }

  /**
   * renders all objects to Graphics g
   */
  public void draw(Graphics2D g) {

    g.setColor(new Color(200, 255, 210));
    g.fill(BORDER);// paint background

    g.setColor(Color.BLACK);

    if (!timer.isRunning()) {
      g.drawString(title, 10, 600);// draw title when not playing
    }

    g.drawString("Score:  " + frameCount / 20, 160, 650);// approximate middle
    g.drawString("High Score:  " + hiScore, 160, 680);// approximate middle


    for(int i=0;i<track.size();i++)//Task 2--DRAW TRACK 
    {
        g.drawImage(trackImg,track.get(i)*40,i*20+frameCount%20,null);
    }
    g.drawImage(ballImg,ballX-10,ballY-10,null);//Task 1--DRAW BALL 


     g.drawLine(leftBound, ballY + 10, leftBound + 80, ballY + 10);



  }

Thank you very much for your given time in advance.

Community
  • 1
  • 1
mysql
  • 19
  • 3
  • You have an interesting problem, painting the gif will only display a single frame (from memory). You could use a `JLabel` to render the gif and add your content to that, but there are issues with doing it that way to – MadProgrammer May 28 '15 at 23:58
  • 1
    Or you could just use a `ImageIcon` and a Swing `Timer` as shown [here](http://stackoverflow.com/questions/26330550/java-gif-animation-not-repainting-correctly/26331052#26331052) – MadProgrammer May 29 '15 at 00:00
  • Thank you so much @MadProgrammer! I'll try to incorporate that into my code. – mysql May 29 '15 at 00:25
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson May 29 '15 at 01:16
  • Thank you, I only included as much information only to be thorough and I thought that including the entirety of my code would be extremely lengthy. I am aware of how to get images, however I am not sure how to implement a gif as a background for my game without it covering my graphics or losing its animation feature. – mysql May 29 '15 at 01:28

0 Answers0