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 & HelpPrograms › Creating a line
Page Index Toggle Pages: 1
Creating a line (Read 376 times)
Creating a line
Feb 24th, 2009, 1:27pm
 
Hi, I am just starting out with processing so I am starting from scratch.  

I know this is very simple but I just need to be able to draw a line when the mouse is pressed. I have read the mousePressed, line and understand initial setup.  If I click once I get my "setPoint" click again this setsEndPoint and then should draw the line.  I can then build on this creating my own rectangle or shape.

Any tips on where to look and start would be great.

Thank you
Re: Creating a line
Reply #1 - Feb 24th, 2009, 6:50pm
 
Quote:
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
boolean started = false;

void setup()
{
  size(400,400);
  background(0);
  stroke(255);
  noLoop();
}

void draw()
{
  if(started) // can't draw a line until we've started
  {
    line(x1, y1, x2, y2);
  }
}

void mousePressed()
{
  if(started)    // if we're going,
  {
    x1 = x2;     // then shift the previous '2' coordinates
    y1 = y2;     // down to the '1' variables,
    x2 = mouseX; // and get new '2' coordinates.
    y2 = mouseY;
    redraw();
  }
  else
  {
    started = true;
    x2 = mouseX;
    y2 = mouseY;
  }
}

Re: Creating a line
Reply #2 - Feb 24th, 2009, 7:55pm
 
Thank you its given me something to start from. Appreciated.

Thank you

J
Re: Creating a line
Reply #3 - Feb 25th, 2009, 1:15am
 
This has got me started. However, this is great for successive lines however I need to be able to start to create a shape out of lines.

When the mouse is clicked once the 1st point is created (this is required when a key is pressed later going back to the origin point).

When I click the mouse again it has by 2nd pair therefore I can create my one line and do this as many times as I want.  When I hit any key it will go by to my 1st point / orgin)

Any help would be great - its a bit tuff when you are studying on your own.

Page Index Toggle Pages: 1