-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSave.pde
More file actions
95 lines (83 loc) · 2.93 KB
/
Copy pathSave.pde
File metadata and controls
95 lines (83 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Function for saving the generated image into a file when you press "S" or "B" key.
* Files will be saved to /out folder, with name: [Sketch name]_[Timestamp].jpg
*
* It works in two ways:
* 1. S key (BASIC)
* If you press the S key ("Save"), it will save the contents of the screen.
* The image is saved in the same resolution as your sketch size. In other words,
* the image will have the same size as "height" by "width" pixels.
*
* 2. B key (ADVANCED)
If you press the B key (for "Buffer"), it will save the contents of an offscreen
* buffer (a PGraphics object). The image will have the size of the buffer, which
* you defined during `buffer = createGraphics(bufferWidth,bufferHeight).`
*
* For B command, the code looks for a PGraphics variable called "buffer" (which
* is the "standard" I use); if it cannot find "buffer", it looks for the first variable
* of type PGraphics, regardless of name. This overengineered way is meant not to break
* the sketch if there is no "buffer" variable - likely there are simpler ways to achieve
* the same.
*
* Version: 27.07.2018
*/
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;
import java.lang.reflect.Field;
PGraphics bufferToSave;
void keyPressed() {
String sketchName = this.getClass().getName();
SimpleDateFormat formatter = new SimpleDateFormat("YYYYMMDD_HHmmss");
Date date = new Date();
String fileName = String.format("/out/%s_%s", sketchName, formatter.format(date));
if (key == 'S' || key == 's') {
fileName += ".jpg";
saveTo(null, fileName);
} else if (key== 'B' || key == 'b') {
bufferToSave = getBuffer();
fileName += ".tiff";
saveTo(bufferToSave, fileName);
}
}
void saveTo(PGraphics source, String fileName) {
if (source != null) {
buffer.save(fileName);
println(String.format("Contents of buffer saved to %s", fileName));
} else {
((PApplet)this).save(fileName);
println(String.format("Contents of screen saved to %s", fileName));
}
}
/**
*
* Returns the first instance of PGraphics found in the sketch, in no particular order.
*
*/
PGraphics getBuffer() {
//looking for a variable called "buffer"
try {
Field bufferField = this.getClass().getField("buffers");
if (bufferField != null)
return (PGraphics)bufferField.get(this);
}
catch(NoSuchFieldException ex) {
}
catch ( IllegalAccessException ex ) {
System.out.println(ex);
}
//if "buffer" not found, then look for first instance of PGraphics
Field[] fields = this.getClass().getDeclaredFields();
//print field names paired with their values
for ( Field field : fields ) {
try {
if (field.getType().getName().contains("PGraphics")) {
return (PGraphics)field.get(this);
}
}
catch ( IllegalAccessException ex ) {
System.out.println(ex);
}
}
return null;
}