We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, this is my first post and I am new to programming.
In order to learn processing I am trying to make a drawing program that shows a randomly generated line and curve once a second, then when the user clicks on the screen the line and curve that are on the screen at that time stay on the screen, as in they are redrawn every frame, whilst the program continues to make a single random line and curve every second.
Essentially the program will "propose" a line and curve every second and the user will either accept by clicking or reject by ignoring, thus resulting in a collaborative drawing between the program and user.
This is my code so far:
float x1;
float x2;
float y1;
float y2;
float cx1;
float cx2;
float cx3;
float cx4;
float cy1;
float cy2;
float cy3;
float cy4;
int clicks=0;
void setup() {
size(500, 500);
frameRate(1);
}
void draw() {
background(100, 100, 100);
x1 = random(500);
x2 = random(500);
y1 = random(500);
y2 = random(500);
cx1 = random(500);
cx2 = random(500);
cx3 = random(500);
cx4 = random(500);
cy1 = random(500);
cy2 = random(500);
cy3 = random(500);
cy4 = random(500);
line(x1, x2, y1, y2);
curve(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
if (clicks == 1) {
line(x1, x2, y1, y2);
curve(cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4);
}
}
void mousePressed() {
clicks = clicks+1;
}
It runs, however the variables which define the location of the line and curve when clicks == 1 change every second when redrawn, how do I store them so that when the user clicks in the window the location of the line and the curve at that time is redrawn until the program stops?
Thanks in advance.
Answers
Well, simply stop picking random() coordinates once user clicks: *-:)
A more simplified version w/o
registerMethod("post", this);
nor forced delay(). \m/However, undocumented dequeueEvents() is still needed before checking for clicked: :-<
Please ignore GoToLoop's replies... I don't think he's grasped exactly what it is you're asking, and his replies aren't exactly helpful to you.
There's a lot of changes here. I've moved you to an object-based model. Classes are blueprints for objects. i've also included two ArrayLists, which are like arrays, except you can dynamically add things to them. I do this when the user clicks. I also draw all the ones that are in the ArrayLists. Finally, I've done away with your framerate call, and moved you to a time-base updating system, so that the new prospective line and curve are updated each second.
Ah, thank you both!
Any replies are helpful to me learning, sometimes it is very hard to put these things into words so I apologize to GoToLoop if I was not clear about my intentions, I'll still go over all three replies in detail to learn what I can so that GoToLoops contributions don't go to waste.
TfGuy44 your code does exactly what I wanted! now I'll open it and my original sketch and spend time modifying my original myself (or just rewrite it from scratch) so that I learn exactly what you did.
Looks like I'll be reading a lot about objects and classes as well.
Thanks again, long live processing!
Common Questions: From several variables to arrays