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.