100% found this document useful (4 votes)
14 views

Download ebooks file Processing for Android: Create Mobile, Sensor-aware, and XR Applications Using Processing, 2nd Edition Andrés Colubri all chapters

XR

Uploaded by

prietlvarm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
14 views

Download ebooks file Processing for Android: Create Mobile, Sensor-aware, and XR Applications Using Processing, 2nd Edition Andrés Colubri all chapters

XR

Uploaded by

prietlvarm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Download Full Version ebookmass - Visit ebookmass.

com

Processing for Android: Create Mobile, Sensor-


aware, and XR Applications Using Processing, 2nd
Edition Andrés Colubri

https://ebookmass.com/product/processing-for-android-create-
mobile-sensor-aware-and-xr-applications-using-
processing-2nd-edition-andres-colubri/

OR CLICK HERE

DOWLOAD NOW

Discover More Ebook - Explore Now at ebookmass.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Processing for Android: Create Mobile, Sensor-aware, and


XR Applications Using Processing, 2nd Edition Andrés
Colubri
https://ebookmass.com/product/processing-for-android-create-mobile-
sensor-aware-and-xr-applications-using-processing-2nd-edition-andres-
colubri/
ebookmass.com

Signal processing for neuroscientists 2nd ed Edition


Drongelen

https://ebookmass.com/product/signal-processing-for-
neuroscientists-2nd-ed-edition-drongelen/

ebookmass.com

Tensors for Data Processing. Theory, Methods, and


Applications Yipeng Liu

https://ebookmass.com/product/tensors-for-data-processing-theory-
methods-and-applications-yipeng-liu/

ebookmass.com

Slavery, Surveillance and Genre in Antebellum United


States Literature Kelly Ross

https://ebookmass.com/product/slavery-surveillance-and-genre-in-
antebellum-united-states-literature-kelly-ross/

ebookmass.com
Analog and Digital Electronics, 1st edition Raghunandan G.
H. Charles H. Roth

https://ebookmass.com/product/analog-and-digital-electronics-1st-
edition-raghunandan-g-h-charles-h-roth/

ebookmass.com

In My Dreams I Hold a Knife Ashley Winstead

https://ebookmass.com/product/in-my-dreams-i-hold-a-knife-ashley-
winstead-5/

ebookmass.com

Selection of HPLC Method in Chemical Analysis Serban C.


Moldoveanu

https://ebookmass.com/product/selection-of-hplc-method-in-chemical-
analysis-serban-c-moldoveanu/

ebookmass.com

Effective Writing for Healthcare Professionals: A Pocket


Guide to Getting Published, 2nd Edition Megan-Jane
Johnstone
https://ebookmass.com/product/effective-writing-for-healthcare-
professionals-a-pocket-guide-to-getting-published-2nd-edition-megan-
jane-johnstone/
ebookmass.com

Wolf Betrayed (Betrayed by Blood Book 2) Bethany Shaw

https://ebookmass.com/product/wolf-betrayed-betrayed-by-blood-
book-2-bethany-shaw/

ebookmass.com
Energy, Entropy, and the Flow of Nature Thomas F. Sherman

https://ebookmass.com/product/energy-entropy-and-the-flow-of-nature-
thomas-f-sherman/

ebookmass.com
Processing for Android
Create Mobile, Sensor-aware, and XR
Applications Using Processing

Second Edition

Andrés Colubri
Processing for Android: Create Mobile, Sensor-aware, and XR Applications Using
Processing, Second Edition
Andrés Colubri
University of Massachusetts, Worcester, MA, USA

ISBN-13 (pbk): 978-1-4842-9584-7 ISBN-13 (electronic): 978-1-4842-9585-4


https://doi.org/10.1007/978-1-4842-9585-4

Copyright © 2023 by Andrés Colubri


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: Spandana Chatterjee
Development Editor: Spandana Chatterjee
Coordinating Editor: Mark Powers
Cover image designed by eStudioCalamar
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004,
U.S.A. 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 GitHub (https://github.com/Apress). For more detailed information, please visit
https://www.apress.com/gp/services/source-code.
Paper in this product is recyclable
To L. J. for accompanying during the writing of this book.
To my family and friends for always supporting me.
And to the Processing community for putting these tools to great use.
Table of Contents

About the Author�����������������������������������������������������������������������������������������������������xv


About the Technical Reviewer�������������������������������������������������������������������������������xvii
Acknowledgments��������������������������������������������������������������������������������������������������xix
Introduction������������������������������������������������������������������������������������������������������������xxi


■Part I: First Steps with Processing for Android���������������������������������� 1

■Chapter 1: Getting Started with the Android Mode����������������������������������������������� 3
What Is the Processing Project?��������������������������������������������������������������������������������������� 3
The Processing Language���������������������������������������������������������������������������������������������������������������������� 3
The Processing Development Environment�������������������������������������������������������������������������������������������� 4

Extending Processing������������������������������������������������������������������������������������������������������� 6
The Contribution Manager���������������������������������������������������������������������������������������������������������������������� 7

Processing for Android����������������������������������������������������������������������������������������������������� 8


Installing the Android Mode�������������������������������������������������������������������������������������������������������������������� 9
Interface of the Android Mode�������������������������������������������������������������������������������������������������������������� 10
Running a Sketch on a Device�������������������������������������������������������������������������������������������������������������� 12
Running a Sketch in the Emulator�������������������������������������������������������������������������������������������������������� 14

Summary������������������������������������������������������������������������������������������������������������������������ 16

■Chapter 2: The Processing Language������������������������������������������������������������������ 17
A Programming Sketchbook for Artists and Designers�������������������������������������������������� 17
The Structure of a Processing Sketch���������������������������������������������������������������������������� 17

v
■ Table of Contents

Drawing with Code��������������������������������������������������������������������������������������������������������� 20


Coordinates������������������������������������������������������������������������������������������������������������������������������������������ 21
Form����������������������������������������������������������������������������������������������������������������������������������������������������� 23
Color����������������������������������������������������������������������������������������������������������������������������������������������������� 25
Applying Geometric Transformations���������������������������������������������������������������������������������������������������� 28

Responding to User Input����������������������������������������������������������������������������������������������� 32


Coding a “Vine Drawing” App����������������������������������������������������������������������������������������� 35
Summary������������������������������������������������������������������������������������������������������������������������ 38

■Chapter 3: From Sketch to Play Store����������������������������������������������������������������� 39
Sketching and Debugging���������������������������������������������������������������������������������������������� 39
Getting Information from the Console�������������������������������������������������������������������������������������������������� 39
Getting more information with logcat��������������������������������������������������������������������������������������������������� 41
Using the Integrated Debugger������������������������������������������������������������������������������������������������������������� 41
Reporting Processing Bugs������������������������������������������������������������������������������������������������������������������ 42

Preparing a Sketch for Release�������������������������������������������������������������������������������������� 42


Adjusting for Device’s DPI�������������������������������������������������������������������������������������������������������������������� 43
Using the Emulator������������������������������������������������������������������������������������������������������������������������������� 46
Setting Icons and Bundle Name����������������������������������������������������������������������������������������������������������� 48
Setting Package Name and Version������������������������������������������������������������������������������������������������������ 48
Exporting As Signed Bundle����������������������������������������������������������������������������������������������������������������� 49

Summary������������������������������������������������������������������������������������������������������������������������ 51


■Part II: Drawing and Interaction������������������������������������������������������� 53

■Chapter 4: Drawing Graphics and Text���������������������������������������������������������������� 55
Drawing Shapes������������������������������������������������������������������������������������������������������������� 55
More Types of Shapes��������������������������������������������������������������������������������������������������������������������������� 55
Curve Shapes��������������������������������������������������������������������������������������������������������������������������������������� 58
Using Bézier Curves����������������������������������������������������������������������������������������������������������������������������� 60
Shape Attributes����������������������������������������������������������������������������������������������������������������������������������� 66
Shape Styles����������������������������������������������������������������������������������������������������������������������������������������� 67

vi
■ Table of Contents

Drawing Images������������������������������������������������������������������������������������������������������������� 69
Drawing Text������������������������������������������������������������������������������������������������������������������� 70
Loading and Creating Fonts������������������������������������������������������������������������������������������������������������������ 70
Text Attributes��������������������������������������������������������������������������������������������������������������������������������������� 73
Scaling Text������������������������������������������������������������������������������������������������������������������������������������������ 73

Renderers in Processing������������������������������������������������������������������������������������������������ 75
The PShape Class����������������������������������������������������������������������������������������������������������� 76
Creating PShapes��������������������������������������������������������������������������������������������������������������������������������� 76
Loading Shapes from SVG�������������������������������������������������������������������������������������������������������������������� 79
Texturing Shapes���������������������������������������������������������������������������������������������������������������������������������� 81

Summary������������������������������������������������������������������������������������������������������������������������ 82

■Chapter 5: Touchscreen Interaction��������������������������������������������������������������������� 83
Touch Events in Android������������������������������������������������������������������������������������������������� 83
Basic Touch Events������������������������������������������������������������������������������������������������������������������������������� 83
Multi-touch Events������������������������������������������������������������������������������������������������������������������������������� 89

Touch-Based Interaction������������������������������������������������������������������������������������������������ 93
Item Selection��������������������������������������������������������������������������������������������������������������������������������������� 93
Scrolling����������������������������������������������������������������������������������������������������������������������������������������������� 95
Swipe and Pinch����������������������������������������������������������������������������������������������������������������������������������� 97

Using the Keyboard������������������������������������������������������������������������������������������������������ 100


Summary���������������������������������������������������������������������������������������������������������������������� 101

■Chapter 6: Live Wallpapers�������������������������������������������������������������������������������� 103
Live Wallpapers������������������������������������������������������������������������������������������������������������ 103
Writing and Installing Live Wallpapers����������������������������������������������������������������������������������������������� 103
Using Multiple Home Screens������������������������������������������������������������������������������������������������������������ 105
Handling Permissions������������������������������������������������������������������������������������������������������������������������� 107

Drawing with Particle Systems������������������������������������������������������������������������������������ 111


Simulating Natural Motion������������������������������������������������������������������������������������������������������������������ 116

vii
■ Table of Contents

An Image-Flow Wallpaper�������������������������������������������������������������������������������������������� 120


Loading, Resizing, and Cropping Images�������������������������������������������������������������������������������������������� 120
Putting Everything Together��������������������������������������������������������������������������������������������������������������� 121
Wrapping the Project Up��������������������������������������������������������������������������������������������������������������������� 124

Summary���������������������������������������������������������������������������������������������������������������������� 126


■Part III: Sensors������������������������������������������������������������������������������ 127

■Chapter 7: Reading Sensor Data������������������������������������������������������������������������ 129
Sensors in Android Devices������������������������������������������������������������������������������������������ 129
Accelerometer������������������������������������������������������������������������������������������������������������������������������������ 129
Gyroscope������������������������������������������������������������������������������������������������������������������������������������������� 130
Magnetometer������������������������������������������������������������������������������������������������������������������������������������ 130
Location���������������������������������������������������������������������������������������������������������������������������������������������� 130

Accessing Sensors from Processing���������������������������������������������������������������������������� 130


Creating a Sensor Manager���������������������������������������������������������������������������������������������������������������� 131
Adding a Sensor Listener������������������������������������������������������������������������������������������������������������������� 131
Reading Data from the Sensor����������������������������������������������������������������������������������������������������������� 132
Reading Data from Other Sensors������������������������������������������������������������������������������������������������������ 134

The Ketai Library���������������������������������������������������������������������������������������������������������� 135


Installing Ketai������������������������������������������������������������������������������������������������������������������������������������ 135
Using Ketai����������������������������������������������������������������������������������������������������������������������������������������� 136
Event Handlers in Ketai���������������������������������������������������������������������������������������������������������������������� 138

Summary���������������������������������������������������������������������������������������������������������������������� 140

■Chapter 8: Driving Graphics and Sound with Sensor Data�������������������������������� 141
Using Ketai to Read Sensor Data���������������������������������������������������������������������������������� 141
Measuring Acceleration������������������������������������������������������������������������������������������������ 141
Shake Detection��������������������������������������������������������������������������������������������������������������������������������� 142
Step Counter��������������������������������������������������������������������������������������������������������������������������������������� 143
Audio-visual Mapping of Step Data���������������������������������������������������������������������������������������������������� 144
Playing Audio�������������������������������������������������������������������������������������������������������������������������������������� 149

viii
■ Table of Contents

Using the Magnetic Sensor������������������������������������������������������������������������������������������ 152


Creating a Compass App�������������������������������������������������������������������������������������������������������������������� 152

The Gyroscope�������������������������������������������������������������������������������������������������������������� 156


Controlling Navigation with the Gyroscope���������������������������������������������������������������������������������������� 159

Summary���������������������������������������������������������������������������������������������������������������������� 162

■Chapter 9: Geolocation�������������������������������������������������������������������������������������� 163
Location Data in Android���������������������������������������������������������������������������������������������� 163
Using Location API in Processing��������������������������������������������������������������������������������� 164
Location Permissions������������������������������������������������������������������������������������������������������������������������� 165
Accessing Data from Multiple Threads����������������������������������������������������������������������������������������������� 167
Location with Ketai����������������������������������������������������������������������������������������������������������������������������� 171
Using Additional Location Data����������������������������������������������������������������������������������������������������������� 172

A Street View Collage��������������������������������������������������������������������������������������������������� 174


Using Google Street View Image API��������������������������������������������������������������������������������������������������� 175
Making a Simple Image Collage��������������������������������������������������������������������������������������������������������� 177
Putting Everything Together��������������������������������������������������������������������������������������������������������������� 179

Summary���������������������������������������������������������������������������������������������������������������������� 182


■Part IV: Wearables and Watch Faces���������������������������������������������� 183

■Chapter 10: Wearable Devices��������������������������������������������������������������������������� 185
From Activity Trackers to Smartwatches���������������������������������������������������������������������� 185
Smartwatches������������������������������������������������������������������������������������������������������������������������������������ 186

Running Watch Face Sketches������������������������������������������������������������������������������������� 186


Using a Watch������������������������������������������������������������������������������������������������������������������������������������� 186
Using the Emulator����������������������������������������������������������������������������������������������������������������������������� 189
Displaying Time���������������������������������������������������������������������������������������������������������������������������������� 191
Counting Steps����������������������������������������������������������������������������������������������������������������������������������� 192

Designing for Smartwatches���������������������������������������������������������������������������������������� 193


Adjusting for Screen Shape���������������������������������������������������������������������������������������������������������������� 194
Watch Face Preview Icons������������������������������������������������������������������������������������������������������������������ 194

Summary���������������������������������������������������������������������������������������������������������������������� 195
ix
■ Table of Contents


■Chapter 11: Visualizing Time����������������������������������������������������������������������������� 197
From Sundials to Smartwatches���������������������������������������������������������������������������������� 197
Using Time to Control Motion������������������������������������������������������������������������������������������������������������� 198
Square vs. Round Watch Faces���������������������������������������������������������������������������������������������������������� 200

Working with a Watch Face Concept���������������������������������������������������������������������������� 203


Elapsed/Remaining Time�������������������������������������������������������������������������������������������������������������������� 204
Adding Interaction������������������������������������������������������������������������������������������������������������������������������ 206
Loading and Displaying Images���������������������������������������������������������������������������������������������������������� 207
Summary���������������������������������������������������������������������������������������������������������������������� 209

■Chapter 12: Visualizing Physical Activity���������������������������������������������������������� 211
Body Sensors��������������������������������������������������������������������������������������������������������������� 211
Step Counter��������������������������������������������������������������������������������������������������������������������������������������� 211
Heart Rate������������������������������������������������������������������������������������������������������������������������������������������� 211

Visualizing Physical Activity in Real Time��������������������������������������������������������������������� 212


Simple Step Counter��������������������������������������������������������������������������������������������������������������������������� 212
Accessing the Heart Rate Sensor������������������������������������������������������������������������������������������������������� 214
Visualizing Step Count Data���������������������������������������������������������������������������������������������������������������� 215
A Beating Heart����������������������������������������������������������������������������������������������������������������������������������� 217
Sensor Debugging������������������������������������������������������������������������������������������������������������������������������ 219

Grow a Tree As You Exercise!��������������������������������������������������������������������������������������� 223


Generating a Tree with a Particle System������������������������������������������������������������������������������������������� 223
Incorporating Step Count Data����������������������������������������������������������������������������������������������������������� 226
Tweaking the Watch Face������������������������������������������������������������������������������������������������������������������� 228
Blooming the Tree������������������������������������������������������������������������������������������������������������������������������� 229

Summary���������������������������������������������������������������������������������������������������������������������� 235


■Part V: 3D and Shaders������������������������������������������������������������������� 237

■Chapter 13: 3D in Processing���������������������������������������������������������������������������� 239
The P3D Renderer�������������������������������������������������������������������������������������������������������� 239
Hello World in 3D�������������������������������������������������������������������������������������������������������������������������������� 239

x
■ Table of Contents

3D Transformations������������������������������������������������������������������������������������������������������ 240


Combining Transformations���������������������������������������������������������������������������������������������������������������� 241

3D Shapes�������������������������������������������������������������������������������������������������������������������� 243


Custom Shapes����������������������������������������������������������������������������������������������������������������������������������� 245
The Camera���������������������������������������������������������������������������������������������������������������������������������������� 246
Immediate vs. Retained Rendering����������������������������������������������������������������������������������������������������� 249
Custom PShape Objects��������������������������������������������������������������������������������������������������������������������� 250
Loading OBJ Shapes��������������������������������������������������������������������������������������������������������������������������� 253
Summary���������������������������������������������������������������������������������������������������������������������� 255

■Chapter 14: Lighting and Texturing������������������������������������������������������������������� 257
Making 3D Scenes more Realistic with Lights and Textures���������������������������������������� 257
Light Sources and Material Properties����������������������������������������������������������������������������������������������� 257
Texture Mapping��������������������������������������������������������������������������������������������������������������������������������� 261

Putting Everything Together����������������������������������������������������������������������������������������� 265


Summary���������������������������������������������������������������������������������������������������������������������� 267

■Chapter 15: GLSL Shaders��������������������������������������������������������������������������������� 269
What Is a Shader?�������������������������������������������������������������������������������������������������������� 269
The Graphics Pipeline: Vertex and Pixel Shaders�������������������������������������������������������������������������������� 269
The PShader Class������������������������������������������������������������������������������������������������������������������������������ 272
Anatomy of a Simple Shader�������������������������������������������������������������������������������������������������������������� 274
Defining Custom Uniforms������������������������������������������������������������������������������������������������������������������ 277

Types of Shaders in Processing����������������������������������������������������������������������������������� 279


Color Shaders������������������������������������������������������������������������������������������������������������������������������������� 279
Texture Shaders���������������������������������������������������������������������������������������������������������������������������������� 280
Light Shaders������������������������������������������������������������������������������������������������������������������������������������� 283
Texlight Shaders��������������������������������������������������������������������������������������������������������������������������������� 286
Image Postprocessing Filters������������������������������������������������������������������������������������������������������������� 288

Day-to-Night Earth Shader and Live Wallpaper������������������������������������������������������������ 292


Summary���������������������������������������������������������������������������������������������������������������������� 301

xi
■ Table of Contents


■Part VI: Extended Reality: VR and AR��������������������������������������������� 303

■Chapter 16: Basics of VR����������������������������������������������������������������������������������� 305
Brief History of VR�������������������������������������������������������������������������������������������������������� 305
Google Cardboard������������������������������������������������������������������������������������������������������������������������������� 305
Hardware Requirements��������������������������������������������������������������������������������������������������������������������� 306

VR in Processing���������������������������������������������������������������������������������������������������������� 306
Stereo Rendering�������������������������������������������������������������������������������������������������������������������������������� 307
Monoscopic Rendering����������������������������������������������������������������������������������������������������������������������� 309

VR Interaction��������������������������������������������������������������������������������������������������������������� 310
Eye and World Coordinates����������������������������������������������������������������������������������������������������������������� 311
The Line of Sight��������������������������������������������������������������������������������������������������������������������������������� 313
Selecting a Shape with Screen Coordinates��������������������������������������������������������������������������������������� 315
Bounding Box Selection���������������������������������������������������������������������������������������������������������������������� 318

Movement in VR����������������������������������������������������������������������������������������������������������� 322


Automatic Movement�������������������������������������������������������������������������������������������������������������������������� 323
Unconstrained Movement������������������������������������������������������������������������������������������������������������������� 325

Summary���������������������������������������������������������������������������������������������������������������������� 328

■Chapter 17: Basics of AR����������������������������������������������������������������������������������� 329
A Brief History of AR����������������������������������������������������������������������������������������������������� 329
AR on Android������������������������������������������������������������������������������������������������������������������������������������� 330
Requirements������������������������������������������������������������������������������������������������������������������������������������� 331
Getting Started����������������������������������������������������������������������������������������������������������������������������������� 331

Drawing Trackables������������������������������������������������������������������������������������������������������ 332


Using Anchors������������������������������������������������������������������������������������������������������������������������������������� 335
Hitting Trackables������������������������������������������������������������������������������������������������������������������������������� 337
Adding a User Interface���������������������������������������������������������������������������������������������������������������������� 340
Using Custom Shaders in AR�������������������������������������������������������������������������������������������������������������� 343
Drawing in AR������������������������������������������������������������������������������������������������������������������������������������� 346
Summary���������������������������������������������������������������������������������������������������������������������� 350

xii
■ Table of Contents


■Chapter 18: Creating XR Experiences���������������������������������������������������������������� 351
Challenges with XR Apps���������������������������������������������������������������������������������������������� 351
Drawing in VR������������������������������������������������������������������������������������������������������������������������������������� 352
Initial Sketches����������������������������������������������������������������������������������������������������������������������������������� 353

A Simple VR UI�������������������������������������������������������������������������������������������������������������� 354


Drawing in 3D��������������������������������������������������������������������������������������������������������������� 359
Flying Around��������������������������������������������������������������������������������������������������������������� 366
Final Tweaks and Packaging���������������������������������������������������������������������������������������� 371
Intro Text��������������������������������������������������������������������������������������������������������������������������������������������� 371
Icons and Package Export������������������������������������������������������������������������������������������������������������������ 372

Summary���������������������������������������������������������������������������������������������������������������������� 373


■Part VII: Appendixes����������������������������������������������������������������������� 375

■Chapter 19: Appendix A: Gradle and Android Studio Integration���������������������� 377
Google Tools for Android Development������������������������������������������������������������������������� 377
Exporting a Sketch As a Gradle Project������������������������������������������������������������������������ 379
Importing into Android Studio�������������������������������������������������������������������������������������� 380
Adding a Processing Sketch to a Layout���������������������������������������������������������������������� 384

■Chapter 20: Appendix B: Processing Libraries for Android������������������������������� 389
Extending Processing with Libraries���������������������������������������������������������������������������� 389
Installing Libraries with the Contribution Manager���������������������������������������������������������������������������� 389
Installing Libraries Manually�������������������������������������������������������������������������������������������������������������� 392

Writing New Libraries��������������������������������������������������������������������������������������������������� 393

Index��������������������������������������������������������������������������������������������������������������������� 397

xiii
About the Author

Andrés Colubri is a senior member of the Processing project and the


main developer of the OpenGL renderer, the video library, and Processing
for Android. He originally studied Physics and Mathematics in Argentina
and later did an MFA in the Design Media Arts Department at UCLA. He
uses Processing as a tool to bridge his interests in computer graphics,
data visualization, and biomedical research. He is currently an assistant
professor at the University of Massachusetts Chan Medical School
(https://co-labo.org).

xv
About the Technical Reviewer

Massimo Nardone has more than 25 years of experience in security, web


and mobile development, cloud, and IT architecture. His true IT passions
are security and Android. He has been programming and teaching others
how to program with Android, Perl, PHP, Java, VB, Python, C/C++, and
MySQL for more than 20 years. He holds a Master of Science degree in
computing science from the University of Salerno, Italy.
He has worked as a CISO, CSO, security executive, IoT executive,
project manager, software engineer, research engineer, chief security
architect, PCI/SCADA auditor, and senior lead IT security/cloud/SCADA
architect for many years. His technical skills include security, Android,
cloud, Java, MySQL, Drupal, COBOL, Perl, web and mobile development,
MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails,
Django CMS, Jekyll, Scratch, and more.
He was a visiting lecturer and supervisor for exercises at the Networking Laboratory of the Helsinki
University of Technology (Aalto University). He also holds four international patents (in the PKI, SIP, SAML,
and Proxy areas). He is currently working for Cognizant as head of cybersecurity and CISO to help clients
in areas of information and cybersecurity, including strategy, planning, processes, policies, procedures,
governance, awareness, and so forth. In June 2017, he became a permanent member of the ISACA
Finland Board.
Massimo has reviewed more than 45 IT books for different publishing companies and is the coauthor
of Pro Spring Security: Securing Spring Framework 5 and Boot 2-based Java Applications (Apress, 2019),
Beginning EJB in Java EE 8 (Apress, 2018), Pro JPA 2 in Java EE 8 (Apress, 2018), and Pro Android Games
(Apress, 2015).

xvii
Acknowledgments

First, I want to give recognition to Ben Fry and Casey Reas, who initiated the Processing project over two
decades ago and have worked steadfastly since then to promote software literacy within the visual arts and
visual literacy within technology. Also, very special thanks to Sam Pottinger for his hard work in Processing 4
and his help to make the Android mode compatible with it.
I would like to express my gratitude to all the Google Summer of Code students who contributed to the
project in very important ways in the years since the first edition: Syam Sundar Kirubakaran (GSoC 2018),
Deeraj Esvar (GSoC 2019), Aditya Rana (GSoC 2020), Rupesh Kumar (GSoC 2022), and Gaurav Puniya
(GSoC 2023).
I extend my appreciation to Chris Coleman and Kate Hollenbach for their invitation to do a residence at
the Clinic for Open Source Arts at the University of Denver in March 2023 to finalize the development of the
Android mode for Processing 4.
Finally, I thank the Apress editors, Spandana Chatterjee, Nirmal Selvaraj, and Mark Powers, for helping
steer this second edition through the finish line.

xix
Introduction

Welcome to the second edition of Processing for Android! In this book, we will explore the exciting world
of creative coding and mobile app development using the Processing programming language specifically
tailored for the Android platform. Whether you are an experienced programmer, an artist looking to expand
your creative horizons, or a beginner interested in exploring the intersection of technology and art, this book
is for you.
Processing, initiated by Ben Fry and Casey Reas in 2001, has revolutionized the way artists, designers,
and developers approach creative coding. Its intuitive syntax and powerful capabilities make it an ideal
tool for creating applications with interactive graphics, sound, animations, and more. With the rise of
smartphones and smartwatches, mobile apps became a widely available medium for exploring new ideas
with code, making Processing for Android a very useful tool to learn, prototype, and produce app projects.
Written by Andrés Colubri, an experienced software developer, computational scientist, and media
artist who has been an active contributor to the Processing project for more than ten years, this book is a
comprehensive guide that will take you on a journey from the fundamentals of Processing coding to building
complex Android applications. No matter if you are new to Processing or have previous experience with
the language, this book will equip you with the knowledge and skills necessary to develop engaging and
innovative apps.
The book is designed with a hands-on approach to learning. Through a series of step-by-step tutorials
and practical examples, you will learn how to set up your Processing development environment, navigate
the Processing for Android interface, and harness the power of the Android platform to craft app-based
experiences. From creating interactive animations to working with sensors, cameras, GLSL shaders, and
virtual and augmented reality, you will gain a deep understanding of the capabilities that are offered by
Processing for Android.
In addition to technical instruction, this book explores the artistic and creative possibilities that you can
unlock with Processing for Android. You will be inspired by the diverse range of examples and full projects
showcased throughout the book, demonstrating how Processing can be used to create visually sophisticated
and conceptually rich mobile apps. Whether you are interested in generative art, data visualization, or
immersive experiences in VR or AR, the combination of Processing and Android provides an exciting
playground for your creativity.
This second edition is a major update of the book, with all code examples and figures from the first
edition carefully revised to reflect the latest changes in the Processing software and the Android platform.
Furthermore, several of the full projects at the end of each section were significantly rewritten to make them
more concise and easier to follow while still highlighting the key techniques of their corresponding section.
An entire new section comprising three chapters devoted to VR and AR was added, as well as a chapter
where shader programming in Processing is described in depth and illustrated with many code examples
from basic to advanced.
As you progress through the chapters, you will not only acquire the technical skills to create Android
apps with Processing but also gain a deeper understanding of programming concepts and computational
thinking. This book encourages you to experiment, explore, and push the boundaries of what is possible
with Processing for Android.

xxi
■ Introduction

So if you are ready to embark on an exciting journey into the world of creative coding and mobile app
development, Processing for Android is your gateway. Let’s dive in and unlock the potential of this powerful
combination of art, technology, and Android!
Happy coding and creating!

xxii
CHAPTER 1

Getting Started with the Android


Mode

In this chapter, we will introduce the Processing software and its Android mode, the community behind
these projects, and how we can start using Processing to create apps for Android devices.

What Is the Processing Project?


The Processing project is a community initiative with the goals of sharing knowledge, fostering education,
and promoting diversity in computer programming. The Processing software is a central part of this
initiative, guided by the Processing Foundation (https://processingfoundation.org/), whose main
mission is to “promote software literacy within the visual arts and visual literacy within technology-related
fields – and to make these fields accessible to diverse communities.” This community grew off the original
Processing software, a programming language and environment created in 2001 by Casey Reas and
Ben Fry at the MIT Media Lab as a teaching and production tool in computational arts and design. The
Processing software has been continuously developed since then and is available for download at https://
processing.org/, with its source code released under free software licenses. From now on, we will simply
refer to Processing when talking about the Processing software.
Processing consists of two complementary parts: the Processing programming language and the
Processing development environment. Together, they form a “software sketchbook” that allows users to
quickly implement, or “sketch,” their ideas with code while also providing them with enough room to
iteratively refine these ideas into fully developed projects. Processing has been used to create many beautiful
and inspiring works in generative art, data visualization, and interactive installations, just to mention a few
areas. There are several online resources listing some of these works, including CreativeApplications.Net
(www.creativeapplications.net/category/processing/), OpenProcessing (https://openprocessing.
org/), and “For Your Processing” on Tumblr (https://fyprocessing.tumblr.com/).

The Processing Language


The Processing language is a set of functions, classes, and constants we can use to program screen drawing,
data input/output, and user interaction, originally implemented in Java and now available in Python,
JavaScript, and other programming languages. The core team of people behind Processing (https://
processing.org/people/) has carefully constructed this language, technically called “Application
Programming Interface” or API, to simplify the development of graphical and interactive applications by
means of a simple and consistent naming convention, unambiguous behavior, and a well-defined scope.

© Andrés Colubri 2023 3


A. Colubri, Processing for Android, https://doi.org/10.1007/978-1-4842-9585-4_1
Chapter 1 ■ Getting Started with the Android Mode

The Java implementation of the Processing language has been part of Processing dating back to 2001
and encompasses around 200 functions. A defining feature of this language is that it offers us the possibility
of creating interactive graphics with very little code. It also includes several simplifications with respect
to the Java language, with the purpose of making it easier to teach to people who are not familiar with
computer code. The following program illustrates these features of the Processing language:

color bg = 150;

void setup() {
  size(200, 200);
}

void draw() {
  background(bg);
  ellipse(mouseX, mouseY, 100, 100);
}

The output of this program is a window of 200 by 200 pixels, with a white circle that follows the
movement of the mouse on a gray background. The functions setup() and draw() are present in most
Processing programs, and they drive its “drawing loop.” All the initialization of the program should take place
in setup(), which is called just once when the program starts up. The draw() function, containing all the
drawing instructions, is then called continuously several times per second (by default, 60 times) so that we
can animate the graphical output of our program. Another term specific to Processing is the word “sketch,”
which refers to a Processing program by highlighting the concept of code sketching we mentioned earlier.

The Processing Development Environment


The Processing Development Environment (PDE) is the software that provides users with a minimalistic
code editor to write, run, and refine Processing sketches (Figure 1-1). The PDE also incorporates an
uncluttered user interface to handle all the sketches created with it and to add libraries and other external
components that extend the core functionality of the PDE, such as p5.js, Python, or Android modes.
The ease of use and simplicity of the PDE and the Processing language are the key elements to realize
the concept of a “code sketchbook.” A stumbling block for many people wanting to start working with
code is the complexity of a modern development environment like Eclipse or IntelliJ, in terms of a lengthy
installation and an overwhelming user interface. In contrast, the PDE addresses these issues by providing
an easy install process and a clean interface, while the simple structure of a Processing sketch enables users
to obtain visual feedback rapidly. Processing’s aim is to enable an iterative development process analogous
to sketching with pen and paper, where we can start with a simple idea and then refine by continuous code
sketching.

■■Note The Processing language can be used outside of the PDE, for example, in advanced Integrated
Development Environments or IDEs, such as Eclipse or IntelliJ. All of Processing’s drawing, data, and interaction
API can be used when writing a program with these IDEs; but many of the simplifications that the Processing
language has with respect to Java will be lost.

Those of us who are familiar with Java have probably noticed that the code shown in Listing 1-1 is not
a valid Java program. For example, there is no explicit definition of a main class encapsulating all the code,
nor the additional instructions required in Java to initialize the graphical display and the user input. This

4
Chapter 1 ■ Getting Started with the Android Mode

sketch needs to be run inside the PDE, which applies a “pre-processing” step to the Processing code to
convert it into a complete Java program. However, this step occurs behind the scenes, and we do not need to
worry about it at all. Only when using an advanced IDE like those noted previously, we need to make sure to
incorporate the Processing API into a syntactically correct Java program.
We can download the latest version of Processing from the main website (https://processing.org/
download). As we pointed out in the previous paragraph, installation is very easy, only requiring unpacking
the zip (on Windows and macOS) or tgz (on Linux) package that contains the PDE and all other core files. We
should be able to then run the PDE without any additional steps, from whichever location inside the home
or applications folder.

Figure 1-1. The Processing Development Environment showing a running sketch in the Java mode

The PDE saves our sketches inside a “sketchbook” folder. Each sketch is stored in a subfolder inside
the sketchbook, which in turn contains one or more source code files with the pde extension. By default,
Processing creates the sketchbook inside the documents folder located in the user’s account (e.g., /Users/
andres/Documents/Processing on macOS), but we can change this location by selecting the desired
sketchbook folder in the Preferences window, available under the “Processing ➤ Settings” menu on macOS
and “File ➤ Settings” on Windows and Linux (Figure 1-2). Notice the sketchbook location at the top.

5
Chapter 1 ■ Getting Started with the Android Mode

Figure 1-2. The Preferences window on macOS

Extending Processing
As we mentioned at the beginning, the Processing project is not only the PDE and the language but also, and
very importantly, the community built around the use of the software and the “goal to empower people of all
interests and backgrounds to learn how to program and make creative work with code, especially those who
might not otherwise have access to these tools and resources,” as stated in the home page of the Processing
Foundation. Thanks to Processing’s open nature and modular architecture, many people have contributed
with improvements and extensions to the “core” software over the years. These contributions fall within one
of the following four categories:
• Libraries: Modules (comprising one or more Java code files built into a jar package
and additional documentation and example files) that make it possible to access
new functionality in the sketches, for example, OpenCV library for computer vision
applications on PC or Mac computers, or TensorFlow Lite for running machine
learning tasks on Android.

6
Chapter 1 ■ Getting Started with the Android Mode

• Programming modes: Alternative code editors and related PDE customizations that
allow using an entirely different language within the PDE, for example, the Android
mode itself! We will see in the next sections of this chapter how to install the Android
mode on top of the default PDE.
• Tools: Applications that can only run from Processing and provide specific
functionality to help while writing code, debugging, and testing the sketch, for
example, the color picker (discussed in Chapter 2).
• Examples: Packages of contributed code sketches that can be used as learning
material or reference, for example, the sketches from the Learning Processing book
by Daniel Shiffman (http://learningprocessing.com/).
The possibility of extending Processing through contributed libraries, modes, tools, and examples has
enabled the growth of Processing into application domains that were not part of the original software, such
as mobile apps, computer vision, and physical computing, while keeping the core functionality simple and
accessible for new programmers.

The Contribution Manager


By default, Processing includes one default mode, Java, where we can write and run sketches on Windows,
Mac, and Linux computers using the Java implementation of the Processing language. Processing also
bundles several “core” libraries, some of which are OpenGL (for drawing hardware-accelerated 2D and 3D
scenes), PDF (to export graphics as PDF files), and data (which allows handling data files in formats such as
CSV and JSON).
We can use the Contribution Manager (CM) to install additional contributions, which makes the entire
process very seamless. A screenshot of the CM is shown in Figure 1-3. The CM has five tabs, the first four
for each type of contribution – libraries, modes, tools, and examples – and the fifth tab for updates. All the
contributions that are registered by their authors in a central repository are accessible through the CM and
can also be updated through the CM when new versions become available.

■■Note Contributions that are not available through the CM can be installed manually. You need to download
the package containing the library, mode, tool, or examples, typically in zip format, and extract it into the
sketchbook folder. There are separate subfolders for libraries, modes, tools, and examples. See https://
processing.org/reference/libraries/ for more info.

7
Chapter 1 ■ Getting Started with the Android Mode

Figure 1-3. The Contribution Manager in Processing showing the Modes tab

Processing for Android


Processing for Android, just like the Processing software itself, is also several things. Primarily, it is a
community effort that started in 2009 with the purpose of supporting the development of Android apps
using Processing and translating some of the concepts of the project to the context of mobile apps: iterative
sketching, simplicity, and accessibility.
If we look more closely into the technical details, Processing for Android is made of two parts: the
processing-android library and the custom PDE programming mode itself. The library is the package
that contains all the functions of the Processing API but re-implemented for the Android platform. The
Android mode provides a customized version of the PDE that allows us to write Processing code and run
it on an Android device or in the emulator. The Android mode includes the processing-android library,
which we need for our Processing code to run without errors. However, we don’t need to worry about these
distinctions at this point, since Processing will let us install and use the Android mode and will make sure
that all the required libraries are placed in the right places. But knowing about the difference between the
android library and the mode would be important for those readers who may be planning to use Processing
for Android in more advanced applications.

■■Note The processing-android library can be imported from an IDE like Android Studio, allowing using all the
Processing functionality in a regular Android app. This advanced use is covered in Appendix A.

8
Chapter 1 ■ Getting Started with the Android Mode

Installing the Android Mode


Once we have installed Processing in our computer, we should be able to open the PDE by running the
Processing application and then to install the most recent release of the Android mode through the CM. The
mode also requires the Android Software Development Kit (SDK) to work. The Android SDK is the set
of libraries, tools, documentation, and other supporting files provided by Google to develop and debug
Android apps. So let’s follow these next steps to install the Android mode and, if needed, the SDK:
1. Open the CM by clicking the “Manage Modes…” option that appears in the drop-
down menu in the upper-right corner of the PDE (Figure 1-4).
2. Select the entry for the Android mode in the Modes tab and click the
“Install” button.
3. After installation is complete, close the CM and switch into the Android mode
using the same drop-down menu from Figure 1-4.
When switching into the Android mode for the first time, it will ask us to locate or download the
Android SDK (Figure 1-5). Because the SDK is very large (up to several GBs), it could be a good idea to use
the one that is already installed to save disk space. However, if that SDK is also used by another development
tool, such as Android Studio, it may get updated or changed outside Processing, which may lead to
incompatibilities with the Android mode. If that’s the case, then it may be better to allow Processing to
download and install its own copy of the Android SDK, which will not interfere with the SDK in use by other
development tools. If no valid Android SDK is detected, Processing will ask to manually locate an SDK, or
automatically download it (Figure 1-5).

Figure 1-4. Opening the Contribution Manager to add a new mode

■■Note Version 4.5+ of the Android mode requires Android version 13, corresponding to API level 33
(https://source.android.com/docs/setup/about/build-numbers). The mode’s automatic SDK
download will retrieve this version from the Google servers.

9
Chapter 1 ■ Getting Started with the Android Mode

Figure 1-5. Pop-up dialog in the Android mode allowing us to search for an existing SDK in our computer
manually or download a new copy of the SDK from Google’s servers

Finally, pre-releases of the Android mode, as well as older versions that are no longer are available
through the CM, are all deposited in the GitHub releases page (https://github.com/processing/
processing-android/releases). So, if we are interested in trying these versions out, we could install
them manually by downloading the corresponding zip file and extracting it into the modes folder in the
Processing’s sketchbook.

Interface of the Android Mode


The editor in the Android mode is very similar to that of the Java mode. The toolbar contains the play and
stop buttons to launch a sketch (on a connected device) and to stop its execution (on the device or in the
emulator). Code autocompletion and integrated debugger in the editor are available as well. The main
menu contains several Android-specific options as well (Figure 1-6). The “File” menu has options to export
the current sketch as a package ready to upload to the Google Play Store, or as a project that can be opened
with Android Studio. The “Sketch” menu contains separate options to run the sketch on a device or in the
emulator, as well as a separate “Android” menu containing several options, among them the type of output
to target with the sketch – regular app, wallpaper, watch face, VR, or AR app – and the list of Android devices
currently connected to the computer. We will cover all these options in the next chapters!

10
Chapter 1 ■ Getting Started with the Android Mode

Figure 1-6. Android-specific options in the interface of the Android mode

11
Chapter 1 ■ Getting Started with the Android Mode

Running a Sketch on a Device


Once we have written some sketch code with the PDE, we can then run it on an Android phone, tablet, or
watch. The first thing we need to do is to make sure that “USB Debugging” is turned on on our device. The
process to do this varies by device and which version of the Android OS we have installed on it. In most of
the cases, this setting is in the Developer Options, under system Setting. On Android 4.2 and higher, the
Developer Options are hidden by default, but we can enable them by following these instructions:
1. Open the Settings app.
2. Scroll to the bottom and select About phone.
3. Scroll to the bottom and tap Build number seven times.
4. Return to the previous screen to find Developer Options near the bottom.
After turning USB Debugging on (which we need to do only once), we need to connect the device to
the computer through the USB port. Processing will then try to recognize it and add it to the list of available
devices in the “Android” menu.

■■Note Version 4.5+ of the Android mode only supports devices that are running Android 4.2 (API level 17)
or newer.

Let’s use the code in Listing 1-1 as our first Processing for Android sketch. It is okay if we do not
understand every line of code in it, as we will go over the Processing API in detail in the next chapters. It
simply draws a black square in the half of the screen that receives a touch press.

Listing 1-1. Our first Processing for Android sketch

void setup() {
  fill(0);
}

void draw() {
  background(204);
  if (mousePressed) {
    if (mouseX < width/2) rect(0, 0, width/2, height);
    else rect(width/2, 0, width/2, height);
  }
}

It is possible to have several devices connected simultaneously to the computer, but only one can be
selected in the Devices menu as the “active” device, which will be where our sketch will be installed and run.
Figure 1-7 shows our first sketch already loaded in the PDE and the selected device to run it on.

12
Chapter 1 ■ Getting Started with the Android Mode

Figure 1-7. Selecting the device to run the sketch on

After we have picked the active device, we can hit the run button or select “Run on Device” under the
“Sketch” menu. Then, we should see some messages scrolling down in the PDE’s console, while Processing
compiles the sketch, packages it as an Android app, and installs it on the device. One important detail is
that the computer needs to be connected to the Internet the first time we run a sketch. The reason is that
Processing uses a tool called Gradle to build the app from the sketch’s source code, but Gradle is not part of
the initial Android mode installation. The Android mode will automatically download the Gradle tool the
first time it tries to run a sketch. It can be offline when running sketches after the first time. If everything goes
well, the sketch should launch and show up on the screen of the device, as shown in Figure 1-8.

Figure 1-8. Running a sketch on a connected phone

13
Chapter 1 ■ Getting Started with the Android Mode

■■Note If we are running Windows, we may need to install a special USB driver to connect to the device
(https://developer.android.com/studio/run/oem-usb.html). In the case of Linux, we may need
to install some additional packages depending on the distribution we are using (https://developer.
android.com/studio/run/device.html). Also, make sure that the USB connection is not configured as
“Charge Only.”

Running a Sketch in the Emulator


If we do not have a device to run our sketch on, we can still use the emulator. The emulator is a program that
creates a “software replica” of a physical device that runs on our computer. This replica is called an Android
Virtual Device (AVD), and even though it is generally slower than a real device, it can also be useful to test
our sketch on hardware we don’t currently have.
The first time we run a sketch in the emulator, Processing will download the “system image” containing
all the information needed to emulate the AVD in our computer (Figure 1-9). However, it will initially ask us
if we want to use the “ARM” or the “x86” images. The reason for this is that Android devices use ARM CPUs
(central processing units), while our desktop or laptop computers often have x86 CPUs (ARM and x86 denote
different “architectures” that CPUs can have to process the instructions from our code). When emulating an
AVD with an ARM image on an x86 CPU, the emulator will convert ARM instructions into x86 instructions
one by one, which could be slow. But if we use the x86 image on an x86 CPU, our computer will be able to
simulate the AVD’s CPU directly and therefore much faster. One drawback of using x86 images is that we
must install an additional software on Mac or Windows called HAXM. Processing already downloaded
HAXM together with the Android SDK, so it will install it for us in case we decide to use x86 images.
However, with the ARM-based Mac computers (M1, M2, etc.), we do not have this issue since their CPUs
have the same ARM architecture as the emulated devices.
We should also keep in mind that HAXM is only compatible with Intel processors, so the emulator won’t
work with x86 images if our computer has an AMD CPU. Linux has its own AVD acceleration system and
does not require HAXM, so we can use x86 images on a Linux computer with an AMD CPU. We would need
to perform some extra configuration steps though, which are described here: https://developer.android.
com/studio/run/emulator-acceleration.html#vm-linux.

14
Chapter 1 ■ Getting Started with the Android Mode

Figure 1-9. System image download dialog in the Android mode

After finishing the download, which can take several minutes depending on our Internet connection
(the system images for the emulator are over 1GB in size), Processing will boot up the emulator and then will
launch the sketch in it. Once our Listing 1-1 is running in the emulator, it should look like the one shown in
Figure 1-10.

15
Chapter 1 ■ Getting Started with the Android Mode

Figure 1-10. Running our sketch in the emulator

Summary
In this first chapter, we have learned what the Processing project and software are and how we can
use Processing to create apps, thanks to its Android mode. As we saw, some of the main features of the
Processing software are its minimal interface and the simple structure of a program, called a “sketch.”
These features allow us to start writing, testing, and refining our own sketches, either on a device or in the
emulator, very quickly.

16
CHAPTER 2

The Processing Language

If you are not familiar with the Processing language, read this chapter for an introduction to how to create 2D
shapes, use geometry transformations and color, and handle touchscreen input in Processing. The chapter
ends with a step-by-step example of a drawing sketch, which we will use in Chapter 3 to learn how to export
and upload an app created with Processing to the Play Store.

A Programming Sketchbook for Artists and Designers


As we learned in the first chapter, the Processing language, together with the Processing Development
Environment (PDE), makes it easier for users who are new to programming to start creating interactive
graphics. The language has been designed to be minimal and simple for learning and yet expressive enough
to create many kinds of code-based projects, including generative art, data visualization, sound art, film, and
performance. It includes around 200 functions across different categories: drawing, interaction, typography,
etc., as well as several classes that help with the handling of form, color, and data.
The creators of Processing were also inspired by the concept of sketching: when we have an idea and
use a paper sketchbook to draw it down, we often create many iterations or variations of the original idea as
we refine it or make changes. Processing supports a similar way of working with computer code by making it
easy for us to obtain visual feedback from the code so we can quickly iterate and refine a programming idea.
This is why you will see Processing described as a “coding sketchbook” in many places. The next section
describes the basic structure in Processing that allows us to sketch with code by getting animated graphics to
the computer screen.

The Structure of a Processing Sketch


Most of the time, we want our Processing sketches to run nonstop to animate graphics on the screen and
keep track of user input. This means that instead of running our code just one time, Processing will continue
running it repeatedly so that we can animate the visual output by programming how it changes from one
frame to the next and how it’s affected by any interaction of the user with the computer (desktop, laptop,
phone, etc.). Because of that, we call these “interactive sketches” in contrast with “static sketches” that run
just one time (we can create these with Processing too, but for now, we will focus on interactive sketches).

■■Note All the code examples in this chapter can be run in the Java or the Android mode, since they don’t
rely on any features of the Processing language specific to either mode. This chapter also assumes some basic
knowledge of programming, including conditions (if/else), loops (for/while), use of variables, and organizing our
code with functions, so we will not go over those concepts.

© Andrés Colubri 2023 17


A. Colubri, Processing for Android, https://doi.org/10.1007/978-1-4842-9585-4_2
Chapter 2 ■ The Processing Language

The structure of an interactive sketch in Processing is uncomplicated: it requires two functions, one
called setup() and the other, draw(). The setup() function will contain the code that we need to run only
one time at the beginning to set things up, and the draw() function will contain the code that generates the
visual output of our sketch and Processing will run every time it has to draw or “render” a new frame on the
screen of the computer, typically 60 times per second. Let’s use these functions to create our first animated
sketch in Processing!
A simple animation could be moving a vertical line from left to right across the screen. We do just
that in Listing 2-1, where we have both setup() and draw(). In the setup() function, we run a few initial
tasks: first, we set the size of the output window or “canvas” where Processing will draw to. In this case, we
are initializing the canvas with fullScreen(), which makes it as large as the entire screen of the computer
(or phone). Then we set the parameters of our line with strokeWeight(2), which sets the line weight
(or thickness) to 2 pixels, and stroke(255), which sets the line color to white (we will go over the use of
numbers to set color values later in this chapter). Finally, we set the value of the variable x, declared at the
top of the code, to zero. This variable holds the horizontal position of the line, and setting to zero means
placing it on the left side of the screen (also later in the chapter we will discuss about screen coordinates
in more detail). In the draw() function, we start by setting the color of the entire canvas to a tone of gray
with background(50) and then draw the line with line(x, 0, x, height). The line() function takes four
parameters: the first two (x, 0) correspond to the first point of the line, and the last two (x, height), to the
second point, so by calling this function, we get Processing to draw a line from (x, 0) to (x, height). The word
“height” is a “constant” in Processing that always represents the height of the canvas and therefore cannot
be used as a variable name by us (the Processing language includes several more constants like this; we
will learn about some of them later in this and following chapters). Because the two points have the same x
coordinate, we will get a vertical line running between the top (0) and the bottom (height) of the screen. The
last two lines of code in the draw() function control the animation: by doing x = x + 1, we are increasing
the value of the horizontal position of our vertical line by one unit; this means that the line will move to the
right one pixel per frame. If we keep adding 1 to x enough times, its value will eventually be larger than the
width of our canvas, and the line will no longer be visible. In that case, we put the line back at the left edge
of the canvas by checking if x is larger than the width and resetting to zero with if (width < x) x = 0. The
output of this sketch running on a phone is shown in Figure 2-1.

Listing 2-1. A sketch that draws a vertical line moving horizontally across the screen

int x;

void setup() {
  fullScreen();
  strokeWeight(2);
  stroke(255);
  x = 0;
}
void draw() {
  background(50);
  line(x, 0, x, height);
  x = x + 1;
  if (width < x) x = 0;
}

18
Chapter 2 ■ The Processing Language

Figure 2-1. Output of the animated line sketch, running on a Pixel 4a phone

This continuous sequence of calls to the draw() function that Processing does automatically in any
interactive sketch is called the “animation loop.” Processing calls the draw() function during the animation
loop at a default frame rate of 60 frames per second; however, we can change this default using the function
frameRate(). For instance, if we add frameRate(1) in setup(), then the animation loop will draw 1 frame
per second.
Sometimes, we may need to stop the animation loop at some point. We can use the noLoop()
function for doing this and then the loop() function to resume the animation afterward. Processing
also has a boolean (logical) constant named looping, which is true or false depending on whether the
sketch is running the animation loop. We can build on top of our previous sketch to pause/resume the
line movement if the user clicks the mouse (on a desktop/laptop) or taps the touchscreen (on a phone),
as shown in Listing 2-2. This code introduces another new function from the Processing language called
mousePressed(), where we can put code that should be run only when the user presses the mouse (or taps
the touchscreen). We will see a few more interaction-handling examples in this chapter and then will
deep-dive into touchscreen interaction more specific for mobile development in Chapter 5.

Listing 2-2. Pausing/resuming the animation loop

int x = 0;

void setup() {
  fullScreen();
  strokeWeight(2);
  stroke(255);
}

void draw() {
  background(50);
  line(x, 0, x, height);
  x = x + 1;
  if (width < x) x = 0;

19
Chapter 2 ■ The Processing Language

void mousePressed() {
  if (looping) {
    noLoop();
  } else {
    loop();
  }
}

We can also write static sketches without setup/draw, which are useful if we only want to create a
fixed composition that does not need to be animated. Processing runs the code in these sketches only
one time. Listing 2-3 contains a simple static sketch that draws the white circle in Figure 2-2. Here, we
use the function ellipse() to draw a circle centered at coordinates (400, 400) and width and height equal
to 300 pixels (by setting different width and height values, we would get an ellipse of any proportions
we like.) We use the fill() function to paint the interior of the circle with a purple color. We also use the
size() function, which allows us to set the width and height of the output canvas. We apply this function
instead of fullScreen() if we only want to use an area of the device’s screen for drawing and interaction.
Processing will paint the pixels outside of the drawing area with a light gray color, as seen in Figure 2-2,
and we will not be able to change this color. Tapping on this area will not result in any interaction events
with the sketch.

Listing 2-3. Static sketch without setup() and draw() functions

size(1000, 1000);
background(255);
fill(150, 100, 250);
ellipse(500, 500, 400, 400);

Figure 2-2. Output of the static sketch in Listing 2-3

D
 rawing with Code
We just saw in the examples from the previous section how the Processing language allows us to “draw with
code.” Even though these examples were very simple, they already point to some general concepts for code-
based drawing in Processing. First, we need to specify the coordinates of the elements we want to draw on
the screen. Second, there are functions, such as line() and ellipse(), that allow us to draw various shapes

20
Chapter 2 ■ The Processing Language

by setting numerical values that define their form, size, and position. Third, we can set the visual “style” of
these shapes (e.g., stroke and fill color) by using functions like fill(), stroke(), and strokeWeight(). In
the next sections, we will delve into these techniques in more depth to learn how to draw different kinds
of shapes.

Coordinates
One of the most important concepts in code-based drawing is knowing how to locate points on the
computer screen using numeric coordinates. Processing draws the graphical output of our code into a
rectangular grid of pixels, numbered from 0 to width-1 along the horizontal direction (the x axis) and 0 to
height-1 along the vertical direction (the y axis), illustrated in Figure 2-3. As we saw before, we can set the
precise width and height of this grid by using the size() function with our desired values as arguments. If we
use the fullScreen() function instead, then the width and height of the grid will be automatically set to the
width and height of the entire screen of our device.

x-axis
0 1 2 ... width-1
0
1
2
y-axis

...
height-1
pixel (1, 2)

Figure 2-3. Diagram of the screen’s pixels

The X coordinates run from left to right, while the Y coordinates run from top to bottom. So the pixel
(0, 0) represents the upper left corner of the screen, and the pixel (width-1, height-1) represents the lower
right corner. The arguments of most of the 2D drawing functions in Processing refer to the pixel coordinates
of the screen. Processing also offers two internal constants, conveniently called width and height, that hold
the values we set in the size() function or determined automatically when using fullScreen(). The following
“toy” sample code, where we set a very small output size so we can see each single pixel, would produce the
output in Figure 2-4 (although the actual output of running this code on a computer would be so small that
we would likely not be able to differentiate its parts):

size(12, 8);
stroke(200, 0, 0);
fill(100, 200, 100);
rect(2, 1, width - 1, height - 2);

21
Chapter 2 ■ The Processing Language

Figure 2-4. Pixels covered by a stroked rectangle in Processing

We should set the size of the shapes we draw with Processing according to the constraints of the screen
size. In general, it is recommended to use the width and height constants when referring to the size of the
screen instead of entering predetermined values, because the width and height of our output will likely
change between devices, especially if we use the fullScreen() function. Even when we set the width and
height in the size() function, if we later change the arguments of size(), we would then need to go through
the code updating any references to the original width and height values. This won’t be necessary if we use
the width and height constants. The code in Listing 2-4 provides a demonstration of this technique. With this
code, we generate a rectangular checkboard of 4×4 by drawing rectangles that have size equal to a quarter
of the width and height of the output canvas. The result is shown in Figure 2-5, as it would appear on a Pixel
4a phone. If we change the values in size(1000, 1000) to something else, the result would still be a grid of 4×4
rectangles, because those rectangles will be resized automatically according to the value of width and height.

Listing 2-4. Using screen coordinates

size(1000, 1000);
noStroke();
fill(255);
background(0);
rect(0, 0, width/4, height/4);
rect(width/2, 0, width/4, width/4);
rect(width/4, height/4, width/4, height/4);
rect(3*width/4, height/4, width/4, height/4);
rect(0, height/2, width/4, height/4);
rect(width/2, height/2, width/4, width/4);
rect(width/4, 3*height/4, width/4, height/4);
rect(3*width/4, 3*height/4, width/4, height/4);

22
Chapter 2 ■ The Processing Language

Figure 2-5. Using screen coordinates to draw a grid of rectangles

Form
All the shapes we draw in Processing have a form in two or three dimensions. One way to think of a shape
is as a perimeter or boundary that separates the inside of the shape from the outside. We already saw that
Processing includes functions to draw common shapes such as rectangles or ellipses by providing the
coordinates of their center and size. Processing will automatically construct the perimeter of these shapes
for us. But to construct the perimeter of an arbitrary shape in Processing, we need to explicitly provide
the coordinates of the vertices alongside the perimeter. We can do this by listing the vertices between the
beginShape() and endShape() functions as shown in Listing 2-5, whose output is presented in Figure 2-6.

Listing 2-5. Using beginShape() and endShape()

size(600, 300);

strokeWeight(2);

// Draw a rectangle in the bottom of the screen


beginShape();
vertex(5, 250);
vertex(590, 250);
vertex(590, 290);
vertex(5, 290);
endShape(CLOSE);

// Draw a star-like shape in right side


beginShape();
vertex(330, 25);
vertex(390, 90);
vertex(510, 10);
vertex(460, 120);
vertex(510, 270);
vertex(410, 180);
vertex(310, 270);
vertex(360, 150);
endShape(CLOSE);

23
Chapter 2 ■ The Processing Language

// Draw a small triangle right above the star shape


beginShape();
vertex(350, 30);
vertex(410, 30);
vertex(390, 75);
endShape();

ellipse(70, 80, 70, 70);

Figure 2-6. Composition created by drawing several shapes

We can learn a few important lessons from this example. First, shapes are drawn on top of each other
according to the order they appear in the code; this is why the rectangle in the bottom, which is the first
shape to be drawn, is partially obstructed by the star-like shape, which is drawn afterwards. Second, shapes
can be open or closed. When a shape is open, the stroke line that goes around its perimeter will not connect
the last and the first vertices, as we can see in the triangle shape. But if we add the argument CLOSE in the
endShape() function, which is the case for the two first shapes, the stroke line will wrap around and result in
an uninterrupted boundary delineating the shape. Third, we can combine common (also called “primitive”)
shapes drawn with functions like ellipse() or rect() with arbitrary shapes drawn with beginShape/endShape.
By applying some basic trigonometry, we can draw shapes that are regular polygons. The vertices
alongside the perimeter of a regular polygon have coordinates x = r * cos(a) and y = r * sin(a), where
r is the radius of the circumference, sin() and cos() are the sine and cosine functions, and a is an angle
between 0 and 360 degrees (for a great intro/refresher about trigonometry, check Chapter 3 from Daniel
Shiffman’s book The Nature of Code, available online at https://natureofcode.com/book/chapter-3-
oscillation/). For example, in Listing 2-6, we draw three regular polygons: a square, a hexagon, and an
octagon.

Listing 2-6. Drawing regular polygons

size(900, 300);

fill(200, 100, 100);


beginShape();
for (float angle = 0; angle <= TWO_PI; angle += TWO_PI/4) {
  float x = 150 + 100 * cos(angle);
  float y = 150 + 100 * sin(angle);
  vertex(x, y);
}
endShape(CLOSE);

24
Random documents with unrelated
content Scribd suggests to you:
Hugo, following these directions, concluded a successful search.
"Right," he said. "Ladder, long, wooden, for purposes of climbing,
one. Correct as per memo. Now what?"
"Put it up."
"Right."
"And hold it very carefully."
"Esteemed order booked," said Hugo. "Carry on."
"Are you sure you are holding it carefully?"
"As in a vise."
"Well, don't let go."
Mr. Carmody, dying a considerable number of deaths in the process,
descended. He found his nephew's curiosity at close range even
more acute than it had been from a distance.
"What on earth were you doing up there?" said Hugo, starting again
at the beginning.
"Never mind."
"But what were you?"
"If you wish to know, a rung broke and the ladder slipped."
"But what were you doing on a ladder?"
"Never mind!" cried Mr. Carmody, regretting more bitterly than ever
before in his life that his late brother Eustace had not lived and died
a bachelor. "Don't keep saying What—What—What!"
"Well, why?" said Hugo, conceding the point. "Why were you
climbing ladders?"
Mr. Carmody hesitated. His native intelligence returning, he
perceived now that this was just what the great public would want to
know. It was little use urging a human talking machine like his
nephew to keep quiet and say nothing about this incident. In a
couple of hours it would be all over Rudge. He thought swiftly.
"I fancied I saw a swallow's nest under the eaves."
"Swallow's nest?"
"Swallow's nest. The nest," said Mr. Carmody between his teeth, "of
a swallow."
"Did you think swallows nested in July?"
"Why shouldn't they?"
"Well, they don't."
"I never said they did. I merely said...."
"No swallow has ever nested in July."
"I never...."
"April," said our usually well-informed correspondent.
"What?"
"April. Swallows nest in April."
"Damn all swallows!" said Mr. Carmody. And there was silence for a
moment, while Hugo directed his keen young mind to other aspects
of this strange affair.
"How long had you been up there?"
"I don't know. Hours. Since half-past five."
"Half-past five? You mean you got up at half-past five to look for
swallows' nests in July?"
"I did not get up to look for swallows' nests."
"But you said you were looking for swallows' nests."
"I did not say I was looking for swallows' nests. I merely said I
fancied I saw a swallow's nest...."
"You couldn't have done. Swallows don't nest in July.... April."
The sun was peeping over the elms. Mr. Carmody raised his
clenched fists to it.
"I did not say I saw a swallow's nest. I said I thought I saw a
swallow's nest."
"And got a ladder out and climbed up for it?"
"Yes."
"Having risen from couch at five-thirty ante meridian?"
"Will you kindly stop asking me all these questions."
Hugo regarded him thoughtfully.
"Just as you like, Uncle. Well, anything further this morning? If not,
I'll be getting along and taking my dip."

III
"I say, Ronnie," said Hugo, some two hours later, meeting his friend
en route for the breakfast table. "You know my uncle?"
"What about him?"
"He's loopy."
"What?"
"Gone clean off his castors. I found him at seven o'clock this morning
sitting on a second-floor window sill. He said he'd got up at five-thirty
to look for swallows' nests."
"Bad," said Mr. Fish, shaking his head with even more than his usual
solemnity. "Second-floor window sill, did you say?"
"Second-floor window sill."
"Exactly how my aunt started," said Ronnie Fish.
"They found her sitting on the roof of the stables, playing the ukulele
in a blue dressing gown. She said she was Boadicea. And she
wasn't. That's the point, old boy," said Mr. Fish earnestly. "She
wasn't. We must get you out of this as quickly as possible, or before
you know where you are you'll find yourself being murdered in your
bed. It's this living in the country that does it. Six consecutive months
in the country is enough to sap the intellect of anyone. Looking for
swallows' nests, was he?"
"So he said. And swallows don't nest in July. They nest in April."
Mr. Fish nodded.
"That's how I always heard the story," he agreed. "The whole thing
looks very black to me, and the sooner you're safe out of this and in
London, the better."

IV
At about the same moment, Mr. Carmody was in earnest conference
with Mr. Molloy.
"That man you were telling me about," said Mr. Carmody. "That
friend of yours who you said would help us."
"Chimp?"
"I believe you referred to him as Chimp. How soon could you get in
touch with him?"
"Right away, brother."
Mr. Carmody objected to being called brother, but this was no time
for being finicky.
"Send for him at once."
"Why, have you given up the idea of getting that stuff out of the
house yourself?"
"Entirely," said Mr. Carmody. He shuddered slightly. "I have been
thinking the matter over very carefully, and I feel that this is an affair
where we require the services of some third party. Where is this
friend of yours? In London?"
"No. He's right around the corner. His name's Twist. He runs a sort of
health-farm place only a few miles from here."
"God bless my soul! Healthward Ho?"
"That's the spot. Do you know it?"
"Why, I have only just returned from there."
Mr. Molloy was conscious of a feeling of almost incredulous awe. It
was the sort of feeling which would come to a man who saw miracles
happening all around him. He could hardly believe that things could
possibly run as smoothly as they appeared to be doing. He had
anticipated a certain amount of difficulty in selling Chimp Twist to Mr.
Carmody, as he phrased it to himself, and had looked forward with
not a little apprehension to a searching inquisition into Chimp Twist's
bona fides. And now, it seemed, Mr. Carmody knew Chimp
personally and was, no doubt, prepared to receive him without a
question. Could luck like this hold? That was the only thought that
disturbed Mr. Molloy.
"Well, isn't that interesting!" he said slowly. "So you know my old
friend Twist, do you?"
"Yes," said Mr. Carmody, speaking, however, as if the
acquaintanceship were not one to which he looked back with any
pleasure. "I know him very well."
"Fine!" said Mr. Molloy. "You see, if I thought we were getting in
somebody you knew nothing about and felt you couldn't trust, it
would sort of worry me."
Mr. Carmody made no comment on this evidence of his guest's nice
feeling. He was meditating and did not hear it. What he was
meditating on was the agreeable fact that money which he had been
trying so vainly to recover from Doctor Twist would not be a dead
loss after all. He could write if off as part of the working expenses of
this little venture. He beamed happily at Mr. Molloy.
"Healthward Ho is on the telephone," he said. "Go and speak to
Doctor Twist now and ask him to come over here at once." He
hesitated for a moment, then came bravely to a decision. After all,
whatever the cost in petrol, oil, and depreciation of tires, it was for a
good object. More working expenses. "I will send my car for him," he
said.
If you wish to accumulate, you must inevitably speculate, felt Mr.
Carmody.

CHAPTER VII

I
The strange depression which had come upon Pat in the shop of
Chas. Bywater did not yield, as these gray moods generally do, to
the curative influence of time. The following morning found her as
gloomy as ever—indeed, rather gloomier, for shortly after breakfast
the noblesse oblige spirit of the Wyverns had sent her on a reluctant
visit to an old retainer who lived—if you could call it that—in one of
the smaller and stuffier houses in Budd Street. Pensioned off after
cooking for the Colonel for eighteen years, this female had retired to
bed and stayed there, and there was a legend in the family, though
neither by word nor look did she ever give any indication of it, that
she enjoyed seeing Pat.
Bedridden ladies of advanced age seldom bubble over with fun and
joie de vivre. This one's attitude toward life seemed to have been
borrowed from her favourite light reading, the works of the Prophet
Jeremiah, and Pat, as she emerged into the sunshine after some
eighty minutes of her society, was feeling rather like Jeremiah's
younger sister.
The sense of being in a world unworthy of her—a world cold and
unsympathetic and full of an inferior grade of human being, had now
become so oppressive that she was compelled to stop on her way
home and linger on the old bridge which spanned the Skirme. From
the days of her childhood this sleepy, peaceful spot had always been
a haven when things went wrong. She was gazing down into the
slow-moving water and waiting for it to exercise its old spell, when
she heard her name spoken and turned to see Hugo.
"What ho," said Hugo, pausing beside her. His manner was genial
and unconcerned. He had not met her since that embarrassing
scene in the lobby of the Hotel Lincoln, but he was a man on whom
the memory of past embarrassments sat lightly. "What do you think
you're doing, young Pat?"
Pat found herself cheering up a little. She liked Hugo. The sense of
being all alone in a bleak world left her.
"Nothing in particular," she said. "Just looking at the water."
"Which in its proper place," agreed Hugo, "is admirable stuff. I've
been doing a bit of froth-blowing at the Carmody Arms. Also buying
cigarettes and other necessaries. I say, have you heard about my
Uncle Lester's brain coming unstuck? Absolutely. He's quite non
compos. Mad as a coot. Belfry one seething mass of bats. He's
taken to climbing ladders in the small hours after swallows' nests.
However, shelving that for the moment, I'm very glad I ran into you
this morning, young Pat. I wish to have a serious talk with you about
old John."
"John?"
"John."
"What about John?"
At this moment there whirred past, bearing in its interior a weedy,
snub-nosed man with a waxed moustache, a large red automobile.
Hugo, suspending his remarks, followed it with astonished eyes.
"Good Lord!"
"What about Johnnie?"
"That was the Dex-Mayo," said Hugo. "And the gargoyle inside was
that blighter Twist from Healthward Ho. Great Scott! The car must
have been over there to fetch him."
"What's so remarkable about that?"
"What's so remarkable?" echoed Hugo, astounded. "What's
remarkable about Uncle Lester deliberately sending his car twenty
miles to fetch a man who could have come, if he had to come at all,
by train at his own expense? My dear old thing, it's revolutionary. It
marks an epoch. Do you know what I think has happened? You
remember that dynamite explosion in the park when Uncle Lester
nearly got done in?"
"I don't have much chance to forget it."
"Well, what I believe has happened is that the shock he got that day
has completely changed his nature. It's a well-known thing. You hear
of such cases all the time. Ronnie Fish was telling me about one
only yesterday. There was a man he knew in London, a money
lender, a fellow who had a glass eye, and the only thing that enabled
anyone to tell which of his eyes was which was that the glass one
had rather a more human expression than the other. That's the sort
of chap he was. Well, one day he was nearly konked in a railway
accident, and he came out of hospital a different man. Slapped
people on the back, patted children on the head, tore up I.O.U.'s,
and talked about its being everybody's duty to make the world a
better place. Take it from me, young Pat, Uncle Lester's whole
nature has undergone some sort of rummy change like that. That
swallow's nest business must have been a preliminary symptom.
Ronnie tells me that this money lender with the glass eye...."
Pat was not interested in glass-eyed money lenders.
"What were you saying about John?"
"I'll tell you what I'm going to do. I'm going home quick, so as to be
among those present when he starts scattering the stuff. It's quite on
the cards that I may scoop that five hundred yet. Once a tightwad
starts seeing the light...."
"You were saying something about John," said Pat, falling into step
with him as he moved off. His babble irked her, making her wish that
she could put the clock back a few years. Age, they say, has its
compensations, but one of the drawbacks of becoming grown-up
and sedate is that you have to abandon the childish practice of
clumping your friends on the side of the head when they wander
from the point. However, she was not too old to pinch her companion
in the fleshy part of the arm, and she did so.
"Ouch!" said Hugo, coming out of his trance.
"What about John?"
Hugo massaged his arm tenderly. The look of a greyhound pursuing
an electric hare died out of his eyes.
"Of course, yes. John. Glad you reminded me. Have you seen John
lately?"
"No. I'm not allowed to go to the Hall, and he seems too busy to
come and see me."
"It isn't so much being busy. Don't forget there's a war on. No doubt
he's afraid of bumping into the parent."
"If Johnnie's scared of Father...."
"There's no need to speak in that contemptuous tone. I am, and
there are few more intrepid men alive than Hugo Carmody. The old
Colonel, believe me, is a tough baby. If I ever see him, I shall run like
a rabbit, and my biographers may make of it what they will. You,
being his daughter and having got accustomed to his ways, probably
look on him as something quite ordinary and harmless, but even you
will admit that he's got eyebrows which must be seen to be
believed."
"Oh, never mind Father's eyebrows. Go on about Johnnie."
"Right ho. Well, then, look here, young Pat," said Hugo, earnestly, "in
the interests of the aforesaid John, I want to ask you a favour. I
understand he proposed to you that night at the Mustard Spoon."
"Well?"
"And you slipped him the mitten."
"Well?"
"Oh, don't think I'm blaming you," Hugo assured her. "If you don't
want him, you don't. Nothing could be fairer than that. But what I'm
asking you to do now is to keep clear of the poor chap. If you happen
to run into him, that can't be helped, but be a sport and do your best
to avoid him. Don't unsettle him. If you come buzzing round, stirring
memories of the past and arousing thoughts of Auld Lang Syne and
what not, that'll unsettle him. It'll take his mind off his job and ... well
... unsettle him. And, providing he isn't unsettled, I have strong
hopes that we may get old John off this season. Do I make myself
clear?"
Pat kicked viciously at an inoffensive pebble, whose only fault was
that it happened to be within reach at the moment.
"I suppose what you're trying to break to me in your rambling,
woollen-headed way is that Johnnie is mooning round that Molloy
girl? I met her just now in Bywater's, and she told me she was
staying at the Hall."
"I wouldn't call it mooning," said Hugo thoughtfully, speaking like a
man who is an expert in these matters and can appraise subtle
values. "I wouldn't say it had quite reached the mooning stage yet.
But I have hopes. You see, John is a bloke whom Nature intended
for a married man. He's a confirmed settler-down, the sort of chap
who...."
"You needn't go over all that again. I had the pleasure of hearing
your views on the subject that night in the lobby of the hotel."
"Oh, you did hear?" said Hugo, unabashed. "Well, don't you think I'm
right?"
"If you mean do I approve of Johnnie marrying Miss Molloy, I
certainly do not."
"But if you don't want him...."
"It has nothing to do with my wanting him or not wanting him. I don't
like Miss Molloy."
"Why not?"
"She's flashy."
"I would have said smart."
"I wouldn't." Pat, with an effort, recovered a certain measure of calm.
Wrangling, she felt, was beneath her. As she could not hit Hugo with
the basket in which she had carried two pounds of tea, a bunch of
roses, and a seed cake to her bedridden pensioner, the best thing to
do was to preserve a ladylike composure. "Anyway, you're probably
taking a lot for granted. Probably Johnnie isn't in the least attracted
by her. Has he ever given any sign of it?"
"Sign?" Hugo considered. "It depends what you mean by sign. You
know what old John is. One of these strong, silent fellows who looks
on all occasions like a stuffed frog."
"He doesn't."
"Pardon me," said Hugo firmly. "Have you ever seen a stuffed frog?
Well, I have. I had one for years when I was a kid. And John has
exactly the same power of expressing emotion. You can't go by what
he says or the way he looks. You have to keep an eye out for much
subtler bits of evidence. Now, last night he was explaining the rules
of cricket to this girl, and answering all her questions on the subject,
and, as he didn't at any point in the proceeding punch her on the
nose, one is entitled to deduce, I consider, that he must be strongly
attracted by her. Ronnie thinks so, too. So what I'm asking you to
do...."
"Good-bye," said Pat. They had reached the gate of the little drive
that led to her house, and she turned sharply.
"Eh?"
"Good-bye."
"But just a moment," insisted Hugo. "Will you...."
At this point he stopped in mid-sentence and began to walk quickly
up the road; and Pat, puzzled to conjecture the reason for so abrupt
a departure, received illumination a moment later when she saw her
father coming down the drive. Colonel Wyvern had been dealing
murderously with snails in the shadow of a bush, and the expression
on his face seemed to indicate that he would be glad to extend the
treatment to Hugo.
He gazed after that officious young man with a steely eye. The
second post had arrived a short time before, and it had included
among a number of bills and circulars a letter from his lawyer, in
which the latter regretfully gave it as his opinion that an action
against Mr. Lester Carmody in the matter of that dynamite business
would not lie. To bring such an action would, in the judgment of
Colonel Wyvern's lawyer, be a waste both of time and money.
The communication was not calculated to sweeten the Colonel's
temper, nor did the spectacle of his daughter in apparently pleasant
conversation with one of the enemy help to cheer him up.
"What are you talking about to that fellow?" he demanded. It was
rare for Colonel Wyvern to be the heavy father, but there are times
when heaviness in a father is excusable. "Where did you meet him?"
His tone disagreeably affected Pat's already harrowed nerves, but
she replied to the question equably.
"I met him on the bridge. We were talking about John."
"Well, kindly understand that I don't want you to hold any
communication whatsoever with that young man or his cousin John
or his infernal uncle or any of that Hall gang. Is that clear?"
Her father was looking at her as if she were a snail which he had just
found eating one of his lettuce leaves, but Pat still contrived with
some difficulty to preserve a pale, saintlike calm.
"Quite clear."
"Very well, then."
There was a silence.
"I've known Johnnie fourteen years," said Pat in a small voice.
"Quite long enough," grunted Colonel Wyvern.
Pat walked on into the house and up the stairs to her room. There,
having stamped on the basket and reduced it to a state where it
would never again carry seed cake to ex-cooks, she sat on her bed
and stared, dry-eyed, at her reflection in the mirror.
What with Dolly Molloy and Hugo and her father, the whole aspect of
John Carroll seemed to be changing for her. No longer was she able
to think of him as Poor Old Johnnie. He had the glamour now of
something unattainable and greatly to be desired. She looked back
at a night, some centuries ago, when a fool of a girl had refused the
offer of this superman's love, and shuddered to think what a mess of
things girls can make.
And she had no one to confide in. The only person who could have
understood and sympathized with her was Hugo's glass-eyed money
lender. He knew what it was to change one's outlook.

II
Mr. Alexander (Chimp) Twist stood with his shoulders against the
mantelpiece in Mr. Carmody's study and, twirling his waxed
moustache thoughtfully, listened with an expressionless face to
Soapy Molloy's synopsis of the events which had led up to his being
at the Hall that morning. Dolly reclined in a deep armchair. Mr.
Carmody was not present, having stated that he would prefer to
leave the negotiations entirely to Mr. Molloy.
Through the open window the sounds and scents of summer poured
in, but it is unlikely that Chimp Twist was aware of them. He was a
man who believed in concentration, and his whole attention now was
taken up by the remarkable facts which his old acquaintance and
partner was placing before him.
The latter's conversation on the telephone some two hours ago had
left Chimp Twist with an open mind. He was hopeful, but cautiously
hopeful. Soapy had insisted that there was a big thing on, but he had
reserved his enthusiasm until he should learn the details. The thing,
he felt, might seem big to Soapy, but to Alexander Twist no things
were big things unless he could see in advance a substantial profit
for A. Twist in them.
Mr. Molloy, concluding his story, paused for reply. The visitor gave
his moustache a final twist, and shook his head.
"I don't get it," he said.
Mrs. Molloy straightened herself militantly in her chair. Of all
masculine defects, she liked slowness of wit least; and she had
never been a great admirer of Mr. Twist.
"You poor, nut-headed swozzie," she said with heat. "What don't you
get? It's simple enough, isn't it? What's bothering you?"
"There's a catch somewhere. Why isn't this guy Carmody able to sell
the things?"
"It's the law, you poor fish. Soapy explained all that."
"Not to me he didn't," said Chimp. "A lot of words fluttered out of him,
but they didn't explain anything to me. Do you mean to say there's a
law in this country that says a man can't sell his own property?"
"It isn't his own property." Dolly's voice was shrill with exasperation.
"The things belong in the family and have to be kept there. Does that
penetrate, or have we got to use a steam drill? Listen here. Old
George W. Ancestor starts one of these English families going—way
back in the year G.X. something. He says to himself, 'I can't last
forever, and when I go then what? My son Freddie is a good boy,
handy with the battle axe and okay at mounting his charger, but he's
like all the rest of these kids—you can't keep him away from the
hock shop as long as there's anything in the house he can raise
money on. It begins to look like the moment I'm gone my collection
of old antiques can kiss itself good-bye.' And then he gets an idea.
He has a law passed saying that Freddie can use the stuff as long as
he lives but he can't sell it. And Freddie, when his time comes, he
hands the law on to his son Archibald, and so on, down the line till
you get to this here now Carmody. The only way this Carmody can
realize on all these things is to sit in with somebody who'll pinch
them and then salt them away somewheres, so that after the cops
are out of the house and all the fuss has quieted down they can get
together and do a deal."
Chimp's face cleared.
"Now I'm hep," he said. "Now I see what you're driving at. Why
couldn't Soapy have put it like that before? Well, then, what's the
idea? I sneak in and swipe the stuff. Then what?"
"You salt it away."
"At Healthward Ho?"
"No!" said Mr. Molloy.
"No!" said Mrs. Molloy.
It would have been difficult to say which spoke with the greater
emphasis, and the effect was to create a rather embarrassing
silence.
"It isn't that we don't trust you, Chimpie," said Mr. Molloy, when this
silence had lasted some little time.
"Oh?" said Mr. Twist, rather distantly.
"It's simply that this bimbo Carmody naturally don't want the stuff to
go out of the house. He wants it where he can keep an eye on it."
"How are you going to pinch it without taking it out of the house?"
"That's all been fixed. I was talking to him about it this morning after I
'phoned you. Here's the idea. You get the stuff and pack it away in a
suitcase...."
"Stuff that there's only enough of so's you can put it all in a suitcase
is a hell of a lot of use to anyone," commented Mr. Twist
disparagingly.
Dolly clutched her temples. Mr. Molloy brushed his hair back from his
forehead with a despairing gesture.
"Sweet potatoes!" moaned Dolly. "Use your bean, you poor sap, use
your bean. If you had another brain you'd just have one. A thing
hasn't got to be the size of the Singer Building to be valuable, has it?
I suppose if someone offered you a diamond you'd turn it down
because it wasn't no bigger than a hen's egg."
"Diamond?" Chimp brightened. "Are there diamonds?"
"No, there aren't. But there's pictures and things, any one of them
worth a packet. Go on, Soapy. Tell him."
Mr. Molloy smoothed his hair and addressed himself to his task once
more.
"Well, it's like this, Chimpie," he said. "You put the stuff in a suitcase
and you take it down into the hall where there's a closet under the
stairs...."
"We'll show you the closet," interjected Dolly.
"Sure we'll show you the closet," said Mr. Molloy generously. "Well,
you put the suitcase in this closet and you leave it lay there. The idea
is that later on I give old man Carmody my cheque and he hands it
over and we take it away."
"He thinks Soapy owns a museum in America," explained Dolly. "He
thinks Soapy's got all the money in the world."
"Of course, long before the time comes for giving any cheques, we'll
have got the stuff away."
Mr. Chimp digested this.
"Who's going to buy it when you do get it away?" he asked.
"Oh, gee!" said Dolly. "You know as well as I do there's dozens of
people on the other side who'll buy it."
"And how are you going to get it away? If it's in a closet in Carmody's
house and Carmody has the key...?"
"Now there," said Mr. Molloy, with a deferential glance at his wife, as
if requesting her permission to re-open a delicate subject, "the
madam and I had a kind of an argument. I wanted to wait till a
chance came along sort of natural, but Dolly's all for quick action.
You know what women are. Impetuous."
"If you'd care to know what we're going to do," said Mrs. Molloy
definitely, "we're not going to hang around waiting for any chances to
come along sort of natural. We're going to slip a couple of knock-out
drops in old man Carmody's port one night after dinner and clear out
with the stuff while...."
"Knock-out drops?" said Chimp, impressed. "Have you got any
knock-out drops?"
"Sure we've got knock-out drops. Soapy never travels without them."
"The madam always packs them in their little bottle first thing before
even my clean collars," said Mr. Molloy proudly. "So you see,
everything's all arranged, Chimpie."
"Yeah?" said Mr. Twist, "and how about me?"
"How do you mean, how about you?"
"It seems to me," pointed out Mr. Twist, eyeing his business partner
in rather an unpleasant manner with his beady little eyes, "that you're
asking me to take a pretty big chance. While you're doping the old
man I'll be twenty miles away at Healthward Ho. How am I to know
you won't go off with the stuff and leave me to whistle for my share?"
It is only occasionally that one sees a man who cannot believe his
ears, but anybody who had been in Mr. Carmody's study at this
moment would have been able to enjoy that interesting experience.
A long moment of stunned and horrified amazement passed before
Mr. Molloy was able to decide that he really had heard correctly.
"Chimpie! You don't suppose we'd double-cross you?"
"Ee-magine!" said Mrs. Molloy.
"Well, mind you don't," said Mr. Twist coldly. "But you can't say I'm
not taking a chance. And now, talking turkey for a moment, how do
we share?"
"Equal shares, of course, Chimpie."
"You mean half for me and half for you and Dolly?"
Mr. Molloy winced as if the mere suggestion had touched an
exposed nerve.
"No, no, no, Chimpie! You get a third, I get a third, and the madam
gets a third."
"Not on your life!"
"What!"
"Not on your life. What do you think I am?"
"I don't know," said Mrs. Molloy acidly. "But, whatever it is, you're the
only one of it."
"Is that so?"
"Yes, that is so."
"Now, now, now," said Mr. Molloy, intervening. "Let's not get
personal. I can't figure this thing out, Chimpie. I can't see where your
kick comes in. You surely aren't suggesting that you should ought to
have as much as I and the wife put together?"
"No, I'm not. I'm suggesting I ought to have more."
"What!"
"Sixty-forty's my terms."
A feverish cry rang through the room, a cry that came straight from a
suffering heart. The temperamental Mrs. Molloy was very near the
point past which a sensitive woman cannot be pushed.
"Every time we get together on one of these jobs," she said, with
deep emotion, "we always have this same fuss about the divvying
up. Just when everything looks nice and settled you start this thing of
trying to hand I and Soapy the nub end of the deal. What's the
matter with you that you always want the earth? Be human, why
can't you, you poor lump of Camembert."
"I'm human all right."
"You've got to prove it to me."
"What makes you say I'm not human?"
"Well, look in the glass and see for yourself," said Mrs. Molloy
offensively.
The pacific Mr. Molloy felt it time to call the meeting to order once
more.
"Now, now, now! All this isn't getting us anywheres. Let's stick to
business. Where do you get that sixty-forty stuff, Chimp?"
"I'll tell you where I get it. I'm going into this thing as a favour, aren't
I? There's no need for me to sit in at this game at all, is there? I've
got a good, flourishing, respectable business of my own, haven't I? A
business that's on the level. Well, then."
Dolly sniffed. Her husband's soothing intervention had failed signally
to diminish her animosity.
"I don't know what your idea was in starting that Healthward Ho
joint," she said, "but I'll bet my diamond sunburst it isn't on the level."
"Certainly it's on the level. A man with brains can always make a
good living without descending to anything low and crooked. That's
why I say that if I go into this thing it will simply be because I want to
do a favour to two old friends."
"Old what?"
"Friends was what I said," repeated Mr. Twist. "If you don't like my
terms, say so and we'll call the deal off. It'll be all right by me. I'll
simply get along back to Healthward Ho and go on running my good,
flourishing, respectable business. Come to think of it, I'm not any too
solid on this thing, anyway. I was walking in my garden this morning
and a magpie come up to me as close as that."
Mrs. Molloy expressed the view that this was tough on the magpie,
but wanted to know what the bird's misfortune in finding itself so
close to Mr. Twist that it could not avoid taking a good, square look at
him had to do with the case.
"Well, I'm superstitious, same as everyone else. I saw the new moon
through the glass, what's more."
"Oh, stop stringing the beads and talk sense," said Dolly wearily.
"I'm talking sense all right. Sixty per cent. or I don't come in. You
wouldn't have asked me to come in if you could have done without
me. Think I don't know that? Sixty's moderate. I'm doing all the hard
work, aren't I?"
"Hard work?" Dolly laughed bitterly. "Where do you get the idea it's
going to be hard work? Everybody'll be out of the house on the night
of this concert thing they're having down in the village, there'll be a
window left open, and you'll just walk in and pack up the stuff. If
that's hard, what's easy? We're simply handing you slathers of
money for practically doing nothing."
"Sixty," said Mr. Twist. "And that's my last word."
"But, Chimpie ..." pleaded Mr. Molloy.
"Sixty."
"Have a heart!"
"Sixty."
"It isn't as though ..."
"Sixty."
Dolly threw up her hands despairingly.
"Oh, give it him," she said. "He won't be happy if you don't. If a guy's
middle name is Shylock, where's the use wasting time trying to do
anything about it?"

III
Mrs. Molloy's prediction that on the night of Rudge's annual dramatic
and musical entertainment the Hall would be completely emptied of
its occupants was not, as it happened, literally fulfilled. A wanderer
through the stable yard at about the hour of ten would have
perceived a light in an upper window: and had he taken the trouble
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookmass.com

You might also like