import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;

public class Panel1 extends JPanel implements ActionListener {
    
    protected static final int DEFAULT_FPS = 30;
    BufferedImage background, foreground;
    int backwidth, backheight;
    int rocketwidth, rocketheight, rocketx, rockety;
    
    public Panel1(int rate) {
	int delay = 1000/rate;
	String fs = File.separator;
	Image backImage = new ImageIcon("images"+fs+"starfield.gif").getImage();
	backwidth = backImage.getWidth(this);
	backheight = backImage.getHeight(this);
	background = new BufferedImage(backwidth, backheight,BufferedImage.TYPE_4BYTE_ABGR);
	background.getGraphics().drawImage(backImage,0,0,this);
	Image foreImage = new ImageIcon("images"+fs+"rocketship.gif").getImage();
	rocketwidth = foreImage.getWidth(this);
	rocketheight = foreImage.getHeight(this);
	foreground = new BufferedImage(rocketwidth,rocketheight,BufferedImage.TYPE_4BYTE_ABGR);
	foreground.getGraphics().drawImage(foreImage,0,0,this);
	javax.swing.Timer ticker = new javax.swing.Timer(delay, this);
	ticker.start();
	setPreferredSize(new Dimension(backwidth,backheight));
	rockety = (backheight - rocketheight)/2;
	rocketx = -rocketwidth;
	System.out.println(backwidth);
    }
    
    public void paintComponent(Graphics g) {
	g.drawImage(background,0,0,this);
	g.drawImage(foreground,rocketx,rockety,this);
    }
    
    public void actionPerformed(ActionEvent e) {
	rocketx+=1;
	if (rocketx > backwidth)
	    rocketx = -rocketwidth;
	repaint();
	// System.out.println("Tick");
    }
        
    public static void main(String[] args) {
	JFrame f = new JFrame("Panel1 animation demo");
	JPanel p = new Panel1(DEFAULT_FPS);
	f.setContentPane(p);
	f.pack();
	f.setVisible(true);
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
