鼠标按一定规律在窗口走

import   javax.swing. * ;  
import   java.awt. * ;  
import   java.awt.event. * ;  
 
public    class   testPaint   extends   JFrame   implements   Runnable   {  
   
private  JPanel  contentPanel;  
   
private  Robot  robot;  
   
private  Thread  thread;  
   
private  JButton  button1;  
   
private  int  x,y;  
   
private  int  movex,movey;  
   
public  testPaint()  throws  Exception  {  
       
super("testPaint");  
       contentPanel
=(JPanel)this.getContentPane();  
       button1
=new  JButton("start");  
       robot
=new  Robot();  
       thread
=new  Thread(this);  
       x
=y=0;  
       movex
=2;  
       movey
=3;  
   }
  
 
   
private  void  init()  {  
       
this.setSize(600,450);  
       
this.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-this.getWidth()/2,  
               Toolkit.getDefaultToolkit().getScreenSize().height
/2-this.getHeight()/2);  
       
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
       contentPanel.setLayout(
new  FlowLayout());  
       contentPanel.add(button1);  
       clickButton  cb
=new  clickButton();  
       contentPanel.addMouseMotionListener(
new  movePanel());  
       button1.addMouseListener(cb);  
       
this.addWindowListener(new  closeWin());  
       
this.setVisible(true);  
       
this.setExtendedState(JFrame.MAXIMIZED_BOTH);  
       
this.setResizable(false);  
   }
  
 
   
public  void  run()  {  
       
try  {  
           
while(true){  
               
if  (x  <=  10)  
                   movex  
=  2;  
               
if  (x  >=  450)  
                   movex  
=  -2;  
               
if(y<=10)  
                   movey
=3;  
               
if(y>=600)  
                   movey
=-3;  
           x
+=movex;  
           y
+=movey;  
           robot.mouseMove(x,y);  
           Thread.sleep(
50);  
           }
  
       }
catch(Exception  e){  
           
if(thread!=null){  
               thread.destroy();  
               thread
=null;  
           }
  
       }
  
   }
  
 
   
public  static  void  main(String[]  args)  {  
       
try{  
           testPaint  tp
=new  testPaint();  
           tp.init();  
       }
catch(Exception  e){  
           e.printStackTrace();  
           System.exit(
-1);  
       }
  
   }
  
class  closeWin  extends  WindowAdapter  {  
   
public  void  windowClosing(WindowEvent  e)  {  
       e.getWindow().setVisible(
false);  
       e.getWindow().dispose();  
       System.exit(
0);  
   }
  
}
  
 
class  clickButton  extends  MouseAdapter  {  
   
public  void  mouseClicked(MouseEvent  e)  {  
       
super.mouseClicked(e);  
       JButton  button
=(JButton)e.getSource();  
       
try{  
           
if(thread==null){  
               thread
=new  Thread(testPaint.this);  
           }
  
               thread.start();  
       }
catch(Exception  ex){  
           ex.printStackTrace();  
       }
  
   }
  
}
  
 
class  movePanel  extends  MouseMotionAdapter{  
   
public  void  mouseMove(MouseEvent  e)  {  
       x
=e.getX();  
       y
=e.getY();  
   }
  
}
  
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值