During my long periods of confinement, I got the idea of exploring new innovative areas of computer science. I really love the procedural generation and mixing it with art to be able to extend the creativity to infinity. I then discovered the Creative Coding that meets my expectations. I wish today to help you to get started with the creative coding with Scala and the Java library Processing.
Creative coding is a type of computer programming in which the goal is to create something expressive instead of something functional. It is used to create live visuals and for VJing, as well as creating visual art and design, entertainment, art installations, projections and projection mapping, sound art, advertising, product prototypes, and much more.
From Wikipedia, the free encyclopedia
Processing
Processing is a software sketchbook and a language for creating visual arts with programming.
Processing is written in Java, so we can write our programs directly from Scala without going through the PDE. It’s possible thanks to the Processing Core dependency that we have to add in your build.sbt (if you work with the sbt build tool).
1 | // https://mvnrepository.com/artifact/org.processing/core |
For our first time with Processing, we are going to creates a simple application that create balls (with physics), when the user clicks on his screen.
Minimal code
To start, this is a minimal example of an empty Processing application in Scala:
1 | import processing.core.PApplet |
PApplet
is a class defined in the Processing Core library and is the base class for all sketches that use processing.core. This class implements a lot of methods and constants needed for image processing and user interaction. In the main
, we use the runSketch
static method who opens a window to display the sketch and take two arguments, an array of arguments (like the name of the window) and the sketch to be displayed.
The Ball class
As I’m not here to teach you programming, I guess you already have the necessary Scala notions to follow this article, so here’s the code for the class Ball :
1 | case class Ball(sketch: PApplet, |
In the constructor of Ball, there is an unexpected parameter: sketch: PApplet
. This parameter allows a ball to access the method who are defined in a PApplet
, for example, the methods for rendering the data. The next
method simply return a new Ball
with updated coordinates and speed (for the bouncing). The render()
method calls the sketch
methods to draw a ball in the sketch. As you may have guessed the fill
method is used to choose a color, and the ellipse
method draws an ellipse from his center and his width and height.
The application
Now that we’ve got a ball, we can really start. I remind you of the objective, which is to create a simple application that creates balls (with physics) when the user clicks on his screen. This is the code for the application :
1 | object BallApp extends PApplet { |
The balls
attribute is used to store the balls of the application. The methods settings
, draw
, and mouseClicked
are defined in the PApplet
class:
settings()
: Defines the actions that will be called at the launching of the application. Here, I choose a screen size and I put my first ball in the application.draw()
: Continuously executes the lines of code contained inside its block until the program is stopped ornoLoop()
is called. Here, I update the coordinates of the balls and I render them.mouseClicked()
: Defines the actions that will be called when a user clicks on his mouse. Here, I create and put a new ball in the application.
The application is now ready to use. This is what I obtain after some clicks:
This was a simple introduction to Creative Coding on my blog, expect to see many more articles on this subject in the coming days. To learn how to use Processing I invite you to follow theses official’s tutorials. I created a Github repository where I would put all my creations with Processing and Scala. Thanks to Happy Coding’s article which inspired me a lot.