actually only Robot can do this in java if no JNI code

解决方案 »

  1.   

    OK,要点专家分过年package xiruo.paint;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();
      }
    }
    }
      

  2.   

    3ks  beyond_xiruo(CorruptionException)!