Hello,
is there a recommended way of creating a sequence of pngs from a sketch as quickly as possible? I just need to save the frames without displaying them.
Would setting the frame rate to something high (999999), using an offscreen PGraphics object and calling save on it at the end of each draw loop be the best way?
Related to this, I see that frameRate's documentation states that "If the processor is not fast enough to maintain the specified rate, it will not be achieved"
In which case, would my approach drop frames? How could I ensure every frame was rendered and saved as quickly as possible?
Thanks.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import processing.core.PApplet;
import processing.core.PGraphics;
public class Rendering extends PApplet {
PGraphics buffer;
float x, y, s = 30;
public void setup() {
size(800, 600);
buffer = createGraphics(800, 600, JAVA2D);
}
public void draw() {
buffer.beginDraw();
buffer.smooth();
buffer.background(0);
buffer.ellipse(x, y, s, s);
buffer.endDraw();
x += 0.1;
y += 0.1;
// show
image(buffer, 0, 0);
}
public void mousePressed() {
buffer.loadPixels();
writeData("out-" + frameCount + ".dat", buffer.pixels);
}
public void writeData(String filename, int[] data) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
for (int i = 0; i < data.length; i++)
writer.write(data[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String _args[]) {
PApplet.main(new String[] { rendering.Rendering.class.getName() });
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import processing.core.PApplet;
import processing.core.PImage;
public class RenderWorker extends PApplet {
public void setup() {
size(800, 600);
colorMode(RGB);
}
public void draw() {
}
public void mousePressed() {
int[] p = readData("out-262.dat");
PImage img = createImage(800, 600, RGB);
System.arraycopy(p, 0, img.pixels, 0, p.length);
img.updatePixels();
image(img, 0, 0);
}
public int[] readData(String filename) {
File file = new File(filename);
int[] data = new int[(int) file.length()];
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
for (int i = 0; i < data.length; i++)
data[i] = reader.read();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
public static void main(String _args[]) {
PApplet.main(new String[] { rendering.RenderWorker.class.getName() });
}
}
I'm going to check again that the app's built for Java 5 (I used Eclipse), then I'm thinking of providing them with a jar and asking them to run it in Terminal with "java -jar app.jar"java version "1.5.0_20"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_20-b02-315)
Java HotSpot(TM) Client VM (build 1.5.0_20-141, mixed mode)
import processing.core.PApplet;
public class RenderingDemo extends PApplet {
float x, y, dx = 0.1f;
float scale = 1;
public void setup() {
size(1200, 600); // , OPENGL); // try P2D and OPENGL to see the issues
smooth();
noStroke();
textFont(createFont("verdana", 12));
textMode(SHAPE);
ellipseMode(CENTER);
}
public void draw() {
background(200);
float tx = width / 2;
float ty = height / 2;
translate(tx, ty);
scale(scale);
fill(127);
ellipse(x, y, 50, 50);
fill(0);
text("text", x, y);
x += dx;
if (x > 100)
dx = -0.1f;
else if (x < -100)
dx = 0.1f;
println(x);
}
public void keyPressed() {
if (key == 'q')
scale += 0.1;
if (key == 'w')
scale -= 0.1;
}
}