我想写一个小球碰撞的动画程序,可是死活显示不出来>.<
小程序有3个类,小球,窗口,还有一个画画的,具体见下文
窗口
import java.awt.Color;
import java.util.Random;
import javax.swing.*;
public class Frame {
ball ball[] = new ball[10];
Random a = new Random();
public static void main(String[] args) {
// TODO 自动生成的方法存根
Frame frame = new Frame();
frame.happen();
}
public void happen(){
JFrame frame=new JFrame();//窗口属性
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setSize(800,800);
for (int i = 0; i < ball.length; i++){//实例化10个小球
ball[i] = new ball(new Color(a.nextInt(255), a.nextInt(255), a.nextInt(255)),
a.nextInt(750), a.nextInt(750), a.nextInt(4) + 1, a.nextInt(4) + 1, 50);
}
paint paint = new paint(ball);//用paint类来画小球
frame.add(paint);//paint类加到窗口中
while(true){//我想让动画一直动下去,所以设了个无限循环
for (int i = 0; i < ball.length; i++){ball[i].move();}
collision();//判断小球们的间距
paint.repaint();//小球动完后画出新位置
try {//加延迟防止动得太快看不清
Thread.sleep(10);
} catch (Exception e){e.printStackTrace();}
}
}
public void collision(){
double[][] distance = new double[ball.length][ball.length];
for (int i = 0; i < ball.length; i++){
for (int j = 0; j < ball.length; j++){
distance[i][j] = Math.sqrt(Math.pow(ball[i].getX()-ball[j].getX(), 2) + Math.pow(ball[i].getY()-ball[j].getY(), 2));
}
}
for (int i = 0; i < ball.length; i++){
for (int j = i + 1; j < i; j++){
if (distance[i][j] < ball[i].getR()+ball[j].getR()){ball[i].crash(ball[j]);}//小球碰撞时调用crash方法
}
}
}
}
小球
import java.awt.*;
public class ball{
private int X,Y,Vx,Vy,R;
private Color color;
public ball(Color color, int x, int y, int vx, int vy, int r){
this.color=color;
this.X=x;
this.Y=y;
this.Vx=vx;
this.Vy=vy;
this.R=r;
}
public void move(){
X+=Vx;
Y+=Vy;
if((X<=R)||(X>=800-R)){Vx=-Vx;}
if((Y<=R)||(Y>=800-R)){Vy=-Vy;}
if(X<R){X=R;}
if(X>800-R){X=800-R;}
if(Y<R){Y=R;}
if(Y>800-R){Y=800-R;}
}
public void crash(ball another){
double a = Math.abs(this.X-another.X)/(this.R+another.R);
double b = Math.sqrt(1-a*a);
double v1 = Vx*a-Vy*b;
double v2 = another.Vx*a-another.Vy*b;
int m1 = R*R, m2 = another.R*another.R;
double v3 = ((m1-m2)*v1+2*m2*v2)/(m1+m2);
double v4 = ((m2-m1)*v2+2*m1*v1)/(m1+m2);
this.setVx((int)(Vx*b+v3*a));
this.setVy((int)(Vy*a+v3*b));
another.setVx((int)(another.Vx*b+v4*a));
another.setVy((int)(another.Vy*a+v4*b));
}
public void setVx(int i){
Vx=i;
}
public void setVy(int i){
Vy=i;
}
public void setX(int i){
X=i;
}
public void setY(int i){
Y=i;
}
public int getVx(){
return Vx;
}
public int getVy(){
return Vy;
}
public int getX(){
return X;
}
public int getY(){
return Y;
}
public int getR(){
return R;
}
public Color getcolor(){
return color;
}
}
画画用的类,问题也是它
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class paint extends JPanel{
ball[] ball;
public paint(ball ball[]){
this.ball = ball;
}
public void painter(Graphics g){
g.setColor(Color.green);
g.fillRect(0, 0, 800, 800);
for (int i = 0; i < ball.length; i++){
g.setColor(ball[i].getcolor());
g.fillOval(ball[i].getX()-ball[i].getR(), ball[i].getY()-ball[i].getR(), 2*ball[i].getR(), 2*ball[i].getR());
}
}
}
描述
程序在eclipse里运行了,只得到一个空白的窗口。我试过在语句后面加输出hello world来判断运行到哪里,结果发现frame.add(paint);的确运行了,也进到无限循环那里了,不过paint的painter()方法没有被执行。关于JPanel的这个用法我是Head first Java上看的,确实是可行的呀··· 不知为何这里就不行了
希望有高手指点一下