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