Hello,
I am trying to use the PrintWriter function to export a list of points (printIn values) which are created from a color tracking sketch, but every time the .txt file is blank. How can I get the .txt to list the points?
Any help would be appreciated.
Thanks.
import processing.video.*;
Capture cam;
PGraphics img;
PrintWriter output;
color selected = color(0,255,0); // selected color
void setup()
{
size(640,480);
smooth();
cam = new Capture(this,640,480);
img = createGraphics(640,480,JAVA2D);
output = createWriter("positions.txt");
}
void draw()
{
if (cam.available()) {
cam.read(); cam.loadPixels();
// pick a different selected color, if you want
if(mousePressed) selected = cam.pixels[mouseX+mouseY*cam.width];
// search for the pixel closer to the selected color
int sx = 0, sy = 0; // selected position
float minDist = 1000; // put a huge number so we do not have to care
for(int y = 0; y < cam.height; y ++) {
for(int x = 0; x < cam.width; x ++) {
color c = cam.pixels[x+y*cam.width];
float d = dist(red(c),green(c),blue(c),
red(selected),green(selected),blue(selected));
if(d < minDist) { minDist = d; sx = x; sy = y; }
}
}
// update the painted image
img.beginDraw();
img.smooth();
img.fill(selected,100); img.noStroke();
img.ellipse(sx,sy,25,25);
img.endDraw();
// draw the cam image and on top of that draw the painted image
image(cam,0,0);
image(img,0,0);
noFill(); stroke(200,255,200); strokeWeight(5);
ellipse(sx,sy,50,50);
println("sx = "+sx+", sy = "+sy);
output.println("sx = "+sx+", sy = "+sy); // Write the coordinate to the file
}
}
void keyPressed() {
// clear
if(key == ' ') img = createGraphics(cam.width,cam.height,JAVA2D);
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
I am trying to use the PrintWriter function to export a list of points (printIn values) which are created from a color tracking sketch, but every time the .txt file is blank. How can I get the .txt to list the points?
Any help would be appreciated.
Thanks.
import processing.video.*;
Capture cam;
PGraphics img;
PrintWriter output;
color selected = color(0,255,0); // selected color
void setup()
{
size(640,480);
smooth();
cam = new Capture(this,640,480);
img = createGraphics(640,480,JAVA2D);
output = createWriter("positions.txt");
}
void draw()
{
if (cam.available()) {
cam.read(); cam.loadPixels();
// pick a different selected color, if you want
if(mousePressed) selected = cam.pixels[mouseX+mouseY*cam.width];
// search for the pixel closer to the selected color
int sx = 0, sy = 0; // selected position
float minDist = 1000; // put a huge number so we do not have to care
for(int y = 0; y < cam.height; y ++) {
for(int x = 0; x < cam.width; x ++) {
color c = cam.pixels[x+y*cam.width];
float d = dist(red(c),green(c),blue(c),
red(selected),green(selected),blue(selected));
if(d < minDist) { minDist = d; sx = x; sy = y; }
}
}
// update the painted image
img.beginDraw();
img.smooth();
img.fill(selected,100); img.noStroke();
img.ellipse(sx,sy,25,25);
img.endDraw();
// draw the cam image and on top of that draw the painted image
image(cam,0,0);
image(img,0,0);
noFill(); stroke(200,255,200); strokeWeight(5);
ellipse(sx,sy,50,50);
println("sx = "+sx+", sy = "+sy);
output.println("sx = "+sx+", sy = "+sy); // Write the coordinate to the file
}
}
void keyPressed() {
// clear
if(key == ' ') img = createGraphics(cam.width,cam.height,JAVA2D);
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
2