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 draw with the mouse
Page Index Toggle Pages: 1
how to draw with the mouse? (Read 359 times)
how to draw with the mouse?
Oct 3rd, 2008, 6:14pm
 
I trying to do as something as simple as when the mouse is dragged, draw a line from a specific point to where the mouse is. What should happen is one end of the line will be stationary while the other follows the mouse as long as it's being dragged. The line should go away when the mouse is released. Here's what I have:

void mouseDragged() {
    if (mouseY > 120) {
      color yellow = color(255, 255, 0);
      fill(yellow);
      line(201, 120, mouseX, mouseY);
 }
}

I have the if statement because I don't want it draw past a certain point. This isn't complete, I know, but I can't even get this part to show up when I run the program. What am I doing wrong? Later, how do I implement mouseReleased to stop drawing the line? Thanks for any help.
Re: how to draw with the mouse?
Reply #1 - Oct 3rd, 2008, 9:28pm
 
Maybe this can get you started:
Code:
static final color LINE_COLOR = #FFFF00;

boolean bDragging;
int anchorX, anchorY;
int posX, posY;

void setup()
{
size(500, 500);
}

void draw()
{
background(#008080);
if (bDragging)
{
stroke(LINE_COLOR);
line(anchorX, anchorY, posX, posY);
}
}

void mousePressed()
{
anchorX = posX = mouseX;
anchorY = posY = mouseY;
}

void mouseReleased()
{
bDragging = false;
}

void mouseDragged()
{
bDragging = true;
if (dist(mouseX, mouseY, anchorX, anchorY) < 120)
{
posX = mouseX;
posY = mouseY;
}
}
Re: how to draw with the mouse?
Reply #2 - Oct 5th, 2008, 4:19am
 
Thank you. That helped a lot. Now I'm trying to write code that says if the mouse is dragged, simply draw pixels following the mouse, regardless of the shape. Do I use curve()?
Re: how to draw with the mouse?
Reply #3 - Oct 5th, 2008, 10:48pm
 
Unless you need to smooth the drawing, no.
Look at the ContinuousLines example in Topics > Drawing category.
Page Index Toggle Pages: 1