JSwing | Create a Magnifying tool using Java Robot
Java Robot is a part of Java AWT (Abstract Window Toolkit ) package . Java Robot is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The purpose of using Java Robot is to gain the control of input events such as mouse, keyboard etc.
In this article we will create a Magnifying toolusing JAVA Robot .
Methods used :
- getPointerInfo() : Returns a PointerInfo instance that represents the current location of the mouse pointer.
- getLocation() : returns a point instance that represents location
- createScreenCapture(Rectangle r) : captures a part of screen which is within the rectangle r.
- drawImage(Image i, int x, int y, ImageObserver o) : draws an image at x, y position on the screen specifying an image observer
- drawImage(Image i, int x, int y,,int w, int h, ImageObserver o) : draws an image at x, y position and specified width and height on the screen specifying an image observer
Java program to create a Magnifying tool using Java Robot
Java
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class magnify extends JFrame {
static magnify m;
Image i;
magnify()
{
super ( "magnify" );
setSize( 200 , 220 );
show();
work();
}
public static void main(String args[])
{
m = new magnify();
}
public void work()
{
try {
Robot r = new Robot();
while (isVisible()) {
Point p = MouseInfo.getPointerInfo().getLocation();
i = r.createScreenCapture( new Rectangle(( int )p.getX() - 30 ,
( int )p.getY() - 30 , 150 , 150 ));
repaint();
}
System.exit( 0 );
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
public void paint(Graphics g)
{
g.drawImage(i, 0 , 0 , 300 , 300 , this );
}
}
|
Output :
Note: the following program might not run in an online compiler please use an offline IDE.Please use a latest version of java
Last Updated :
14 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...