将gif设置为小型Java游戏

时间:2015-05-28 23:26:47

标签: java swing background-image gif

我目前正在开展一个小型的ZigZag游戏项目。我已完成编码所有必要的代码以使代码工作,但我想将游戏本身的背景更改为动画.gif。我尝试了几种方法但是没有一种方法可行,因为gif最终出现在我的图形上或者只是停止了骑行。我确实找到了一些有用的链接,但我不太明白它背后的概念。

以下是链接:

Setting Loaded GIF as a Background

我也试图密切关注这个例子(由kschneid Show an animated BG in Swing提供) 但gif再次涵盖了我的所有图形。

我如何设置动画gif,以便它不会丢失其循环功能,但这不会覆盖我的图形?

这是我的代码(注意:我只包含了我认为与问题相关的方法)

   /**
   * 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);



  }

非常感谢您提前给出的时间。

0 个答案:

没有答案