一个类似屏幕保护的小球运动程序

学习线程这一块时,弄了一个简单的小球在界面运动的程序。 可以添加任意个小球进去,小球运动到墙面时会反弹回来。

//数据接口
[code]public interface Config {

public static final int height = 40 ; //球的高
public static final int wight = 40; //球的宽
public static final int boardHeight = 600; //画板的高
public static final int boardWight = 700; //画板的宽
public static final int UIboardHeight = 700; //窗口的高
public static final int UIboardWight = 700; //窗口的宽
}
[/code]


//主界面
[code]
public class MyBall extends JFrame{


public static void main(String [ ] args){
MyBall mb = new MyBall( );
mb.init( );

}


Graphics g;
public void init ( ){

//创建窗口
this.setTitle("滚动的球");
this.setSize(Config.UIboardWight, Config.UIboardHeight);
this.setDefaultCloseOperation(3);
this.setLayout(new FlowLayout());
//添加按钮
JButton jb = new JButton("开始");

JPanel jp1 = new JPanel ();
jp1.setBackground(Color.GRAY);

JPanel jp = new JPanel( );
jp.setPreferredSize(new Dimension(Config.boardWight,Config.boardHeight));
jp1.add(jb);
this.add(jp1,BorderLayout.NORTH);
this.add(jp,BorderLayout.CENTER);

this.setVisible(true);
MyLisener my = new MyLisener(jp);
jb.addActionListener(my);
}

}
[/code]

//监听器

[code]
public class MyLisener implements ActionListener {
Graphics g;
JPanel jp ;


public MyLisener (JPanel jp){

this.jp = jp;
}

public void actionPerformed(ActionEvent e) {

System.out.println("MyLisener.actionPerformed()");
Myframe mf = new Myframe(jp,g);
mf.start( );


}

}

[/code]

线程 : 画小球运动采用的是:画一个球,就将前一个球的颜色变成背景色。
[code]
public class Myframe extends Thread {
public int x = 0;
public int y = 0 ;
Graphics g;
JPanel jp ;

//重新覆盖时的x y 的值


double a =0 ;
double b =0 ;
int x0 = 0;
int y0 = 0;


public void run( ){

draw();
}
//重写构造函数
public Myframe(JPanel jp , Graphics g) {
this.g = g;
this.jp = jp;
}



//画图方法
public void draw( ){

System.out.println("执行了");

//随机位置出现
Random rad = new Random();
x = Config.boardWight/(rad.nextInt(50)+1);
y =Config.boardWight /(rad.nextInt(50)+1);


while(true){
draw1( );
}
}


//睡眠时间函数
private void ss(){

try {
sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
}

//球的运动和撞墙的函数
private void runXY( ) {
if (x == 0)
{
a =0 ;
}
if (x == Config.boardWight - Config.wight){
a =Math.PI;
}
x= x + (int )Math.cos(a);
if (y == 0)
{
b =0 ;
}
if (y == Config.boardHeight - Config.height){
b =Math.PI;
}
y = y + (int)Math.cos(b);


}



//画小球
private void draw1( ){
g = jp.getGraphics( );
//画颜色和背景色一样的球,去掉黑条
Color cl = jp.getBackground();
g.setColor(cl);
g.fillOval(x0, y0, Config.wight ,Config.height );

//画球
Color color = new Color(255,0,0);
g.setColor(Color.pink);
g.fillOval(x, y, Config.wight, Config.height);
x0 = x ; y0 = y;

//调用球运动的函数
runXY( );

//睡眠时间函数调用
ss();
}

}

[/code]
正常运行:
[img]http://dl2.iteye.com/upload/attachment/0091/6707/3362dcec-182e-3a4c-9651-42bd9946fc3f.png[/img]

我想把小球设置成透明的时候,就出现了严重的闪屏现象。 不过看动画效果还有点像流星雨来着


[img]http://dl2.iteye.com/upload/attachment/0091/6715/19b64f71-5d9a-36ac-8944-b457ee833bf9.png[/img]


这只是个基本的模型。 接下来一段时间会把小球之间碰撞的函数添加进去,然后再用双缓冲通道实现绘图 。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值