|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Java, improving my Mouse class.
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
*cough* Stock *cough*
|
I now have a successful program that just moves the mouse around Humanlike (does not 'teleport' around the screen) depending on what the user input is. No fancy forms or anything yet though, I don't know how to do those yet anyways
![]() "Mouse" class (need the java file below to test this) Code:
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.MouseInfo;
public class Mouse{
int mouseSpeed, cX, cY; //cX = current X location
public Mouse(int mSpeed)
{
mouseSpeed = mSpeed;
cX = 0;
cY = 0;
}
public double hypot(double s1, double s2)
{
return Math.sqrt(Math.pow(s1, 2) + Math.pow(s2, 2));
}
public void windMouse(double xs, double ys, double xe, double ye, double gravity, double wind, double minWait, double maxWait, double maxStep, double targetArea) throws Exception
{
double veloX=0, veloY=0, windX=0, windY=0, veloMag, dist, randomDist, lastDist, step;
int lastX, lastY;
double sqrt2, sqrt3, sqrt5;
Robot r = new Robot();
sqrt2 = Math.sqrt(2);
sqrt3 = Math.sqrt(3);
sqrt5 = Math.sqrt(5);
while(hypot(xs - xe, ys - ye) > 1)
{
dist = hypot(xs - xe, ys - ye);
wind = Math.min(wind, dist);
if(dist > targetArea)
{
windX = windX / sqrt3 + (((int)(Math.random() * Math.round(wind)) * 2 + 1) - wind) / sqrt5;
windY = windY / sqrt3 + (((int)(Math.random() * Math.round(wind)) * 2 + 1) - wind) / sqrt5;
}
else
{
windX = windX / sqrt2;
windY = windY / sqrt2;
if(maxStep < 3)
{
maxStep = (int)(Math.random() * 3) + 3;
}
else
{
maxStep = maxStep / sqrt5;
}
}
veloX = veloX + windX;
veloY = veloY + windY;
veloX = veloX + gravity * (xe - xs) / dist;
veloY = veloY + gravity * (ye - ys) / dist;
if(hypot(veloX, veloY) > maxStep)
{
randomDist = maxStep / 2.0 + (int)(Math.random() * (Math.round(maxStep) / 2));
veloMag = Math.sqrt(veloX * veloX + veloY * veloY);
veloX = (veloX / veloMag) * randomDist;
veloY = (veloY / veloMag) * randomDist;
}
lastX = (int)Math.round(xs);
lastY = (int)Math.round(ys);
xs = (xs + veloX);
ys = (ys + veloY);
if((lastX != Math.round(xs)) || (lastY != Math.round(ys)))
{
r.mouseMove(Math.round((int)xs), Math.round((int)ys));
}
step = hypot(xs - lastX, ys - lastY);
r.delay((int)Math.round((maxWait - minWait) * (step / maxStep) + minWait));
lastDist = dist;
}
if((Math.round(xe) != Math.round(xs)) || (Math.round(ye) != Math.round(ys)))
{
r.mouseMove(Math.round((int)xe), Math.round((int)ye));
}
}
public int posX()
{
PointerInfo pInfo = MouseInfo.getPointerInfo();
Point mouseLocation = new Point(pInfo.getLocation());
return (int)mouseLocation.getX();
}
public int posY()
{
PointerInfo pInfo = MouseInfo.getPointerInfo();
Point mouseLocation = new Point(pInfo.getLocation());
return (int)mouseLocation.getY();
}
public void getPos()
{
cX = posX();
cY = posY();
}
public void move(int x, int y, int rx, int ry) throws Exception
{
int cx, cy;
double randSpeed = ((Math.random() * mouseSpeed) / 2 + mouseSpeed) / 10;
if(randSpeed == 0)
randSpeed = 0.1;
getPos();
x = x + (int)(Math.random() * rx);
y = y + (int)(Math.random() * ry);
windMouse(cX, cY, x, y, 9.0, 3.0, (10.0/randSpeed), (15.0/randSpeed), (10.0*randSpeed), (10.0*randSpeed));
}
public void click(boolean left)throws Exception
{
int counter = 0;
Robot r = new Robot();
if(left)
{
r.mousePress(InputEvent.BUTTON1_MASK);
do
{
r.delay((int)(Math.random() * 10) + 5);
counter ++;
}
while(counter < 4);
r.mouseRelease(InputEvent.BUTTON1_MASK);
}else{
r.mousePress(InputEvent.BUTTON3_MASK);
do
{
r.delay((int)(Math.random() * 10) + 50);
counter ++;
}
while(counter < 4);
r.mouseRelease(InputEvent.BUTTON3_MASK);
}
}
}
Code:
/**
* Write a description of class Tester here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Tester
{
public static void main(String[] args) throws Exception
{
Mouse mouse = new Mouse(15);
for(int x = 0; x <= 30; x ++)
{
mouse.move(0, 0, 500, 600);
}
}
}
I just want to know, is there a simple way for me to move the mouse in the background? and make it to where I can still use OCN while java paints me a beautiful picture on MS paint? And if it is not simple, but there is a way that you know of. I am willing to learn! Thank you
__________________
"_Trev"
|
|||||||||||||
|
|
|
|
|
#2 (permalink) | ||||||||||||
|
Say what?
![]() |
By moving in the background, do you mean having it draw in the background while OCN is open full screen? I don't know much about java, but maybe you can make it so that half you screen is a browser window and the other half is paint. Whenever the script detects mouse movement (you moving the mouse to click on something) it will pause until the mouse stops moving for 2 seconds. It will then resume the drawing - moving the mouse back to mspaint and continuing on.
How to do that? I have no idea Good luck!
__________________
|
||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|