Why Do I Find It So Hard To Integrate Code From Elsewhere?

blackneos940

Active Member
Joined
May 16, 2017
Messages
347
Reaction score
207
Credits
332
Hey guys. :3 It is I, black! :3 Anywho, it seems that despite all my years of trying to understand Programming, I still have a hard time integrating existing Code into a Program I'm writing... :< Take for example this:
Code:
public static void click(int x, int y) throws AWTException{
    Robot bot = new Robot();
    bot.mouseMove(x, y);   
    bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
    bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}

I have NO idea how to make that work in the context of my Code. :( Specifically, it's Java, which I should know enough of by now to make this work..... :( Take this Code for example, the Java File I'm trying to get to Compile:
Code:
import java.awt.Robot;

public class Click_Mouse

{
  public static void click(int x, int y) throws AWTException

  {
    Robot bot = new Robot();
    bot.mouseMove(x, y);   
    bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
    bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);

  }

  public static void main()

  {
    bot();

  }

}

Man, you'd think after all these years, I'd be able to make this work. :( Thanks for any help, guys. :) Have a good day, ok...? :3
 


I don't have my laptop handy ATM.
What error messages are you getting?

The most obvious error I can see in the code is the call to bot() in main.
The main function doesn't know what bot is.
Bot is just a local variable in the click method.

Did you mean to call the click method in main instead?
E.g. click(200,200);
That would call click, which would create a new local instance of the Robot class called bot and would then move the mouse cursor to 200,200 and perform a mouse-click, comprising of a mouse-button1-down and mouse-button1-up event.

Also, because the click method can throw an exception - you might have to enclose the call to click in a try/catch statement. To allow the program to deal with, and recover from any exceptions that are thrown!
 
I don't have my laptop handy ATM.
What error messages are you getting?

The most obvious error I can see in the code is the call to bot() in main.
The main function doesn't know what bot is.
Bot is just a local variable in the click method.

Did you mean to call the click method in main instead?
E.g. click(200,200);
That would call click, which would create a new local instance of the Robot class called bot and would then move the mouse cursor to 200,200 and perform a mouse-click, comprising of a mouse-button1-down and mouse-button1-up event.

Also, because the click method can throw an exception - you might have to enclose the call to click in a try/catch statement. To allow the program to deal with, and recover from any exceptions that are thrown!
Hey Jas... :3 I'm downloading JDK on Windows, and will try to Compile it there. :) Uno momento... :3
 
I don't have my laptop handy ATM.
What error messages are you getting?

The most obvious error I can see in the code is the call to bot() in main.
The main function doesn't know what bot is.
Bot is just a local variable in the click method.

Did you mean to call the click method in main instead?
E.g. click(200,200);
That would call click, which would create a new local instance of the Robot class called bot and would then move the mouse cursor to 200,200 and perform a mouse-click, comprising of a mouse-button1-down and mouse-button1-up event.

Also, because the click method can throw an exception - you might have to enclose the call to click in a try/catch statement. To allow the program to deal with, and recover from any exceptions that are thrown!
Here's the error javac threw. :3

Code:
Click_Mouse.java:6: error: cannot find symbol
  public static void click(int x, int y) throws AWTException
                                                ^
  symbol:   class AWTException
  location: class Click_Mouse
Click_Mouse.java:11: error: cannot find symbol
    bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                   ^
  symbol:   variable InputEvent
  location: class Click_Mouse
Click_Mouse.java:12: error: cannot find symbol
    bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                     ^
  symbol:   variable InputEvent
  location: class Click_Mouse
Click_Mouse.java:19: error: cannot find symbol
    bot();
    ^
  symbol:   method bot()
  location: class Click_Mouse
4 errors

Sorry I took so long to get back to you. :>
 
Ok, well I was definitely correct about the call to bot() in your main function.

The other errors are because the compiler doesn't know what the AWTException class is. And it also doesn't know what InputEvent is.
So you'll need to import the appropriate libraries for AWTException and InputEvent.
It's been a while since I did any Java. And my laptop isn't handy for me to look up which libraries they are in.

The best place to look would probably be in the code where you got the snippet of code you're using. Grep the code-base you took that snippet from - for import statements. And work out which ones reference AWTException and InputEvent. Then add those to your code.
 
Ok, well I was definitely correct about the call to bot() in your main function.

The other errors are because the compiler doesn't know what the AWTException class is. And it also doesn't know what InputEvent is.
So you'll need to import the appropriate libraries for AWTException and InputEvent.
It's been a while since I did any Java. And my laptop isn't handy for me to look up which libraries they are in.

The best place to look would probably be in the code where you got the snippet of code you're using. Grep the code-base you took that snippet from - for import statements. And work out which ones reference AWTException and InputEvent. Then add those to your code.
Well, sounds like something that's over my head..... So I tried doing what you said, but I'm still getting the same errors... Am I just doomed to suck at this...? I mean, I'm actually putting forth effort. :(
 
Ok, well I was definitely correct about the call to bot() in your main function.

The other errors are because the compiler doesn't know what the AWTException class is. And it also doesn't know what InputEvent is.
So you'll need to import the appropriate libraries for AWTException and InputEvent.
It's been a while since I did any Java. And my laptop isn't handy for me to look up which libraries they are in.

The best place to look would probably be in the code where you got the snippet of code you're using. Grep the code-base you took that snippet from - for import statements. And work out which ones reference AWTException and InputEvent. Then add those to your code.
So it turns out that further down the page, I found a more complete example, with the import Statements! :3 Now it works! :3 Thanks for your help Jas. :> I seriously need to get my head around OOP more. :3
 
@blackneos940 - It takes years of study and practice to master programming - regardless of the language/methodology.

Object Oriented Programming can be a bit tricky at first, but it isn't too difficult once you have grasped the basics. Keep plugging away at it and I'm sure it will eventually click!
 
@blackneos940 - It takes years of study and practice to master programming - regardless of the language/methodology.

Object Oriented Programming can be a bit tricky at first, but it isn't too difficult once you have grasped the basics. Keep plugging away at it and I'm sure it will eventually click!
Yeah, you're right. :3 I think that if I can master it, it will greatly improve my skill set... :3
 

Members online


Latest posts

Top