我的java学习之路,想象,图形界面

//一个球从 100 米高度自由落下,每次落地后反跳回原package BallOrAnt;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.Timer;






public class BallJframe extends JFrame implements FocusListener, ActionListener {
private double height;
private JButton btnStart;
private JComboBox comboxNum,comboxHeight;
private int num;
private Timer timer=new Timer(10, this);
private JTextField tfdBound,tfdRoute;
private BallCanvas ballcanvas;
public BallJframe(double height) {
super("The free fall motion simulation");
Dimension dim=getToolkit().getScreenSize();
this.setBounds(dim.width/8,dim.height/8,dim.width*2/3+100,dim.height*2/3);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
JToolBar toolbar=new JToolBar();//工具栏


toolbar.setLayout(new FlowLayout( FlowLayout.LEFT,20, 5));
this.getContentPane().add(toolbar,BorderLayout.NORTH);
JLabel lbHeght=new JLabel("下落高度:");
toolbar.add(lbHeght);
String[] str={"100","200","300","400"};
String[] numStr={"1","2","3","4","5","6","7","8","9","10"};
comboxHeight=new JComboBox(str);
toolbar.add(comboxHeight);
JLabel lbNum=new JLabel("弹起次数:");
toolbar.add(lbNum);
comboxNum=new JComboBox(numStr);
toolbar.add(comboxNum);
comboxNum.addActionListener(this);
comboxHeight.addActionListener(this);
comboxNum.addActionListener(this);
toolbar.add(new JLabel("弹起高度:")) ;
tfdBound=new JTextField(""+0,10);
tfdBound.setEditable(false);
toolbar.add(tfdBound);

toolbar.add(new JLabel("总路径:")) ;
tfdRoute=new JTextField(""+0,10);

tfdRoute.setEditable(false);
toolbar.add(tfdRoute);

btnStart=new JButton("开始下落");
toolbar.add(btnStart);
btnStart.addActionListener(this);
//ballcanvas=new BallCanvas(height);
// this.getContentPane().add(ballcanvas);
height=Double.parseDouble(""+comboxHeight.getSelectedItem());
this.setVisible(true);
}

public static void main(String[] args) {
new BallJframe(100);
}


@Override
public void focusGained(FocusEvent e) {
ballcanvas.timer.restart();

}


@Override
public void focusLost(FocusEvent e) {
ballcanvas.timer.stop();

}

public class BallCanvas extends JPanel implements ActionListener {
private  double height=0;
private double rount=0;
private double sun=0;
private Ball ball;
Timer timer;
public BallCanvas(double height) {
this.height=height;
ball=new Ball(200, 10);
ball.height=height;
ball.y=(int)(430-height);
timer=new Timer(100, this);
}

public double stopOfKeep(){
double sum=0.0,begin=100;
for(int i=0;i<num;i++){
if(i==num-1){
sum = sum + begin;
}else{
sum = sum + begin +begin/2;
}
begin = begin/2;
}
return begin;
}
public double stopOfSum(){
double sum=0.0,begin=100;
for(int i=0;i<num;i++){
if(i==num-1){
sum = sum + begin;
}else{
sum = sum + begin +begin/2;
}
begin = begin/2;
}
return sum;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);//清除残影
g.setColor(Color.blue);
g.fillOval(ball.x,ball.y, 10, 10);
if(ball.y>=this.getHeight()){
ball.isFall=false;
ball.height/=2;
}
if(ball.y<=430-ball.height){
ball.isFall=true;
}
if(ball.isFall==true){
ball.y+=10;
rount+=10;
}else{
ball.y-=10;
rount+=10;
}
if(ball.height<stopOfKeep()){
timer.stop();
BallJframe.this.height/=2;
tfdRoute.setText(""+stopOfSum());
g.fillOval(ball.x,this.getHeight()-10, 10, 10);
}
}
@Override
public void actionPerformed(ActionEvent e) {
tfdBound.setText(""+ball.height/2);
tfdRoute.setText(""+ballcanvas.getRount());
repaint();//刷新画布
}



public double getRount() {
return rount;
}

public  class Ball{
private int x,y;
private boolean isFall=true;
private double height;
private double sum=0;
public Ball(int x, int y) {
this.x = x;
this.y = y;
}
public void fall(){
sum = sum + this.height;
}
public void bound(){
this.height = this.height/2;
sum = sum + this.height;

}

}
public double getMyHeight() {
return ball.height;
}
public void setMyHeight(double height){
ball.height=height;
}
public void mydispose(){
dispose();
}






}


@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnStart){
height=Double.parseDouble(""+comboxHeight.getSelectedItem());
ballcanvas=new BallCanvas(height);
this.getContentPane().add(ballcanvas);
this.setVisible(true);
ballcanvas.timer.start();

}
if(e.getSource()==comboxHeight){
height=Double.parseDouble(""+comboxHeight.getSelectedItem());

}
if(e.getSource()==comboxNum){
num=Integer.parseInt(""+comboxNum.getSelectedItem());
}
}












}高度的一半;再落下,求它在第
10 次落地时,共经过多少米?第 10 次反弹多高?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值