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


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

//数据接口
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;  //窗口的宽
}



//主界面
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);
	} 
	
}


//监听器

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( );
		   

	}
		   
	} 



线程 : 画小球运动采用的是:画一个球,就将前一个球的颜色变成背景色。
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();
	}
	
}


正常运行:
一个类似屏幕保护的小球运动程序_第1张图片

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


一个类似屏幕保护的小球运动程序_第2张图片


  这只是个基本的模型。 接下来一段时间会把小球之间碰撞的函数添加进去,然后再用双缓冲通道实现绘图 。

你可能感兴趣的:(thread)