编写一个窗体的类BallFrameDemo:
package com.lzy.ball;
import javax.swing.JFrame;
public class BallFrameDemo extends JFrame {
JFrame
frame; public
BallFrameDemo(){ frame=new
JFrame(); } public void
showSony(){ frame.setSize(800, 600);
frame.setTitle("多个小球的碰壁反弹");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false); //禁止最大化
frame.setLocationRelativeTo(null); //居中显示
BallPanelDemoA panel=new
BallPanelDemoA();
Thread td=new
Thread(panel);
td.start();
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args)
{ new
BallFrameDemo().showSony();
}
}
编写一个面板类BallPanelDemoA:
package com.lzy.ball;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class BallPanelDemoA extends JPanel implements Runnable
{
Random random;
BallDemo[] balls;
int
size=6; //设置小球的个数
public BallPanelDemoA(){
random=new Random();
balls=new BallDemo[size];
for(int
i=0;i
balls[i]=new
BallDemo(random.nextInt(700),random.nextInt(500),20+i*2,new
Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
this.setBackground(Color.BLUE);
for(int
i=0;i
balls[i].draw(g);
}
}
@Override
public void run() {
while(true){
for(int i=1;i
for(int j=0;j
balls[i].setPanel(this);
balls[i].move();
balls[j].setPanel(this);
balls[j].move();
if(balls[i].twoBallisHit(balls[j])){ //当两球相撞时,把一个球的半径设为0,相当于消失了,看不见了;
balls[i].setR(30);
balls[j].setR(0);
}
}
}
try {
Thread.sleep(50);
} catch
(InterruptedException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}
把小球封装在BallDemo中:
package com.lzy.ball;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class BallDemo {
private int x;
private int y;
private int r;
private Color color;
private JPanel panel;
private int flag=0;
public static final int
LEFT_DOWN=0; //小球的运行轨迹为左下:x++;y++
public static final int
LEFT_UP=1; //小球的运行轨迹为左上:x++;y--
public static final int
RIGHT_DOWN=2; //小球的运行轨迹为右下:x--;y++
public static final int
RIGHT_UP=3; //小球的运行轨迹为右上:x--;y--
public BallDemo(){}
public BallDemo(int x, int y, int r, Color color)
{ //通过写一个带参数的构造方法,方便BallDemo的对象把值传进来;
this.setX(x);
this.setY(y);
this.setR(r);
this.setColor(color);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public JPanel getPanel() {
return panel;
}
public void setPanel(JPanel panel) {
this.panel = panel;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public void draw(Graphics
g){ //在小球的自己类里面画小球,方便在外面调用数组获取多个小球;
g.setColor(color);
g.fillOval(x, y, 2*r,
2*r);
} public void
move(){ //每个小球都有自己的move()方法,用来判断自己的位置;
switch (this.flag) {
case LEFT_DOWN:
x++;
y++;
if(x>panel.getWidth()-2*r){
this.flag=RIGHT_DOWN;
}
if(y>panel.getHeight()-2*r){
this.flag=LEFT_UP;
}
break;
case LEFT_UP:
x++;
y--;
if(x>panel.getWidth()-2*r){
this.flag=RIGHT_UP;
}
if(y<0){
this.flag=LEFT_DOWN;
}
break;
case RIGHT_DOWN:
x--;
y++;
if(y>panel.getHeight()-2*r){
this.flag=RIGHT_UP;
}
if(x<0){
this.flag=LEFT_DOWN;
}
break;
case RIGHT_UP: