Programming 101Learn to Code with the ProcessingLanguage Using a Visual ApproachSecond EditionJeanine MeyerProgramming 101: Learn to Code with the Processing Language Using a VisualApproach Jeanine Meyer Mt Kisco, NY, USA ISBN-13 (pbk): 978-1-4842-8193-2 ISBN-13 (electronic): 978-1-4842-8194-9 https://doi.org/10.1007/978-1-4842-8194-9 Copyright 2022 by Jeanine Meyer This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made.
The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: James Robinson-Prior Development Editor: James Markham Coordinating Editor: Jessica Vakili Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 NY Plaza, New York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail booktranslations@springernature.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on the Github repository: https://github.com/Apress/Programming-101. For more detailed information, please visit http://www.apress.com/source- code. Printed on acid-free paper To my family, who inspire and teach me.Table of Contents About the Author xv About the Technical Reviewer xvii Acknowledgments xix Introduction xxi Chapter 1: Basics 1 Abstract 1 Programming Concepts 3 Programming Languages and Natural Languages 3 Values and Variables 3 Functions 5 Specifying Positions and Angles 6 Colors 7 Development Environment 7 Role of Planning 8 Under the Covers 8 Processing Programming Features 9 Processing Development Environment 9 Functions 11 Angles 12 Implementing Hello, World 14 Implementing the Daddy Logo 22 Planning 22 Daddy Logo Program 25 v Table of ConTenTs Things to Look Up 29 How to Make This Your Own 29 What You Learned 30 Whats Next 31 Chapter 2: Interactions 33 Abstract 33 Note 33 Programming Concepts 35 Events 35 Randomness 36 Displaying Images from Files 36 Calculations, Including Built-in Functions 36 Looping 37 Processing Programming Features 37 Note 38 Under the Covers 41 Polygon Sketch Operation Overview 42 Implementing the Polygon Sketch 44 Planning 44 Polygon Sketch Program 47 Coin-Toss Sketch Operation Overview 49 Implementing the Coin-Toss Sketch 52 Planning 52 Things to Look Up 56 How to Make This Your Own 56 What You Learned 57 Whats Next 58 vi Table of ConTenTs Chapter 3: Animation Using Arrays and Parallel Structures 59 Abstract 59 More on the Sketches 59 Programming Concepts 64 Animation 65 Logical Operations 65 Arrays 65 Parallel Structures 66 Compound Statements 66 Pseudorandom Processing 66 Processing Programming Features 67 Caution 67 Implementing a Bouncing Ball 71 Planning 71 Program 72 Implementing a Set of Three Bouncing Balls 74 Planning 74 Program 74 Implementing Pentagon Bouncing 76 Planning 76 Implementing Bouncing Polygons 78 Planning 79 Program 79 Under the Covers 82 Things to Look Up 82 How to Make This Your Own 83 What You Learned 83 Whats Next 84 vii Table of ConTenTs Chapter 4: Classes 85 Abstract 85 Programming Concepts 86 Classes 86 Phases of Operations 87 Tolerance or Margin 88 Processing Programming Features 88 Classes 88 Definition of Images, Rectangles, and Ellipses 90 Dynamic Arrays 90 Tolerance and OK-So-Far Coding 92 Bouncing Objects Overview 92 Implementing the Bouncing Objects 94 Planning 94 Program 96 Make Path and Travel Path Overview 100 Implementing the Make Path and Travel Path 103 Planning 104 Program 105 Jigsaw Overview 109 Implementing the Jigsaw 111 Planning 111 Program 114 Under the Covers 125 Things to Look Up 126 How to Make This Your Own 126 What You Learned 127 Whats Next 127 viii Table of ConTenTs Chapter 5: More Interactions 129 Abstract 129 More on the Sketches 129 Programming Concepts 130 Ballistic Motion 130 Character (char) Data Type vs String Data Type 131 Use of Files 131 Case Statement 131 Elapsed Time 131 Regular Expressions 132 Processing Programming Features 132 The char Data Type 132 The keyPressed Function, key, and keyCode 133 Table Files 133 The Switch Statement 133 The millis and Other Time Functions 135 The match Function for Regular Expressions 136 ArrayList 136 Under the Covers 136 Slingshot Operation Overview 138 Implementing the Slingshot Sketch 139 Planning 139 Programming the Slingshot Sketch 142 Snake Operation Overview 151 Implementing the Snake Sketch 153 Planning 154 Programming the Snake Sketch 155 Image Test Operation Overview 163 Implementing the Image Test 166 ix Table of ConTenTs Things to Look Up 171 How to Make This Your Own 171 What You Learned 172 Whats Next 172 Chapter 6: Images and Graphics 173 Abstract 173 More on the Sketches 174 Programming Concepts 176 Images As Arrays of Pixels 177 Case Statement 177 Pixel Processing 177 The beginShape and endShape Vertex Functions 178 Changing the Coordinate System 178 Hue-Saturation-Brightness Color Mode 179 Changing Image Sketch Overview 179 Implementing the Image Transformations 187 Planning 187 Programming the Image Sketch 188 Origami Flower Graphic Overview 191 Planning 191 Implementing the Origami Flower Sketch 192 Programming the Origami Flower 194 Programming the Hexagon with HSB Color Mode 197 Under the Covers 203 Things to Look Up 204 How to Make This Your Own 205 What You Learned 206 Whats Next 206 x Table of ConTenTs Chapter 7: Using Files for Making a Holiday Card 207 Abstract 207 Programming Concepts 208 Files 208 Libraries 209 Fonts 209 Callbacks 210 Feedback to Users 210 Processing Programming Features 210 Use of the Sound Library 210 Making and Saving an Image of the Current Window 211 Use of Java Input/Output Library 212 Subclasses 213 Show Fonts Sketch Operation Overview 214 Implementing the Show Fonts Sketch 216 Programming the Show Fonts Sketch 217 Make Card Sketch Operation Overview 218 Implementing the Make Card Sketch 223 Planning 223 Programming the Make Card Sketch 225 Under the Covers 231 Things to Look Up 232 How to Make This Your Own 232 What You Learned 232 Whats Next 233 Chapter 8: Combining Videos, Images, and Graphics 235 Abstract 235 Programming Concepts 235 Video 236 Copying a Video 236 xi Table of ConTenTs Processing Programming Features 236 Video 237 Classes and Subclasses 238 Under the Covers 238 Family Collage Operation Overview 239 Implementing the Family Collage Sketch 242 Planning 242 Programming the Family Collage Sketch 243 Things to Look Up 252 How to Make This Your Own 252 What You Learned 257 Whats Next 257 Chapter 9: Word Guessing Game 259 Abstract 259 More on the Sketches 259 Programming Concepts 262 Implementing an Existing Application 262 Testing and Scaling Up 262 Displaying the State of the Game 263 Displaying Text 264 Processing Programming Features 264 Note 265 Operation Overview 266 Implementing the Word Game Sketches 270 Planning 270 Programming the Word Game Sketches 272 Things to Look Up 286 How to Make This Your Own 287 What You Learned 287 Whats Next 288 xii Table of ConTenTs Chapter 10: 3D 289 Abstract 289 Programming Concepts 292 Processing Programming Features 293 Under the Covers 303 Rolling Ball at Alhambra Operation Overview 303 Implementing the Rolling Ball at Alhambra 304 Planning 304 Programming the Rolling Ball at Alhambra 305 Rotating Cube Operation Overview 310 Implementing the Rotating Cube 310 Planning 310 Programming the Rotating Cube 312 Things to Look Up 318 How to Make This Your Own 319 What You Learned 321 Whats Next 321 Appendix A: Introduction to p5js 323 Getting Started Using p5js 323 Overview of Examples 325 Implementing Daddy Logo 327 Implementing Fearless Girls vs the Bull 331 Implementing Rainbow Helix 336 Whats Next 341 Index 343 xiii
Next page