We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How To Drag An Image
Page Index Toggle Pages: 1
How To Drag An Image? (Read 533 times)
How To Drag An Image?
Jan 15th, 2008, 9:28pm
 
Hey,

I found an example of coding on this website that lets you drag a white box around a screen with the mouse.

Can someone please tell me the coding needed for dragging an image from my data folder around the screen while the mouse is pressed on it ?

Your time is much appreciated.

Many Thanks,
Steve.
Re: How To Drag An Image?
Reply #1 - Jan 16th, 2008, 1:38am
 
// i'll do this for one image, so you see the concept
// hope it works.. just off the top of my head
//
// first, you need a stored imageposition
int imgX, imgY;
// and an image
PImage img;

// mouseposition offset to image
int mX, mY;

// regular old setup
void setup()
{
 size(800,600);
 img = loadImage("img.jpg");
 centerImage();
}

// draw method
void draw()
{
 background(0);
 if(mousePressed){ // is the mousebutton being held?
   imgX = mouseX-mX;
   imgY = mouseY-mY;
 }
 image(img,imgX,imgY);
}

// mousepressed
void mousePressed()
{
 // set variables for holding mouseposition offset
 // to the image
 mX = mouseX-imgX;
 mY = mouseY-imgY;
}

// any key pressed, will re-center the image
void keyPressed()
{
centerImage();
}

// center image function
void centerImage()
{
 imgX = (width-img.width)/2;
 imgY = (height-img.height)/2;
}
Re: How To Drag An Image?
Reply #2 - Feb 1st, 2008, 4:29pm
 
how do I detect if the mouse if on an image which is rotated?
Page Index Toggle Pages: 1