Question

So, I'm working on a processing project that lets me use an image as a cursor but I've been having problems with the cursor image because it has been constantly blinking. I read that if the cursor image is too big it has a tendency to constantly blink. However, I was wondering if there was any way that I could keep the size of my image, while still maintaining it as a cursor. OR,

I was wondering if there was a code that lets me press the image and drag it around the screen. :/

Here's the code that I've been using.

// Declaring a variable of type PImage
PImage img;  
PImage img2;

void setup() {
  size(815,514);
  // Make a new instance of a PImage by loading an image file
  img = loadImage("preamble.jpg");
  img2 = loadImage("blackgun.png");
}

void draw() {
  background(0);
  // Draw the image to the screen at coordinate (0,0)
  image(img,0,0);
  //using the image as the cursor
if (mouseX < 50) {
    cursor(img2);
  } else {
    cursor(img2);
  }
}
Was it helpful?

Solution

In the reference they say: "it is recommended to make the size 16x16 or 32x32 pixels" about the image to be used as cursor. You can do this by calling resize:

img2 = loadImage("blackgun.png");
img2.resize(32,32);

Also there is no point in the lines:

if (mouseX < 50) {
    cursor(img2);
  } else {
    cursor(img2);
  }

As either way you end up with the same img2 as cursor image.

YOu can just use:

image(img, mouseX, mouseY);

but the cursor will be over the image.

That's a simple and poor drag... I have here an old example of a little better drag and drop, it is using rects() intead of images but the idea is the same and you can easily adapt it to use images:

DragMe[] drags = new DragMe[40];

void setup() {
  size(400, 400);
  for (int i = 0; i < drags.length; i++) {
    drags[i]  = new DragMe();
  }
}

void draw() {
  background(255);
  for (int i = 0; i < drags.length; i++) {
    drags[i].display();
    drags[i].update();
  }
}



void mousePressed() {
  for (int i = 0; i < drags.length; i++) {
    if (!drags[i].isOver())
      drags[i].dontMove = true;
    drags[i].offset_x = mouseX - drags[i].pos_x;
    drags[i].offset_y = mouseY - drags[i].pos_y;
  }
}


void mouseReleased() {
  for (int i = 0; i < drags.length; i++) {
    drags[i].locked = false;
    drags[i].dontMove = false;
  }
}






class DragMe {
  float pos_x, pos_y, SIZE = 20;
  float prev_x, prev_y;
  boolean locked;
  boolean dontMove;
  color c = color (0, 170, 170);
  float offset_x, offset_y;

  DragMe() {
    pos_x = random(width-SIZE);
    pos_y = random(height-SIZE);
  }


  void update() {
    if (isOver() && !locked && !dontMove || locked && !dontMove )
      c = color (170);
    else
      c = color (0, 170, 170);

    if (isClicked()) {
      locked = true;
    }
    if (locked && !dontMove) {

      pos_x =  mouseX - offset_x;
      pos_y =  mouseY - offset_y;
    }
  }



  void display() {
    fill(c);
    rect(pos_x, pos_y, SIZE, SIZE);
  }

  boolean isOver() {
    float right_x = pos_x + SIZE;
    float bottom_y = pos_y + SIZE;
    return mouseX >= pos_x && mouseX <= right_x &&
      mouseY >= pos_y && mouseY <= bottom_y;
  }

  boolean isClicked() {
    return isOver() && mousePressed && !dontMove;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top