PostRobot Class in Java

Posts 115 of 15 · Page 1 of 1
Robot Class in Java
Hey what's up guys. I'm pretty new here and new to programming in general. Unluckily for me I decided to learn java first but I'm trying to make the best of it. So today I'm going to be teaching you how to make a simple bot(I think thats what this is called). You should be familiar with basic Java for this. TheNewBoston on youtube has some great tutorials on a whole bunch of languages/subjects.

First thing first, we need to import the Robot class.
Code:
import java.awt****bot;
Its pretty sweet it can type, move your mouse, and click.. to name a few things. Check out the API for the full list of what it can do. just google robot class java.

Continuing along
Code:
import java.awt****bot;

public class mpghTutorial {
  public static void main(String[] args) {
     // Next we have to instantiate it
     Robot bot = new Robot();
  }
}
If you type that into an IDE like eclipse, you're going to see an ominous looking error message. We need to surround it with try/catch statements...
What that basically means is, we're going to try and do something (in this case make a robot and have it do something) and if it can't accomplish this than we handle that exception.

Code:
import java.awt****bot;

public class mpghTutorial {
  public static void main(String[] args) {
     
      try{
      Robot bot = new Robot();
      bot.mouseMove(100, 100);
     } catch (AWTException e) {
        System.out.println("The robot can't process the command."); // or you can just have e.printStackTrace(); like all the cool kids
    }

  }
}
"mouseMove" is a method contained in the Robot class and it takes x and y coordinates for its arguments..

So now if you run this you're probably thinking.. wow this is pretty useless.. it just moves my mouse to the corner.. well yes. But you can put it in a loop and have it keep moving around and clicking...

Code:
import java.awt****bot;
import java.awt.event.InputEvent;

public class mpghTutorial {
  public static void main(String[] args) {
     
      try{
      Robot bot = new Robot();
      bot.mouseMove(100, 100);
      bot.mousePress(InputEvent.BUTTON1_MASK);
      bot.mouseRelease(InputEvent.BUTTON1_MASK);
     } catch (AWTException e) {
        System.out.println("The robot can't process the command."); // 
    }

  }
}
Woah, did you run that? Now we're getting somewhere. Let me add that BUTTON1 = left click, BUTTON2 = middle button(wheel), and BUTTON3 =
right click.
We're calling the mousePress method to click down, if we stopped there then mr bot would keep holding the button down so we call the mouseRelease method... to uh, release it. Oh you're so smart.

If you get creative and use loops you can make some pretty interesting stuff. I made a program that went through a website and sent friend requests to 7000 + people...

you: what how do I get it to click where I want it to click... Am i supposed to just guess the coordinates?!?

Sigh, just because I'm a nice guy I'll show you how I did it... again, I've only been programming for 1-2 months and there may be more efficient ways to do things... nonetheless here's how I did it.

Theres a class called MouseInfo and its part of the abstract windowing toolkit(awt)... and it can get your mouse location.

Code:
import java.awt.MouseInfo;

public class mpghTutorialBonus {
  public static void main(String[] args) {

     while(true)
         System.out.println(MouseInfo.getPointerInfo().getLocation());
  }
}
okay so this will keep running until you terminate the program. So what i did was resize eclipse so it was out of the way but still visible and held my mouse over the location that I wanted to click... i looked over at the console and wrote down the coordinates.

I hope that someone finds this useful and if you have any questions dont hesitate to ask..

Peace!

edit. What the, why is there asterisks?? it's supposed to be
import java [dot] awt [dot] Robot [semicolon]
Quote Originally Posted by Cosmo_ View Post
Unluckily for me I decided to learn java first
.___.

Then why not pick up a book for another language? Not like Oracle makes you swear upon your life, that you will never betray Java.

Also Java is a pretty neat programming language, as you have portability and also it's purely OOP which more or less simplifies the whole programming concept :\
I'm not sure how that's relevant to the tutorial but I shall respond anyways... I am learning C on the side but I'm taking a class on Java. I'm not saying it's terrible or anything, it's just not what I'm looking for in a language. Like most people here, I would like to hack games. I'm pretty sure it could be done in Java but tutorials and books on the subject aren't as readily available as they are with c/c++.
Interesting, I'll see if I can utilize this in my next project.
thank you for the share/info.
Thanks a lot for sharing!
Quote Originally Posted by Cosmo_ View Post
Hey what's up guys. I'm pretty new here and new to programming in general. Unluckily for me I decided to learn java first but I'm trying to make the best of it. So today I'm going to be teaching you how to make a simple bot(I think thats what this is called). You should be familiar with basic Java for this. TheNewBoston on youtube has some great tutorials on a whole bunch of languages/subjects.

First thing first, we need to import the Robot class.
Code:
import java.awt****bot;
Its pretty sweet it can type, move your mouse, and click.. to name a few things. Check out the API for the full list of what it can do. just google robot class java.

Continuing along
Code:
import java.awt****bot;

public class mpghTutorial {
  public static void main(String[] args) {
     // Next we have to instantiate it
     Robot bot = new Robot();
  }
}
If you type that into an IDE like eclipse, you're going to see an ominous looking error message. We need to surround it with try/catch statements...
What that basically means is, we're going to try and do something (in this case make a robot and have it do something) and if it can't accomplish this than we handle that exception.

Code:
import java.awt****bot;

public class mpghTutorial {
  public static void main(String[] args) {
     
      try{
      Robot bot = new Robot();
      bot.mouseMove(100, 100);
     } catch (AWTException e) {
        System.out.println("The robot can't process the command."); // or you can just have e.printStackTrace(); like all the cool kids
    }

  }
}
"mouseMove" is a method contained in the Robot class and it takes x and y coordinates for its arguments..

So now if you run this you're probably thinking.. wow this is pretty useless.. it just moves my mouse to the corner.. well yes. But you can put it in a loop and have it keep moving around and clicking...

Code:
import java.awt****bot;
import java.awt.event.InputEvent;

public class mpghTutorial {
  public static void main(String[] args) {
     
      try{
      Robot bot = new Robot();
      bot.mouseMove(100, 100);
      bot.mousePress(InputEvent.BUTTON1_MASK);
      bot.mouseRelease(InputEvent.BUTTON1_MASK);
     } catch (AWTException e) {
        System.out.println("The robot can't process the command."); // 
    }

  }
}
Woah, did you run that? Now we're getting somewhere. Let me add that BUTTON1 = left click, BUTTON2 = middle button(wheel), and BUTTON3 =
right click.
We're calling the mousePress method to click down, if we stopped there then mr bot would keep holding the button down so we call the mouseRelease method... to uh, release it. Oh you're so smart.

If you get creative and use loops you can make some pretty interesting stuff. I made a program that went through a website and sent friend requests to 7000 + people...

you: what how do I get it to click where I want it to click... Am i supposed to just guess the coordinates?!?

Sigh, just because I'm a nice guy I'll show you how I did it... again, I've only been programming for 1-2 months and there may be more efficient ways to do things... nonetheless here's how I did it.

Theres a class called MouseInfo and its part of the abstract windowing toolkit(awt)... and it can get your mouse location.

Code:
import java.awt.MouseInfo;

public class mpghTutorialBonus {
  public static void main(String[] args) {

     while(true)
         System.out.println(MouseInfo.getPointerInfo().getLocation());
  }
}
okay so this will keep running until you terminate the program. So what i did was resize eclipse so it was out of the way but still visible and held my mouse over the location that I wanted to click... i looked over at the console and wrote down the coordinates.

I hope that someone finds this useful and if you have any questions dont hesitate to ask..

Peace!

edit. What the, why is there asterisks?? it's supposed to be
import java [dot] awt [dot] Robot [semicolon]
Interesting, Will have to save this. Right now i'm downloading java. I'm sure this will be useful for future projects.
Cosmo_ if you are new in programmation starting with awt it's a bad idea, The awt Package serves for graphics(in this tutorial awt ,It is not used for nothing) and in genereal for manage event.
Anyway before to speak of awt, i think you must introduce this package and the standards class and standard Event class.
For example you use InputEvent Class , especially you use a static member and you non-specific that it is the superclass(abstract class however) of inputs and much less of its manager.
Quote Originally Posted by TBH1 View Post
Cosmo_ if you are new in programmation starting with awt it's a bad idea, The awt Package serves for graphics(in this tutorial awt ,It is not used for nothing) and in genereal for manage event.
Anyway before to speak of awt, i think you must introduce this package and the standards class and standard Event class.
For example you use InputEvent Class , especially you use a static member and you non-specific that it is the superclass(abstract class however) of inputs and much less of its manager.
I'm sorry I don't really understand what you're saying.
As far as I know awt is the abstract windowing toolkit. You don't have to create graphics to use it though. This was meant to be a basic tutorial on how someone can create a simple bot. You can read about the super classes and all that good stuff in the official documentation.
Quote Originally Posted by Cosmo_ View Post
I'm sorry I don't really understand what you're saying.
As far as I know awt is the abstract windowing toolkit. You don't have to create graphics to use it though. This was meant to be a basic tutorial on how someone can create a simple bot. You can read about the super classes and all that good stuff in the official documentation.
you can't speak directly the class Robot , but introducing first all class Event (like InputEvent) and after say i use only static member for example for create bot .
Quote Originally Posted by TBH1 View Post
you can't speak directly the class Robot , but introducing first all class Event (like InputEvent) and after say i use only static member for example for create bot .
You're right. I forgot to talk about input event. Sorry about that.
I think what he is saying is that generally using awt classes for your graphical programs is bad practice.
The graphics using awt is generated by the native GUI code itself in the operating system, while Swing code is generated by the JVM itself. Using awt might produces weird results on different platforms.
Though it doesn't really matter in this case since Robot class is no GUI based.
Posts 115 of 15 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?