Paul
Paul

Reputation: 31

Looping a mouse movement in Processing

I am trying to de-pixellate an image over time, which is working. However when this has finished, I want the mouse to go back to its original point and start again, forever. I have tried loop commands with no luck.

I can only get this code to work in version 2.2.1

import java.awt.Robot;
PImage img;
int pixls = 0;
int x, y, yinc;
int XOffset = 0;
int YOffset = 30;
int counter = YOffset;
Robot robot;

void setup() {
  size(900, 900);
  //noCursor();
  noStroke();
  img = loadImage("p.jpg");
  img.resize(900, 900);
  //surface.setLocation(XOffset, YOffset);
}

void draw() {
  loadPixels();
  frameRate(14);
  pixls = (int)map(mouseY, height, 20, 250, 20); //pixellation 
  for (int i = 0; i < pixls; i++) {
    for (int j = 0; j < pixls; j++) {
      float r = red(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
      float g = green(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
      float b = blue(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
      fill(r, g, b);
      rect((width/pixls)*i, (height/pixls)*j, width/pixls, height/pixls);
      yinc = (height/pixls)*j;
    }
} 
  try {
    robot = new Robot();
    robot.mouseMove(XOffset*2, counter); //counter is start of mouse
    if (counter > height + YOffset)   
   {
     counter = YOffset + 30; // +30 for menubar
    }
  }
  catch (Exception e) {
    //println("error = ", e);
  }
  counter++; 
}

Upvotes: 1

Views: 72

Answers (1)

apodidae
apodidae

Reputation: 2713

The following should work in Processing 2.2.1. It uses an old method for finding the XOffset and YOffset of the app's window.

/*
  Will work in Processing 2.2.1
*/

import java.awt.Robot;

Robot robot;

PImage img;
int pixls = 0;
int x, y, yinc;
int XOffset = 0;
int YOffset = 0;
int counter = 0;

void setup() {
  size(710, 710);
 // noCursor();
  noStroke();
  img = loadImage("myImage.jpg");
  img.resize(710, 710);
}

void draw() {
   int XOffset = frame.getLocationOnScreen().x;
   int YOffset = frame.getLocationOnScreen().y;
  pixls = (int)map(mouseY, 0, height, 5, 350);
  for (int i = 0; i < pixls; i++) {
    for (int j = 0; j < pixls; j++) {
      float r = red(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
      float g = green(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
      float b = blue(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
      fill(r, g, b);
      rect((width/pixls)*i, (height/pixls)*j, width/pixls, height/pixls);
      yinc = (height/pixls)*j;
    }
  }
  try {
    robot = new Robot();
    robot.mouseMove(XOffset + 10, counter);
    if (counter > height + YOffset + 30) {
      counter = YOffset + 30; // + 30 for menubar
    }
  } catch (Exception e) {
    println("error = ", e);
  }
  counter++;
}

Upvotes: 1

Related Questions