 Simulate Mouse movement with Robot

Hello !

I’m attempting to simulate the movement of my mouse with Robot but it does not work : my mouse just jump between the points but the particles don’t follow it.
Can you explain me what is wrong with my code ?

import java.awt.AWTException;
import java.awt.Robot;
Robot robot;

int x;
int y;
float thold = 5;
float spifac = 1.05;
int outnum;
float drag = 0.01;
int big = 500;
ball bodies[] = new ball[big];
float mX;
float mY;
int[] mousX={200,100,900,230};
int[] mousY={200,100,450,300};

void setup() {
size(900, 450);
strokeWeight(1);
fill(255, 255, 255);
stroke(255, 255, 255, 5);
background(0, 0, 0);
smooth();
for(int i = 0; i < big; i++) {
bodies[i] = new ball();
}
try
{
robot = new Robot();
robot.setAutoDelay(40);
}
catch (AWTException e)
{
println(“Robot class not supported by your system!”);
exit();
}
}

void draw() {
if(keyPressed) {
saveFrame("Focus " + outnum);
outnum++;
}
if(mousePressed) {
background(0, 0, 0);

mX += 0.3 * (mouseX - mX);
mY += 0.3 * (mouseY - mY);

}
for (int k=0; k<2;k = k+1) {
x=mousX[k];
y=mousY[k];
robot.delay(1000);
robot.mouseMove(x, y);
mX += 0.3 * (mouseX - mX);
mY += 0.3 * (mouseY - mY);
for(int i = 0; i < big; i++) {
bodies[i].render();
}
}
}

class ball {
float X;
float Y;
float Xv;
float Yv;
float pX;
float pY;
float w;
ball() {
X = random(width);
Y = random(height);
w = random(1 / thold, thold);
}
void render() {
if(!mousePressed) {
Xv /= spifac;
Yv /= spifac;
}
Xv += drag * (mX - X) * w;
Yv += drag * (mY - Y) * w;
X += Xv;
Y += Yv;
line(X, Y, pX, pY);
pX = X;
pY = Y;
}
}

1 Like