Results 1 to 5 of 5

Thread: How to create mouse event using Robot class in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How to create mouse event using Robot class in java?

    Hello friends,
    I am new to this forum. I am working on live project where I am use java as front end and SQL as back end. I want to create mouse event using Robot class. I tried various method but none of them worked out. Can anyone tell me how to create mouse event using Robot class in java? Please give me any example of it.
    Thanks in advanced.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,949

    Re: How to create mouse event using Robot class in java?

    Hey it is very easy process to create mouse event using Robot class in java. You have to just pass Mouseevent() method to robot class to achieve this. In the following program I have automated the process of creating mouse event using the java.awt.Robot class. Just try to understand it.


    Code:
    package sam.java.example.awt;
    
    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.InputEvent;
    
    public class MovingMouseEg {
        public static void main(String[] args) {
            try {
                Robot robotic = new Robot();
    
                robot.mouseMove(250, 250);
    
                
                robotic.mousePress(InputEvent.Button_MASK);
                robotic.mouseRelease(InputEvent.Button1_MASK);
    
                
                robotic.mouseWheel(-150);
            } catch (AWTException es) {
                es.printStackTrace();
            }
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,015

    Re: How to create mouse event using Robot class in java?

    You have to use two class java.awt.Graphics and java.awt.Graphics2D to do this. It is used to load mouse event in your program. In following program I have create mouseCickEg class to do this. I also have use two variable to decide assign height and width for creating mouse event. I have use getContentPane() to add all these information into one panel.



    Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class mouseCickEg {
    
        public static void main(String[] args) {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame fsw = new JFrame("Test");
            fsw.setAlwaysOnTop(true);
            Component csw = new JPanel() {
                    @Override
                    public void paintComponent(Graphics gws) {
                            Graphics2D g2ws = (Graphics2D)gws.create();
                            g2.setColor(Color.graysw);
                            int wsw = getWidth();
                            int hsw = getHeight();
                            g2sw.fillRect(0, 0, wsw,hsw);
                            g2ws.setComposite(AlphaComposite.Clear);
                            g2ws.fillRect(w/4, h/4, w-2*(w/4), h-2*(h/4));
                    }
            };
            c.setPreferredSize(new Dimension(350, 350));
            fsw.getContentPane().add(csw);
            fsw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fsw.pack();
            fsw.setVisible(true);
            com.sun.awt.AWTUtilities.setWindowOpaque(fsw,false);
        }
    
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,010

    Re: How to create mouse event using Robot class in java?

    Robot class is used to develop remote system input events for the purposes of testing automation. It is also used to self-running demos. For creating mouse event you have to use mouseMove() method. You can do this in following way.

    public void mouseMove(int a, int b)

    Where x is referred to X position and
    y is referred to Y position

  5. #5
    Join Date
    May 2008
    Posts
    2,302

    Re: How to create mouse event using Robot class in java?

    It is very easy process to create mouse event using Robot class in java. I have written following program for you. Just try to understand it. It is very simple program. In following program I have use MouseEg to define robot class. After creating robot class I have created object of it which is known as "roboq". Go through each line of following program.



    Code:
    import java.awt.Robot;
    import java.awt.event.KeyEvent;
    
    public class MouseEg {
      public static void main(String[] args) throws Exception {
        Robot roboq = new Robot();
    
        robot.delay(3360);
    
        roboq.keyPress(KeyEvents.VK_Q);
        roboq.keyPress(KeyEvents.VK_W);
        roboq.keyPress(KeyEvents.VK_E);
        roboq.keyPress(KeyEvents.VK_R);
        roboq.keyPress(KeyEvents.VK_T);
        roboq.keyPress(KeyEvents.VK_Y);
      }
    }

Similar Threads

  1. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  2. How to use Event class of java?
    By Protectors in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 07:15 AM
  3. How to create a class in java script
    By Lawford in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 01:16 AM
  4. How to create key press event using Robot class in java?
    By Kasper in forum Software Development
    Replies: 4
    Last Post: 03-02-2010, 05:33 PM
  5. How to create an inner class in java?
    By MABON in forum Software Development
    Replies: 4
    Last Post: 01-02-2010, 03:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •