i'm completely new to the programming world and i'm very frustrated at my inability to write code in java and processing......yet i do not lose hill though the uphill battle makes me bananas.....whatever....i need to write a processing program whereby i use mouse control to draw a point on the drawing window when the mouse button is pressed.....so far so good i did manage that bit....however i am then bugged down by the idea of extending this program to place a succession of points each connected to the next by a straight line segment so that when a key on the keyboard is pressed the sketch is terminated by drawing a line from the last point to the first. i though of using arrays and a for loop but as yet havent come to solving this and writing the code.......and then i need to store the coordinate pairs in a file......
everybody tells me to practise....and rightly so..but is there anyone who can help me at least indicating useful tutotial sites abt processing and java please...
many thnks
regards
gametheory
Hors ligne
Hi Gametheory,
You need a two-dimensional array to keep point coordinates, then for each mouse click add mouseX and mouseY of the click (using mousePressed) as a new point coordinates. In the draw() part of your sketch, redraw this array each frame with point() and line().
I could give you code but you want to learn by yourself, don't you ?
If you get stuck or lose patience, just ask...
Hors ligne
thanks a million i will work it out from there
i appreciate
regards
Hors ligne
further to my problem whereby i had to plot points on d screen and join them by a straight line segment which stops once a key is pressed that is d drawing ends this is what i managed to come up with:
boolean drawVertex = false;
void setup(){
size(450,450);
background(25,88,126);
strokeWeight(30);
int [] x = {50, 67, 89, 124, 345};
int [] y = { 98, 56,45,125, 56};
beginShape();
for(int i = 0; i < x.length; i++){
point (x[i], y[i]);
}
beginShape();
for(int i = 0; i < x.length; i++){
vertex (x[i], y[i]);
}
void keyPressed() {
if ((key == 'A')){
endShape(CLOSE) = true;
}
}
}
whereby im missing the stage whereby the vertex actaully passes from one point to another..........among the errors is dat d compiler points out that void keyPressed is an unexpected token.....
thnks
ps i just tried this too...............
boolean drawVertex = false;
void setup(){
size(500,500);
strokeWeight (18);
}
void draw(){
background(45,200,88);
if (drawVertex == true) {
beginShape ();
point (25,450);
point (45,156);
endShape();
}
}
void keyPressed() {
if ((key == 'A')) {
drawVertex = true;
}
}
void keyReleased(){
drawVertex = false;
}
but i need to see the line being plotted.........maybe frameRate() ??
thnks
Dernière modification par gametheory (2009-02-10 19:40:45)
Hors ligne
Your first attempt would work with some minor changes : add draw(), and put keyPressed() out from setup() and draw() as shown below, pressing caps A closes the shape.
boolean drawVertex = false; int [] x = {50, 67, 89, 124, 345}; int [] y = { 98, 56,45,125, 56}; void setup(){ size(450,450); background(25,88,126); strokeWeight(30); } void draw() { beginShape(); for(int i = 0; i < x.length; i++){ point (x[i], y[i]); vertex (x[i], y[i]); } } void keyPressed() { if ((key == 'A')){ endShape(CLOSE); } }
Hors ligne
thanks a lot but i need to see dpoints plotting from one point to another as in slow motion......maybe i should use threading???
thanks again
Hors ligne
hi itsme again guys.....i have manged to write a processing program whereby i load a text file called positions and plot the corresponding points. However in order to draw these points and calling the draw method, the program doesn't respond....this is my code so far....any help gretaly appreciated
thanks
PrintWriter output;
void setup()
{
size(1024,768);
strokeWeight(7);
output = createWriter("positions.txt");
}
void draw()
{
if (mousePressed)
{
output.println(mouseX + "\t" + mouseY + "\n");
point(20,120);
point(5,180);
point(16,200);
point(100,60);
point(120,40);
point(70,20);
}
if (keyPressed)
{output.flush();
output.close();
}
}
that is as regards the plotting of points.....if i need to use draw these points should i use a for loop in the draw method?
plus how will draw identify these points? shouldi store them in an array 2 dimensional array first?
thnks
Hors ligne
Hi Gametheory,
citation :
if i need to use draw these points should i use a for loop in the draw method?
plus how will draw identify these points? shouldi store them in an array 2 dimensional array first?
Yes, read the file contents into an array, then from the draw method, call a function to draw your array from point 0 to point X, point X should be 1 at the first step, 2 at next step, etc. You could use millis() to define this step.
Here are some bits of code that could be useful :
int maxpoints = 1000; int nb = 0; float points[][] = new float[maxpoints][2]; void setup() { size(400, 400); } void draw() { background(255,255,255); stroke(0); frameRate(25); drawPoints(); } void mousePressed() { nb ++; points[nb][0] = mouseX; points[nb][1] = mouseY; } void drawPoints() { if (nb > 0) { for (int i = 0; i <= nb; i++) { point(points[i][0], points[i][1]); } if (nb > 1) { for (int i = 2; i <= nb; i++) { line(points[i-1][0], points[i-1][1], points[i][0], points[i][1]); if (i == nb) { line(points[1][0], points[1][1], points[i][0], points[i][1]); } } } } }
Hors ligne
many thanks....i will look in it
regards
Hors ligne
just tried it out.....many thanks....!!! i appreciate
regards
Hors ligne
its me again.....i must design a 3d teapot....and i would like to design curves....would it be useful to use curveVertex or curveBezier??
thnks
Hors ligne
if i use vertex for a 3d structure must i use three parameters one of which would be negative??
thnks
Hors ligne
gametheory a écrit:
its me again.....i must design a 3d teapot....and i would like to design curves....would it be useful to use curveVertex or curveBezier??
bezierVertex will give you more control over the curves drawn, with control points, but it's harder to use. curveVertex only need points coordinates and the curves will be drawn passing by these points.
gametheory a écrit:
if i use vertex for a 3d structure must i use three parameters one of which would be negative??
You'll need points within a 3d space, with 3 coordinates, one for each axis : x, y ,z . Those coordinates could be positive or negative. You should take a look at processing 3D examples : http://processing.org/learning/3d/
Hors ligne