彈力球遊戲

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package bouncegame;

 

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

 

/**

 *

 * @author zakixumingze

 */

public class BounceFrame extends JFrame {

 

    private BouncePanel bouncePanel;//1.放置球的容器

    private JPanel buttonPanel;//1.放置操作鈕的容器

    private JButton btnStart;//開始按鈕

    private JButton btnClose;//關閉按鈕

 

    public BounceFrame() {

        setTitle("彈力球遊戲");

        setSize(600, 450);//設置窗體大小

 

        //2.構建兩個窗體對象放到窗體中

        bouncePanel = new BouncePanel();

        this.add(bouncePanel, BorderLayout.CENTER);

        buttonPanel = new BouncePanel();

        this.add(buttonPanel, BorderLayout.SOUTH);

 

        //3.構建兩個按鈕對象並添加到按鈕面板中

        btnStart = new JButton("開始");

        buttonPanel.add(btnStart);

        btnClose = new JButton("關閉");

        buttonPanel.add(btnClose);

 

        //兩個按鈕對象編寫點擊事件

        btnStart.addActionListener(new ActionListener() {

 

            public void actionPerformed(ActionEvent e) {

                addBall();

            }

        });

        btnClose.addActionListener(new ActionListener() {

 

            public void actionPerformed(ActionEvent e) {

               System.exit(0);

            }

        });

 

 

 

    }

 

    public void addBall() {

        Ball b = new Ball(); //構建一個新球

        bouncePanel.add(b); //把球添加到容器中

        BallThread bt = new BallThread(b, bouncePanel); //構建球線程對象

        bt.start();//啓動線程

    }

 

    public static void main(String[] args) {

        BounceFrame bf = new BounceFrame();//1.

        bf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        bf.setVisible(true);

    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bouncegame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
/**
 *
 * @author Administrator
 */
public class BouncePanel extends JPanel {
    private List<Ball> balls = new ArrayList<Ball>();
    public void add(Ball b){
        balls.add(b);
    }
    @Override
    public  void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.red);
        for(Ball b : balls){
            g2.fill(b);//用球填充
        }
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bouncegame;
import java.awt.Container;
import java.awt.geom.Rectangle2D;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Administrator
 */
public class BallThread extends Thread {
    private Ball b;
    private Container c;//球所放的容器
    public BallThread(Ball b,Container c){
        this.b = b;
        this.c = c;
    }
    /**
     * 處理球的移動
     */
    @Override
     public void run(){
        while(true){
            try {
                b.move(c.getBounds());
                c.repaint();//球移動後要容器刷新所有的對象
                Thread.sleep(1);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
     }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bouncegame;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
/**球的功能
 * 1.能夠繪製一個圓形的實體球
 * 2.能夠在容器中移動
 * @author Administrator
 */
class Ball extends Ellipse2D.Double {
    //private double x = 0;//球默認位置的x座標
    //private double y = 0;//球默認位置的y座標
    //private double w = 15;//球的外接圓的寬
    //private double h = 15;//球的外接圓的高
    private double dx; //球在x座標上移動的步長
    private double dy; //..
    /**
     *
     * @param ax  x座標
     * @param ay  y座標
     * @param aw  球的寬度
     * @param ah  球的高度
     * @param xl
     * @param yl
     */
    private Ball(double ax,double ay,double aw,double ah,double xl, double yl){
        super(ax, ay, aw, ah);
       dx = xl;
       dy = yl;
    }
    public Ball(){
        this(0, 0, 15, 15 ,1, 1);
    }
    public void move(Rectangle2D bounds){
        this.x += dx;
        this.y += dy;
        //當球處於左邊界
        if(this.x < bounds.getMinX()){
            this.x = bounds.getMinX();
            dx = -dx;//反彈
        }
        //當球處於右邊界時
        if((this.x +this.width) > bounds.getMaxX()){
            this.x = bounds.getMaxX() - this.width;
            dx = -dx;
        }
        //當球處於頂部
            if(this.y < bounds.getMinY()){
                this.y = bounds.getMinY();
                dy = -dy;
            }
        //當球處於底部
        if((this.y + this.height) > bounds.getMaxY()){
            this.y =bounds.getMaxY()-this.height;
            dy = -dy;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章