mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* LoadFile 1
|
||||
*
|
||||
* Loads a text file that contains two numbers separated by a tab ('\t').
|
||||
* A new pair of numbers is loaded each frame and used to draw a point on the screen.
|
||||
*/
|
||||
|
||||
String[] lines;
|
||||
int index = 0;
|
||||
|
||||
void setup() {
|
||||
size(200, 200);
|
||||
background(0);
|
||||
stroke(255);
|
||||
frameRate(12);
|
||||
lines = loadStrings("positions.txt");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (index < lines.length) {
|
||||
String[] pieces = split(lines[index], '\t');
|
||||
if (pieces.length == 2) {
|
||||
int x = int(pieces[0]) * 2;
|
||||
int y = int(pieces[1]) * 2;
|
||||
point(x, y);
|
||||
}
|
||||
// Go to the next line for the next run through draw()
|
||||
index = index + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
70 35
|
||||
69 35
|
||||
68 39
|
||||
67 42
|
||||
66 47
|
||||
64 51
|
||||
64 54
|
||||
63 57
|
||||
60 60
|
||||
58 64
|
||||
51 69
|
||||
48 72
|
||||
44 73
|
||||
39 75
|
||||
35 75
|
||||
30 75
|
||||
25 75
|
||||
21 75
|
||||
17 73
|
||||
13 69
|
||||
12 66
|
||||
11 61
|
||||
11 57
|
||||
10 49
|
||||
10 45
|
||||
10 38
|
||||
12 32
|
||||
13 29
|
||||
16 23
|
||||
20 19
|
||||
24 16
|
||||
27 15
|
||||
31 13
|
||||
33 13
|
||||
37 13
|
||||
40 15
|
||||
42 16
|
||||
45 19
|
||||
46 21
|
||||
47 24
|
||||
48 26
|
||||
48 29
|
||||
48 33
|
||||
47 39
|
||||
43 45
|
||||
42 47
|
||||
38 50
|
||||
35 51
|
||||
32 51
|
||||
30 51
|
||||
27 50
|
||||
27 50
|
||||
26 46
|
||||
26 41
|
||||
29 36
|
||||
30 34
|
||||
31 33
|
||||
31 33
|
||||
32 33
|
||||
33 33
|
||||
34 33
|
||||
34 33
|
||||
35 33
|
||||
37 33
|
||||
39 33
|
||||
42 32
|
||||
44 31
|
||||
46 29
|
||||
48 29
|
||||
49 27
|
||||
52 24
|
||||
53 23
|
||||
57 19
|
||||
61 16
|
||||
63 14
|
||||
67 13
|
||||
69 12
|
||||
69 12
|
||||
77 11
|
||||
77 11
|
||||
80 11
|
||||
86 16
|
||||
90 21
|
||||
93 25
|
||||
95 29
|
||||
95 32
|
||||
95 33
|
||||
95 37
|
||||
94 41
|
||||
93 44
|
||||
92 46
|
||||
91 49
|
||||
89 51
|
||||
87 55
|
||||
85 59
|
||||
82 62
|
||||
80 64
|
||||
79 67
|
||||
77 69
|
||||
74 71
|
||||
68 72
|
||||
65 73
|
||||
63 73
|
||||
62 73
|
||||
60 72
|
||||
58 69
|
||||
57 67
|
||||
57 66
|
||||
56 60
|
||||
56 56
|
||||
56 54
|
||||
58 49
|
||||
60 47
|
||||
62 47
|
||||
63 47
|
||||
67 48
|
||||
70 52
|
||||
73 55
|
||||
74 57
|
||||
74 58
|
||||
74 60
|
||||
74 62
|
||||
73 65
|
||||
70 68
|
||||
67 69
|
||||
65 70
|
||||
63 70
|
||||
62 70
|
||||
60 68
|
||||
57 65
|
||||
55 64
|
||||
50 62
|
||||
46 61
|
||||
40 60
|
||||
38 60
|
||||
36 60
|
||||
32 61
|
||||
30 62
|
||||
27 64
|
||||
26 68
|
||||
25 71
|
||||
25 77
|
||||
25 81
|
||||
26 84
|
||||
28 86
|
||||
31 87
|
||||
33 88
|
||||
36 88
|
||||
39 86
|
||||
41 85
|
||||
43 83
|
||||
44 81
|
||||
45 76
|
||||
45 74
|
||||
45 71
|
||||
40 67
|
||||
37 65
|
||||
34 63
|
||||
33 61
|
||||
33 61
|
||||
32 60
|
||||
33 49
|
||||
37 45
|
||||
41 41
|
||||
45 39
|
||||
47 38
|
||||
51 37
|
||||
54 37
|
||||
58 38
|
||||
61 41
|
||||
63 44
|
||||
65 46
|
||||
66 49
|
||||
66 51
|
||||
67 55
|
||||
67 58
|
||||
67 60
|
||||
66 62
|
||||
64 65
|
||||
63 66
|
||||
61 67
|
||||
60 68
|
||||
58 68
|
||||
55 69
|
||||
54 69
|
||||
51 69
|
||||
48 69
|
||||
46 68
|
||||
45 66
|
||||
44 65
|
||||
44 63
|
||||
44 61
|
||||
44 59
|
||||
44 56
|
||||
44 55
|
||||
45 53
|
||||
47 52
|
||||
49 50
|
||||
50 48
|
||||
51 47
|
||||
52 46
|
||||
54 46
|
||||
55 45
|
||||
55 45
|
||||
56 44
|
||||
57 44
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* LoadFile 2
|
||||
*
|
||||
* This example loads a data file about cars. Each element is separated
|
||||
* with a tab and corresponds to a different aspect of each car. The file stores
|
||||
* the miles per gallon, cylinders, displacement, etc., for more than 400 different
|
||||
* makes and models. Press a mouse button to advance to the next group of entries.
|
||||
*/
|
||||
|
||||
Record[] records;
|
||||
String[] lines;
|
||||
int recordCount;
|
||||
PFont body;
|
||||
int num = 9; // Display this many entries on each screen.
|
||||
int startingEntry = 0; // Display from this entry number
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
fill(255);
|
||||
noLoop();
|
||||
|
||||
body = loadFont("TheSans-Plain-12.vlw");
|
||||
textFont(body);
|
||||
|
||||
lines = loadStrings("cars2.tsv");
|
||||
records = new Record[lines.length];
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
String[] pieces = split(lines[i], '\t'); // Load data into arrayif (pieces.length == 9) {
|
||||
records[recordCount] = new Record(pieces);
|
||||
recordCount++;
|
||||
}
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
for (int i = 0; i < num; i++) {
|
||||
int thisEntry = startingEntry + i;
|
||||
text(thisEntry + " > " + records[thisEntry].name, 20, 20 + i*20); // Print name to console
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
startingEntry += num;
|
||||
if (startingEntry + num > records.length) {
|
||||
startingEntry -= num;
|
||||
}
|
||||
redraw();
|
||||
}
|
||||
|
||||
class Record {
|
||||
String name;
|
||||
float mpg;
|
||||
int cylinders;
|
||||
float displacement;
|
||||
float horsepower;
|
||||
float weight;
|
||||
float acceleration;
|
||||
int year;
|
||||
float origin;
|
||||
public Record(String[] pieces) {
|
||||
name = pieces[0];
|
||||
mpg = float(pieces[1]);
|
||||
cylinders = int(pieces[2]);
|
||||
displacement = float(pieces[3]);
|
||||
horsepower = float(pieces[4]);
|
||||
weight = float(pieces[5]);
|
||||
acceleration = float(pieces[6]);
|
||||
year = int(pieces[7]);
|
||||
origin = float(pieces[8]);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* SaveFile 1
|
||||
*
|
||||
* Saving files is a useful way to store data so it can be viewed after a
|
||||
* program has stopped running. The saveStrings() function writes an array
|
||||
* of strings to a file, with each string written to a new line. This file
|
||||
* is saved to the sketch's folder. This example won't work in a web browser
|
||||
* because of Java security restrictions.
|
||||
*/
|
||||
|
||||
int[] x = new int[0];
|
||||
int[] y = new int[0];
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(204);
|
||||
stroke(0);
|
||||
noFill();
|
||||
beginShape();
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
vertex(x[i], y[i]);
|
||||
}
|
||||
endShape();
|
||||
// Show the next segment to be added
|
||||
if (x.length >= 1) {
|
||||
stroke(255);
|
||||
line(mouseX, mouseY, x[x.length-1], y[x.length-1]);
|
||||
}
|
||||
}
|
||||
|
||||
void mousePressed() { // Click to add a line segment
|
||||
x = append(x, mouseX);
|
||||
y = append(y, mouseY);
|
||||
}
|
||||
|
||||
void keyPressed() { // Press a key to save the data
|
||||
String[] lines = new String[x.length];
|
||||
for (int i = 0; i < x.length; i++) {
|
||||
lines[i] = x[i] + "\t" + y[i];
|
||||
}
|
||||
saveStrings("lines.txt", lines);
|
||||
exit(); // Stop the program
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* SaveFile 2
|
||||
*
|
||||
* This file a PrintWriter object to write data continuously to a file
|
||||
* while the mouse is pressed. When a key is pressed, the file closes
|
||||
* itself and the program is stopped. This example won't work in a web browser
|
||||
* because of Java security restrictions.
|
||||
*/
|
||||
|
||||
PrintWriter output;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
// Create a new file in the sketch directory
|
||||
output = createWriter("positions.txt");
|
||||
frameRate(12);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
if (mousePressed) {
|
||||
point(mouseX, mouseY);
|
||||
// Write the coordinate to a file with a
|
||||
// "\t" (TAB character) between each entry
|
||||
output.println(mouseX + "\t" + mouseY);
|
||||
}
|
||||
}
|
||||
|
||||
void keyPressed() { // Press a key to save the data
|
||||
output.flush(); // Write the remaining data
|
||||
output.close(); // Finish the file
|
||||
exit(); // Stop the program
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Save Many Images.
|
||||
*
|
||||
* The saveFrame() function allows you to save images from
|
||||
* a program while it is running. This example saves the first
|
||||
* 50 frames of a program. These images can be imported into
|
||||
* animation software or QuickTime and then saved as a movie.
|
||||
*/
|
||||
|
||||
float x = 33;
|
||||
float numFrames = 50;
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
smooth();
|
||||
noStroke();
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(0);
|
||||
x += random(-2, 2);
|
||||
ellipse(x, 100, 80, 80);
|
||||
if (frameCount <= numFrames) {
|
||||
saveFrame("circles-####.tif");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Save One Image
|
||||
*
|
||||
* The save() function allows you to save an image from the
|
||||
* display window. In this example, save() is run when a mouse
|
||||
* button is pressed. The image "line.tif" is saved to the
|
||||
* same folder as the sketch's program file.
|
||||
*/
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(200, 200);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
background(204);
|
||||
line(0, 0, mouseX, height);
|
||||
line(width, 0, 0, mouseY);
|
||||
}
|
||||
|
||||
void mousePressed()
|
||||
{
|
||||
save("line.tif");
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Tile Images
|
||||
*
|
||||
* Draws an image larger than the screen by tiling it into small sections.
|
||||
* The scaleValue variable sets amount of scaling: 1 is 100%, 2 is 200%, etc.
|
||||
*/
|
||||
|
||||
int scaleValue = 3; // Multiplication factor
|
||||
int xoffset = 0; // x-axis offset
|
||||
int yoffset = 0; // y-axis offset
|
||||
|
||||
void setup()
|
||||
{
|
||||
size(600, 600);
|
||||
stroke(0, 100);
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
scale(scaleValue);
|
||||
translate(xoffset *(-width / scaleValue), yoffset *(-height / scaleValue));
|
||||
line(10, 150, 500, 50);
|
||||
line(0, 600, 600, 0);
|
||||
setOffset();
|
||||
}
|
||||
|
||||
void setOffset()
|
||||
{
|
||||
save("lines-" + xoffset + "-" + yoffset + ".jpg");
|
||||
xoffset++;
|
||||
if (xoffset == scaleValue) {
|
||||
xoffset = 0;
|
||||
yoffset++;
|
||||
if (yoffset == scaleValue) {
|
||||
exit();
|
||||
}
|
||||
}
|
||||
background(204);
|
||||
}
|
||||
Reference in New Issue
Block a user