Table Of ContentProgramming 101
The How and Why of Programming
Revealed Using the Processing
Programming Language
Jeanine Meyer
Programming 101
Jeanine Meyer
Mt Kisco, New York, USA
ISBN-13 (pbk): 978-1-4842-3696-3 ISBN-13 (electronic): 978-1-4842-3697-0
https://doi.org/10.1007/978-1-4842-3697-0
Library of Congress Control Number: 2018946534
Copyright © 2018 by Jeanine Meyer
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484236963. For more
detailed information, please visit http://www.apress.com/source-code.
Contents
Introduction xix
Chapter 1: Basics1
Programming Concepts ����������������������������������������������������������������������������������������������������������������2
Programming Languages and Natural Languages ������������������������������������������������������������������2
Values and Variables ���������������������������������������������������������������������������������������������������������������3
Functions ���������������������������������������������������������������������������������������������������������������������������������5
Specifying Positions and Angles ���������������������������������������������������������������������������������������������5
Colors ��������������������������������������������������������������������������������������������������������������������������������������6
Development Environment ������������������������������������������������������������������������������������������������������6
Role of Planning ����������������������������������������������������������������������������������������������������������������������7
Under the Covers���������������������������������������������������������������������������������������������������������������������������7
Processing Programming Features �����������������������������������������������������������������������������������������������8
Processing Development Environment ������������������������������������������������������������������������������������8
Functions �������������������������������������������������������������������������������������������������������������������������������10
Angles ������������������������������������������������������������������������������������������������������������������������������������11
Implementing Hello, World ����������������������������������������������������������������������������������������������������������13
Implementing the Daddy Logo ����������������������������������������������������������������������������������������������������21
Planning ���������������������������������������������������������������������������������������������������������������������������������21
Daddy Logo Program �������������������������������������������������������������������������������������������������������������24
Things to Look Up �����������������������������������������������������������������������������������������������������������������������27
How to Make This Your Own �������������������������������������������������������������������������������������������������������27
What You Learned �����������������������������������������������������������������������������������������������������������������������28
What’s Next ���������������������������������������������������������������������������������������������������������������������������������29
Chapter 2: Interactions 31
Programming Concepts ��������������������������������������������������������������������������������������������������������������33
Events ������������������������������������������������������������������������������������������������������������������������������������33
Randomness ��������������������������������������������������������������������������������������������������������������������������34
Displaying Images from Files ������������������������������������������������������������������������������������������������34
Calculations, Including Built-in Functions �����������������������������������������������������������������������������34
Looping ����������������������������������������������������������������������������������������������������������������������������������35
Processing Programming Features ���������������������������������������������������������������������������������������������35
Under the Covers�������������������������������������������������������������������������������������������������������������������������39
Polygon Sketch Operation Overview �������������������������������������������������������������������������������������������40
Implementing the Polygon Sketch ����������������������������������������������������������������������������������������������42
Planning ���������������������������������������������������������������������������������������������������������������������������������42
Polygon Sketch Program �������������������������������������������������������������������������������������������������������46
Coin Toss Sketch Operation Overview �����������������������������������������������������������������������������������������48
Implementing the Coin Toss Sketch ��������������������������������������������������������������������������������������������50
Planning ���������������������������������������������������������������������������������������������������������������������������������50
Things to Look Up �����������������������������������������������������������������������������������������������������������������������53
How to Make This Your Own �������������������������������������������������������������������������������������������������������54
What You Learned �����������������������������������������������������������������������������������������������������������������������55
What’s Next ���������������������������������������������������������������������������������������������������������������������������������55
Chapter 3: Animation Using Arrays and Parallel Structures 57
More on the Sketches �����������������������������������������������������������������������������������������������������������������57
Programming Concepts ��������������������������������������������������������������������������������������������������������������63
Animation ������������������������������������������������������������������������������������������������������������������������������63
Logical Operations �����������������������������������������������������������������������������������������������������������������63
Arrays ������������������������������������������������������������������������������������������������������������������������������������64
Parallel Structures �����������������������������������������������������������������������������������������������������������������64
Compound Statements ����������������������������������������������������������������������������������������������������������64
Pseudo-Random Processing �������������������������������������������������������������������������������������������������64
Processing Programming Features ���������������������������������������������������������������������������������������������65
Implementing a Bouncing Ball ����������������������������������������������������������������������������������������������������69
Planning ���������������������������������������������������������������������������������������������������������������������������������69
Program ���������������������������������������������������������������������������������������������������������������������������������70
Implementing a Set of Three Bouncing Balls ������������������������������������������������������������������������������71
Planning ���������������������������������������������������������������������������������������������������������������������������������72
Program ���������������������������������������������������������������������������������������������������������������������������������72
Implementing Pentagon Bouncing ����������������������������������������������������������������������������������������������74
Planning ���������������������������������������������������������������������������������������������������������������������������������74
Implementing Bouncing Polygons �����������������������������������������������������������������������������������������������76
Planning ���������������������������������������������������������������������������������������������������������������������������������76
Program ���������������������������������������������������������������������������������������������������������������������������������77
Under the Covers�������������������������������������������������������������������������������������������������������������������������79
Things to Look Up �����������������������������������������������������������������������������������������������������������������������80
How to Make This Your Own �������������������������������������������������������������������������������������������������������80
What You Learned �����������������������������������������������������������������������������������������������������������������������81
What’s Next ���������������������������������������������������������������������������������������������������������������������������������81
Chapter 4: Classes 83
Programming Concepts ��������������������������������������������������������������������������������������������������������������83
Classes ����������������������������������������������������������������������������������������������������������������������������������������83
Phases of Operations �������������������������������������������������������������������������������������������������������������84
Tolerance or Margin ���������������������������������������������������������������������������������������������������������������85
Processing Programming Features ���������������������������������������������������������������������������������������������85
Classes ����������������������������������������������������������������������������������������������������������������������������������85
Dynamic Arrays ���������������������������������������������������������������������������������������������������������������������86
Tolerance and OK So Far Coding �������������������������������������������������������������������������������������������88
Bouncing Objects Overview ��������������������������������������������������������������������������������������������������������88
Implementing the Bouncing Objects �������������������������������������������������������������������������������������������90
Planning ���������������������������������������������������������������������������������������������������������������������������������90
Program ���������������������������������������������������������������������������������������������������������������������������������92
Make Path and Travel Path Overview ������������������������������������������������������������������������������������������96
Implementing the Make Path and Travel Path ��������������������������������������������������������������������������100
Planning �������������������������������������������������������������������������������������������������������������������������������100
Program �������������������������������������������������������������������������������������������������������������������������������102
Jigsaw Overview �����������������������������������������������������������������������������������������������������������������������105
Implementing the Jigsaw����������������������������������������������������������������������������������������������������������107
Planning �������������������������������������������������������������������������������������������������������������������������������108
Program �������������������������������������������������������������������������������������������������������������������������������109
Under the Covers�����������������������������������������������������������������������������������������������������������������������120
Things to Look Up ���������������������������������������������������������������������������������������������������������������������121
How to Make This Your Own �����������������������������������������������������������������������������������������������������122
What You Learned ���������������������������������������������������������������������������������������������������������������������122
What’s Next �������������������������������������������������������������������������������������������������������������������������������122
Chapter 5: More Interactions 123
More on the Sketches ���������������������������������������������������������������������������������������������������������������123
Programming Concepts ������������������������������������������������������������������������������������������������������������123
Ballistic Motion ��������������������������������������������������������������������������������������������������������������������124
Character (char) Data Type vs� String Data Type������������������������������������������������������������������125
Use of Files ��������������������������������������������������������������������������������������������������������������������������125
Case Statement �������������������������������������������������������������������������������������������������������������������125
Elapsed Time �����������������������������������������������������������������������������������������������������������������������125
Regular Expressions ������������������������������������������������������������������������������������������������������������125
Processing Programming Features �������������������������������������������������������������������������������������������126
The char Data Type ��������������������������������������������������������������������������������������������������������������126
The keyPressed Function, key, and keyCode �����������������������������������������������������������������������126
Table Files ����������������������������������������������������������������������������������������������������������������������������127
The Case Statement ������������������������������������������������������������������������������������������������������������127
The millis and Other Time Functions �����������������������������������������������������������������������������������129
Under the Covers�����������������������������������������������������������������������������������������������������������������������130
Slingshot Operation Overview ���������������������������������������������������������������������������������������������������132
Implementing the Slingshot Sketch ������������������������������������������������������������������������������������������133
Planning �������������������������������������������������������������������������������������������������������������������������������133
Programming the Slingshot Sketch �������������������������������������������������������������������������������������136
Snake Operation Overview ��������������������������������������������������������������������������������������������������������145
Implementing the Snake Sketch �����������������������������������������������������������������������������������������������146
Planning �������������������������������������������������������������������������������������������������������������������������������147
Programming the Snake Sketch ������������������������������������������������������������������������������������������148
Image Test Operation Overview �������������������������������������������������������������������������������������������156
Implementing the Image Test ����������������������������������������������������������������������������������������������159
Things to Look Up ���������������������������������������������������������������������������������������������������������������������164
How to Make This Your Own �����������������������������������������������������������������������������������������������������164
What You Learned ���������������������������������������������������������������������������������������������������������������������165
What’s Next �������������������������������������������������������������������������������������������������������������������������165
Chapter 6: Images, Graphics, and Building on Prior Work 167
More on the Sketches ���������������������������������������������������������������������������������������������������������������167
Programming Concepts ������������������������������������������������������������������������������������������������������������168
Error Handling ���������������������������������������������������������������������������������������������������������������������168
Images as Arrays of Pixels ��������������������������������������������������������������������������������������������������168
Transformations �������������������������������������������������������������������������������������������������������������������169
Processing Programming Features �������������������������������������������������������������������������������������������169
Getting a File from the Web �������������������������������������������������������������������������������������������������169
Pixel Processing ������������������������������������������������������������������������������������������������������������������170
The beginShape and endShape Vertex Functions ���������������������������������������������������������������171
Transformations �������������������������������������������������������������������������������������������������������������������171
Under the Covers�����������������������������������������������������������������������������������������������������������������������172
Image to Grayscale Operation Overview �����������������������������������������������������������������������������������172
Implementing the Image to Grayscale ��������������������������������������������������������������������������������������180
Planning �������������������������������������������������������������������������������������������������������������������������������180
Programming the Image to Grayscale ���������������������������������������������������������������������������������181
Origami Flower Operation Overview �����������������������������������������������������������������������������������������186
Implementing the Origami Flower Sketch���������������������������������������������������������������������������������187
Planning �������������������������������������������������������������������������������������������������������������������������������187
Programming the Origami Flower ���������������������������������������������������������������������������������������189
Things to Look Up ���������������������������������������������������������������������������������������������������������������������193
How to Make This Your Own �����������������������������������������������������������������������������������������������������193
What You Learned ���������������������������������������������������������������������������������������������������������������������194
What’s Next �������������������������������������������������������������������������������������������������������������������������������194
Chapter 7: Using Files for Making a Holiday Card 195
Programming Concepts ������������������������������������������������������������������������������������������������������������196
Files �������������������������������������������������������������������������������������������������������������������������������������196
Libraries�������������������������������������������������������������������������������������������������������������������������������197
Fonts ������������������������������������������������������������������������������������������������������������������������������������197
Callbacks �����������������������������������������������������������������������������������������������������������������������������198
Feedback to Users ���������������������������������������������������������������������������������������������������������������198
Processing Programming Features �������������������������������������������������������������������������������������������198
Use of the Sound Library �����������������������������������������������������������������������������������������������������198
Making and Saving an Image of the Current Window ���������������������������������������������������������199
Use of Java Input/Output Library �����������������������������������������������������������������������������������������200
Subclasses ��������������������������������������������������������������������������������������������������������������������������201
Show Fonts Sketch Operation Overview �����������������������������������������������������������������������������������202
Implementing the Show Fonts Sketch ��������������������������������������������������������������������������������������203
Programming the Show Fonts Sketch ���������������������������������������������������������������������������������204
Make Card Sketch Operation Overview ������������������������������������������������������������������������������������205
Implementing the Make Card Sketch ����������������������������������������������������������������������������������������210
Planning �������������������������������������������������������������������������������������������������������������������������������210
Programming the Make Card Sketch �����������������������������������������������������������������������������������212
Under the Covers�����������������������������������������������������������������������������������������������������������������������218
Things to Look Up ���������������������������������������������������������������������������������������������������������������������219
How to Make This Your Own �����������������������������������������������������������������������������������������������������219
What You Learned ���������������������������������������������������������������������������������������������������������������������219
What’s Next �������������������������������������������������������������������������������������������������������������������������������220
Chapter 8: Combining Videos, Images, and Graphics 221
Programming Concepts ������������������������������������������������������������������������������������������������������������221
Video ������������������������������������������������������������������������������������������������������������������������������������221
Copying a Video �������������������������������������������������������������������������������������������������������������������222
Processing Programming Features �������������������������������������������������������������������������������������������222
Video ������������������������������������������������������������������������������������������������������������������������������������222
Classes and Subclasses ������������������������������������������������������������������������������������������������������224
Under the Covers�����������������������������������������������������������������������������������������������������������������������224
Family Collage Operation Overview ������������������������������������������������������������������������������������������225
Implementing the Family Collage Sketch����������������������������������������������������������������������������������227
Planning �������������������������������������������������������������������������������������������������������������������������������227
Programming the Family Collage Sketch ����������������������������������������������������������������������������228
Things to Look Up ���������������������������������������������������������������������������������������������������������������������237
How to Make This Your Own �����������������������������������������������������������������������������������������������������237
What You Learned ���������������������������������������������������������������������������������������������������������������������242
What’s Next �������������������������������������������������������������������������������������������������������������������������������242
Chapter 9: Hangman 243
More on the Sketches ���������������������������������������������������������������������������������������������������������������243
Programming Concepts ������������������������������������������������������������������������������������������������������������245
Implementing an Existing Application ���������������������������������������������������������������������������������246
Testing and Scaling Up ��������������������������������������������������������������������������������������������������������246
Processing Programming Features �������������������������������������������������������������������������������������������247
Hangman Sketches Operation Overview �����������������������������������������������������������������������������������249
Implementing the Hangman Sketches ��������������������������������������������������������������������������������������253
Planning �������������������������������������������������������������������������������������������������������������������������������253
Programming the Hangman Sketches ���������������������������������������������������������������������������������255
Things to Look Up ���������������������������������������������������������������������������������������������������������������������268
How to Make This Your Own �����������������������������������������������������������������������������������������������������269
What You Learned ���������������������������������������������������������������������������������������������������������������������269
What’s Next �������������������������������������������������������������������������������������������������������������������������������269
Chapter 10: 3D 271
Programming Concepts ������������������������������������������������������������������������������������������������������������274
Processing Programming Features �������������������������������������������������������������������������������������������275
Under the Covers�����������������������������������������������������������������������������������������������������������������������284
Rolling Ball at Alhambra Operation Overview ���������������������������������������������������������������������������284
Implementing the Rolling Ball at Alhambra �������������������������������������������������������������������������������285
Planning �������������������������������������������������������������������������������������������������������������������������������285
Programming the Rolling Ball at Alhambra �������������������������������������������������������������������������286
Rotating Cube Operation Overview �������������������������������������������������������������������������������������������291
Implementing the Rotating Cube ����������������������������������������������������������������������������������������������291
Planning �������������������������������������������������������������������������������������������������������������������������������291
Programming the Rotating Cube �����������������������������������������������������������������������������������������293
Things to Look Up ���������������������������������������������������������������������������������������������������������������������298
How to Make This Your Own �����������������������������������������������������������������������������������������������������299
What You Learned ���������������������������������������������������������������������������������������������������������������������301
What’s Next �������������������������������������������������������������������������������������������������������������������������������302
Appendix A: Publishing on the Web 303
I mplementation �������������������������������������������������������������������������������������������������������������������������306
Preloading of Images ����������������������������������������������������������������������������������������������������������������307
Adding HTML to the body Element ��������������������������������������������������������������������������������������������308
Adding Responses to Touch ������������������������������������������������������������������������������������������������������309
Index 313