java向量组——设计一个球,使它由一边(A点)直线运动到另一边(B点),运动到中间时,A点出现第二个球,两球一起运动,当第一个球到达B点时消失(移除),当第二个球运动到中间时,...
java向量组——设计一个球,使它由一边(A点)直线运动到另一边(B点),运动到中间时,A点出现第二个球,两球一起运动,当第一个球到达B点时消失(移除),当第二个球 运动到中间时, A点再出现第一个球和第二个一起运动.........就这样一直重复下去(可在下面改)
public class GoingBoll extends JFrame{
mypanel mp=null;
public static void main(String[] args) {
GoingBoll goingboll=new GoingBoll();
}
public GoingBoll(){
this.setTitle("goingboll");
this.setSize(800,500);
this.setLocation(200, 150);
this.setVisible(true);
mp=new mypanel();
this.add(mp);
}
}
class mypanel extends JPanel implements Runnable{
boll b=null;
public mypanel(){
b=new boll(700, 300, 10);
Thread n=new Thread(b);
n.start();
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.blue);
g.fillOval(b.getX(), b.getY(), 50, 50);
this.repaint();
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
class boll implements Runnable{
int x;
int y;
int s;
boolean bLive=true;
//Vectorbolls=new Vector();
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 getS() {
return s;
}
public void setS(int s) {
this.s = s;
}
public boll(int x,int y,int s){
this.s=s;
this.x=x;
this.y=y;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
}
while(bLive){
try {
Thread.sleep(7);
} catch (Exception e) {
// TODO: handle exception
}
x-=3;
System.out.println("x="+x);
if(x<50){
bLive=false;
}
}
}
}
展开