Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
Export buttons and sliders template/example.
Requires the ControlP5 library http://www.sojamo.de/libraries/controlP5/
*/
import processing.pdf.*;
import java.util.Date;
import controlP5.*;
ControlP5 cp5;
Date date;
boolean recording = false;
int numRows = 2;
int numCols = 3;
void setup() {
size(700, 600);
cp5 = new ControlP5(this); //create a ControlP5 object to add sliders and buttons to
date = new Date();
pixelDensity(displayDensity()); //for retina/high res display
setupControls();
}
void draw() {
//start recording a PDF
if (recording) {
//add the date to the filename so as not to overwrite a previous design
beginRecord(PDF, "dot grid " + date.getTime()/1000 + ".pdf");
}
background(255);
drawDotGrid(numRows, numCols);
if (recording) {
recording = false;
endRecord();
}
}
void exportPDFButton() {
recording = true;
}
void exportPNGButton() {
saveFrame("dot grid " + date.getTime()/1000 + ".png");
}
//draw your own cool parametric / generative thing here!
void drawDotGrid(int numRows, int numCols) {
fill(0);
pushMatrix();
for (int row=0; row<numRows; row++) {
pushMatrix();
for (int col=0; col<numCols; col++) {
ellipse(50, 50, 10, 10);
translate(20, 0);
}
popMatrix();
translate(0, 20);
}
popMatrix();
}
void rowSlider(int val) { //associated with the slider of the same name
numRows = val;
}
void columnSlider(int val) { //associated with the slider of the same name
numCols = val;
}
//add the ControlP5 GUI elements. This only needs to happen once,
//so don't call from draw().
void setupControls() {
cp5.addButton("exportPDFButton")
.setBroadcast(false)
.setPosition(550, height-95)
.setSize(130, 50)
.setLabel("Export PDF")
.setColorBackground(color(100, 0, 255))
.setColorActive(color(120, 0, 255))
.setColorForeground(color(100, 0, 180))
.setColorCaptionLabel(color(255, 255, 60))
.setBroadcast(true)
.getCaptionLabel().setFont(createFont("Rockwell", 8)).toUpperCase(false).align(CENTER, CENTER)
;
cp5.addButton("exportPNGButton")
.setBroadcast(false)
.setPosition(400, height-95)
.setSize(130, 50)
.setLabel("Export PNG")
.setColorBackground(color(100, 0, 255))
.setColorActive(color(120, 0, 255))
.setColorForeground(color(100, 0, 180))
.setColorCaptionLabel(color(255, 255, 60))
.setBroadcast(true)
.getCaptionLabel().setFont(createFont("Rockwell", 8)).toUpperCase(false).align(CENTER, CENTER)
;
cp5.addSlider("rowSlider")
.setBroadcast(false)
.setPosition(30, height-150)
.setSize(300, 30)
.setRange(0, 20)
.setValue(numRows)
.setLabel("Rows")
.setSliderMode(Slider.FLEXIBLE)
.setBroadcast(true)
.getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0).setFont(createFont("Arial", 12)).toUpperCase(false).setColor(0)
;
cp5.addSlider("columnSlider")
.setBroadcast(false)
.setPosition(30, height-75)
.setSize(300, 30)
.setRange(0, 20)
.setValue(numCols)
.setLabel("Columns")
.setSliderMode(Slider.FLEXIBLE)
.setBroadcast(true)
.getCaptionLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0).setFont(createFont("Arial", 12)).toUpperCase(false).setColor(0)
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.