How to Click a desired pixels on the screen? - java

I'm trying to make a program that clicks the specified pixel on the screen.
I can do a screen capture like this
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle)
but I don't know how to make a procedure on this image.
What can I use to do this? Thanks :)

You can click on a specific Pixel with the mouse using Robot class in java
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.InputEvent;
public class MouseClicker {
static Robot rb;
public static void main(String[] args) {
try {
rb = new Robot();
} catch (AWTException e) {
System.out.println("Error while creating object" + e);
}
rb.mouseMove(1360, 768); //this method takes two parameters (Height, Width)
rb.mousePress(InputEvent.BUTTON1_DOWN_MASK);//this method takes one parameter (BUTTON) to press it
rb.delay(10); //this is the delay between every press
rb.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);//this method takes one parameter (BUTTON) to release it
}
}
For more refrance you can see the Oracle Docs

Related

How to move cursor and click on a specific pixel in java screen

I am trying to move cursor to a specific pixel in the screen and click on it using a java program.
I tried to capture a screenshot then work on it but I am stuck.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle)
You can click on a specific Pixel with the mouse using Robot class in java
and move it using move method.
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.InputEvent;
public class MouseClicker {
static Robot rb;
public static void main(String[] args) {
try {
rb = new Robot();
} catch (AWTException e) {
System.out.println("Error while creating object" + e);
}
rb.mouseMove(1360, 768); //this method takes two parameters (Height, Width)
rb.mousePress(InputEvent.BUTTON1_DOWN_MASK);//this method takes one parameter (BUTTON) to press it
rb.delay(10); //this is the delay between every press
rb.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);//this method takes one parameter (BUTTON) to release it
}
}
For more reference you can see the Oracle Docs.

How can I use java to capture the screen of some applications?

I have found that java awt can capture the full-screen, but how can I capture the screen of a specific applications? For example, I opened a matlab application and eclipse application. And I cannot tell the size of application screen to the program and just know the matlab is an active window now. And I want to only capture the screen of Matlab. How can I do that?
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import javax.imageio.ImageIO;
public class Screenshot {
public static final long serialVersionUID = 1L;
public static void main(String[] args)
{
try {
Thread.sleep(120);
Robot r = new Robot();
// It saves screenshot to desired path
String path = "D:// Shot.jpg";
// Used to get ScreenSize and capture image
Rectangle capture =
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage Image = r.createScreenCapture(capture);
ImageIO.write(Image, "jpg", new File(path));
System.out.println("Screenshot saved");
}
catch (AWTException | IOException | InterruptedException ex) {
System.out.println(ex);
}
}
}
To capture screenshot of a portion of the screen, we need to specify a rectangle region to be captured.the following statements create a capture region which is the first quarter of the screen.
try {
Robot robot = new Robot();
String format = "jpg";
String fileName = "PartialScreenshot." + format;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle captureRect = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2);
BufferedImage screenFullImage = robot.createScreenCapture(captureRect);
ImageIO.write(screenFullImage, format, new File(fileName));
System.out.println("A partial screenshot saved!");
} catch (AWTException | IOException ex) {
System.err.println(ex);
}

Keyboard input trigger mouse click

I want to create a program / script which triggers mouse clicks or drag an drops when the keyboard is used. For example: If u press 1, the mouse location is saved. If u press 2 the mouse will go to the saved location. I know this is possible in different programming languages and i was wondering which one is the best to use for this purpose. And could someone give me a little headstart?
Edit:
import java.awt.MouseInfo;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import java.awt.AWTException;
import java.awt.event.*;
public class nudan implements KeyListener{
int x1;
int y1;
public static void main(String[] args) throws AWTException{
JFrame jf = new JFrame("Key Event");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.addKeyListener(new nudan());
jf.setVisible(true);
jf.setAlwaysOnTop(true);
Robot rt = new Robot();
}
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == e.VK_NUMPAD1){
System.out.println("Key Pressed: " + e.getKeyChar());
this.y1 = MouseInfo.getPointerInfo().getLocation().y;
this.x1 = MouseInfo.getPointerInfo().getLocation().x;
}
}
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == e.VK_NUMPAD2){
System.out.println(x1);
System.out.println(y1);
try {
new Robot().mouseMove(x1, y1);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
}
#Override
public void keyTyped(KeyEvent e) {
}
}
Thank you so far guys. So this works. It saves the location and Prints '1' if you press Numpad 1. When numpad 2 is pressed, it goes to the saved location and prints the saved location. But somehow when i start my game and try to use this, my mouse doesn't move, eventhought it prints the locations so the script is still running. Anyone has a clue?
This can be done with pretty much every language in a fairly easy way, there is no need to use a specific language for it.
I assume you just started out programming, so one golden rule in programming: just Google it!
For java you can easily find the documentation of the MouseInfo class (java.awt.MouseInfo), which will in large provide the functionalities you'll need.
Java
import java.awt.MouseInfo;
public class ExampleMouseInfo {
public static void main(String[] args){
int mouseYPos = MouseInfo.getPointerInfo().getLocation().y;
int mouseXPos = MouseInfo.getPointerInfo().getLocation().x;
System.out.println(mouseXPos);
System.out.println(mouseYPos);
}
}
Output
488
477
This snippet of code will get the position of your mouse when you run the program. So if you want to trigger this part of code at a certain point (like you said by pressing a button) you make a function out of it and wrap it into an event handler.
Edit: example of moving a mouse can be found here.

Pick the color in the image by coordinates

I want to pick up the RBG value of the color in the image when the mouse clicks the position of the color. Actually, I put the image on the top left corner in the jFrame. I try to get the mouse location, for example, x= 190, y=80, which is near the last pixel of the image. However, the image size is 200x24. Therefore, I cannot convert the mouse pointer position to the pixel of the image. Is there any method to do this? Thank you.
Add more information:
I create a jframe and put a jlabel which is the image at the top left corner of the jframe. What I want to do it is: when I use the mouse point and click the position on the image, I would get the color of this position.
screen capture: http://i.stack.imgur.com/SjFhr.png
[when i use the mouse point to the black position of the image, it shows r=240,g=240,b=240]
frame.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
try {
System.out.println(getPointerColor());
Thread.sleep(1000);
} catch (AWTException awte) {
System.out.println("Error while getting pointer's color!");
} catch (InterruptedException ie) {
System.out.println("Error while sleeping!");
}
}
});
You can use the Robot class (see Documentation) to get the colour for a set of coordinates relative to the screen or the GraphicsDevice you want:
public Color getPixelColor(int x, int y) throws AWTException {
Robot robot = new Robot();
return robot.getPixelColor(x, y);
}
You can then retrieve the RGB values from that returned Color object. Make sure your coordinates are good though!
As an additional test, you can try running the following, which displays the pointer's pointed colour relative to the screen (in other words, the absolute cursor coordinates) every second:
import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
public class Test {
public static void main(String[] args) throws Exception {
while (true) {
try {
System.out.println(getPointerColor());
Thread.sleep(1000);
} catch (AWTException awte) {
System.out.println("Error while getting pointer's color!");
} catch (InterruptedException ie) {
System.out.println("Error while sleeping!");
}
}
}
public static Color getPointerColor() throws AWTException {
Point coordinates = MouseInfo.getPointerInfo().getLocation();
Robot robot = new Robot();
return robot.getPixelColor((int) coordinates.getX(), (int) coordinates.getX());
}
}
If it's a Swing UI, use JColorChooser for this: http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html. If you must do it manually, convert the mouse pointer position by subtracting a hard-coded offset.

Multiple instances of the same animated GIF in a Canvas (Java)

So I'm making a game where you can put bombs on the location of your character. Each bomb is associated with a GIF image when the bomb is displayed and eventually go BOOM (think about Bomberman).
The problem was, when i tried to paint more than one bomb on the screen, it was painted from the last frame of the GIF. Investigating, I found the method image.flush() to reset the GIF cicle but now the problem is that every time I paint a second bomb on the screen, the GIF cycle is reset for all previously bombs on screen.
Here is my constructor for each bomb:
public Tnt(int x, int y){
this.x = x;
this.y = y;
ImageIcon ii = new ImageIcon("src/main/resources/modelObjects/tnt.gif");
image = ii.getImage();
image.flush();
}
Every bomb i create enters an ArrayList (listTnt) and is removed after 6 secs, so i only paint the bombs already active.
Here is my method for drawing:
public void draw(Graphics2D g2d, JPanel board){
for(Tnt tnt: listTnt){
g2d.drawImage(tnt.getImage(), tnt.getX(), tnt.getY(), board);
}
}
EDIT: Seems that the problem was ImageIcon, since it reuses the image using Toolkit.getImage. Instead, Toolkit.createImage create a not reusable image.
Here is my new constructor for Tnt that worked perfectly:
public Tnt(int x, int y){
this.x = x;
this.y = y;
Toolkit t = Toolkit.getDefaultToolkit ();
image = t.createImage("src/main/resources/modelObjects/tnt.gif");
}
I dont even need image.flush() now. Thank you all.
The underlying Image is being reused amongst each ImageIcon.
Judging by the OpenJDK source code, it appears to be due to the fact that each simply requests the Image via Toolkit.getImage.
This method has a nifty caveat, however, which explains the issue at hand:
The underlying toolkit attempts to resolve multiple requests with the same filename to the same returned Image.
Instead, you should skip the ImageIcon step completely (since it's inappropriate to be using a Swing class unnecessarily in the first place), and instead call Toolkit.createImage, which states in the documentation:
The returned Image is a new object which will not be shared with any other caller of this method or its getImage variant.
Good luck.
As I did not know how to solve this, I tried #super_ solution and it works quite nicely. I share the code for anyone who wants an example. +1 to him
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class TestAnimatedGif {
private static final int IMAGE_COUNT = 9;
protected void initUI() {
JFrame frame = new JFrame(TestAnimatedGif.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
frame.add(panel);
frame.setSize(600, 400);
frame.setVisible(true);
final Timer t = new Timer(1000, null);
t.addActionListener(new ActionListener() {
int count = 0;
#Override
public void actionPerformed(ActionEvent e) {
if (count < IMAGE_COUNT) {
try {
JLabel image = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().createImage(
new URL("http://www.sitevip.net/gifs/bomba/BOMB-B_animado.gif"))));
panel.add(image);
count++;
panel.revalidate();
panel.repaint();
System.err.println("image added");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
t.stop();
}
}
});
t.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestAnimatedGif().initUI();
}
});
}
}

Categories

Resources