package holan.tw;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;

public class GameAnimation extends JFrame implements ActionListener {
	final int SCREEN_WIDTH = 400;
	final int SCREEN_HEIGHT = 400;
	final int RECT_WIDTH = 20;
	final int RECT_HEIGHT = 20;
	final int DELAY_MS = 20;

	int dx = 1;
	int xPos = 0;
	int yPos = SCREEN_HEIGHT / 2;
	Timer timer;

	public GameAnimation() {
        setTitle("遊戲動畫");
        setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        
        timer = new Timer(DELAY_MS, this);
        timer.setInitialDelay(190);
        timer.start();
	}
   
    public void update(Graphics g) { 
        this.paint(g); 
    } 
 
    public void paint(Graphics g) { 
        super.paint(g);
        g.setColor(Color.GREEN);
        g.fillRect(xPos, yPos, RECT_WIDTH, RECT_HEIGHT);
    }

	public static void main(String[] args) {
		new GameAnimation().show();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		this.repaint();
    	xPos += 1;
    	if( xPos >= SCREEN_WIDTH ) xPos = 0;
	}
}