用java实现跳动的小球示例代码
实现效果为一个小球接触左右侧时,会反向的运动。
importjavafx.application.Application;
importjavafx.event.ActionEvent;
importjavafx.event.EventHandler;
importjavafx.scene.Group;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.paint.Color;
importjavafx.scene.shape.Ellipse;
importjavafx.stage.Stage;
importjava.util.Timer;
importjava.util.TimerTask;
publicclassBallMoveextendsApplication{
//x记录小球的横坐标,默认值为初始位置
staticintx=200;
//e为小球
staticEllipsee=newEllipse();
//temp记录小球的移动情况:当temp为left时,左移;temp为right时,右移
staticStringtemp="left";
//创建计时器
staticTimert=newTimer();
//创建记录器,当多次点击过“确定”按钮后,只有第一次点击有效
staticbooleanrecord=true;
publicstaticvoidmain(String[]args){
launch(args);
}
publicvoidstart(Stages){
//创建Group面板
Grouproot=newGroup();
//创建场景
Scenescene=newScene(root,400,250,Color.WHITE);
//创建按钮
Buttonstart=newButton("开始");
Buttonstop=newButton("停止");
//创造一个小球
e.setCenterX(x);
e.setCenterY(90);
e.setRadiusX(50);
e.setRadiusY(50);
e.setFill(Color.RED);
//放置开始按钮
start.setLayoutX(100);
start.setLayoutY(180);
//放置停止按钮
stop.setLayoutX(250);
stop.setLayoutY(180);
//为开始按钮添加事件
start.setOnAction(newEventHandler(){
publicvoidhandle(ActionEventevent){
System.out.println("开始按钮被触发");
if(record==true){
t=newTimer();
t.schedule(newTimerTask(){
publicvoidrun(){
e.setFill(Color.rgb((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
//小球的半径为50,场景的宽度为400,那么小球横坐标达到50或者350时,转向移动
if(x<50){temp="right";}
if(x>350){temp="left";}
if(temp.equals("left")){e.setCenterX(x-=5);
}else{e.setCenterX(x+=5);}
}
},0,25);
}
//“开始"按钮被点击且事件被触发,record=false;
record=false;
}
});
//为停止按钮添加事件
stop.setOnAction(newEventHandler(){
publicvoidhandle(ActionEventevent){
System.out.println("停止按钮被触发");
record=true;
t.cancel();
}
});
root.getChildren().add(e);
root.getChildren().add(start);
root.getChildren().add(stop);
s.setTitle("移动小球");
s.setScene(scene);
s.show();
}
}
我还做了一个升级版,扩展到上下左右一起移动,代码如下
importjavafx.application.Application;
importjavafx.scene.Group;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.paint.Color;
importjavafx.scene.shape.Ellipse;
importjavafx.scene.shape.Line;
importjavafx.stage.Stage;
importjava.util.Timer;
importjava.util.TimerTask;
publicclassBallMove2extendsApplication{
//x记录小球的横坐标,默认值为初始位置
staticintx=200;
//y记录小球的纵坐标,默认值为初始位置
staticinty=90;
//distance_x记录小球每次横向移动的距离,取1到4之间的随机数
staticdoubledistance_x=Math.random()*4+1;
//distance_y记录小球每次纵向移动的距离,由于每次移动的距离为5,由distance_x可求出distance_y
staticdoubledistance_y=Math.sqrt(25-distance_x*distance_x);
//e为小球
staticEllipsee=newEllipse();
//temp1记录小球的横向移动情况:当temp为left时,左移;temp为right时,右移
staticStringtemp1="left";
//temp2记录小球的纵向移动情况:当temp为up时,上移;temp为down时,下移
staticStringtemp2="down";
//创建计时器
staticTimert=newTimer();
//创建记录器,当多次点击过“确定”按钮后,只有第一次点击有效
staticbooleanrecord_start=true;
staticbooleanrecord_stop=false;
publicstaticvoidmain(String[]args){
launch(args);
}
publicvoidstart(Stages){
/*System.out.println(distance_x+"***"+distance_y);*/
//创建Grooup面板
Grouproot=newGroup();
//创建场景
Scenescene=newScene(root,400,250,Color.WHITE);
//创建按钮
Buttonstart=newButton("开始");
Buttonstop=newButton("停止");
//创建一条分割线,分割小球和按钮
Linel=newLine();
//放置线条
l.setStartX(0);
l.setStartY(160);
l.setEndY(160);
l.setEndX(400);
//放置小球
e.setCenterX(x);
e.setCenterY(y);
e.setRadiusX(20);
e.setRadiusY(20);
e.setFill(Color.RED);
//放置开始按钮
start.setLayoutX(100);
start.setLayoutY(190);
//放置停止按钮
stop.setLayoutX(250);
stop.setLayoutY(190);
//为开始按钮添加事件
start.setOnAction(event->{
/*创建一个小球随机角度移动的思路:
假设小球每次移动的距离为5,当横坐标或者纵坐标其中一个确定时,另外可以根据三角函数求出
现在可以用随机函数,令横坐标为1到4之间随机的数字,那么横坐标也可以由此求出
如何验证每次角度不同?
当点击“停止”按钮之后,再次点击“停止”按钮即可重置小球位置和移动的角度
**/
if(record_start){
t=newTimer();
t.schedule(newTimerTask(){
publicvoidrun(){
//随机取颜色,justhaveafun
//e.setFill(Color.rgb((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
//小球的半径为20,场景的宽度为400,那么小球横坐标达到20或者380时,转向移动
if(x<20){temp1="right";}
if(x>380){temp1="left";}
//小球的半径为20,场景的高度为160,那么小球纵坐标达到20或者140时,转向移动
if(y<20){temp2="up";}
if(y>140){temp2="down";}
if(temp1.equals("left")){e.setCenterX(x-=distance_x);
}else{e.setCenterX(x+=distance_x);}
if(temp2.equals("down")){e.setCenterY(y-=distance_y);
}else{e.setCenterY(y+=distance_y);}
}
},0,20);
}
//“开始"按钮被点击且事件被触发,record=false;
record_start=false;
record_stop=false;
});
//为停止按钮添加事件
stop.setOnAction(event->{
/*System.out.println("停止按钮被触发");*/
//当第二次点击"停止"时,小球重置
if(record_stop){
//重置横向和纵向移动的距离
distance_x=Math.random()*4+1;
distance_y=Math.sqrt(25-distance_x*distance_x);
//重置小球的位置
e.setCenterX(x=200);
e.setCenterY(y=90);
record_stop=false;
}
record_stop=true;
record_start=true;
t.cancel();
});
root.getChildren().addAll(start,stop,l,e);
s.setTitle("弹跳小球");
s.setScene(scene);
s.show();
}
}
以上代码设置了个彩蛋,不知道你能不能找到!
总结
到此这篇关于用java实现跳动的小球示例代码的文章就介绍到这了,更多相关java跳动的小球内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!