Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to create mouse event using Robot class in java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-02-2010
Member
 
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.

Reply With Quote
  #2  
Old 03-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,946
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();
        }
    }
}
Reply With Quote
  #3  
Old 03-02-2010
Member
 
Join Date: May 2008
Posts: 2,005
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);
    }

}
Reply With Quote
  #4  
Old 03-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,999
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
Reply With Quote
  #5  
Old 03-02-2010
Member
 
Join Date: May 2008
Posts: 2,289
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);
  }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to create mouse event using Robot class in java?"
Thread Thread Starter Forum Replies Last Post
Converting java class to java bean class Ucchal Software Development 6 09-08-2010 10:24 AM
How to use Event class of java? Protectors Software Development 5 25-02-2010 07:15 AM
How to create a class in java script Lawford Software Development 5 11-02-2010 01:16 AM
How to create key press event using Robot class in java? Kasper Software Development 4 03-02-2010 05:33 PM
How to create an inner class in java? MABON Software Development 4 01-02-2010 03:40 PM


All times are GMT +5.5. The time now is 05:00 AM.