Scala + Processing is a fun way to learn a new language.
Recently, I happened to be convinced by my own experience: in order to learn how to apply some new technologies, it is not enough to read a couple of books on the topic, because without practice the theory instantly disappears from the head.
But what can make such an interesting thing on Scala? In fact, the choice is not too big. I somehow came up with a small tool, slowly wrote it, and “scored”. And in a few months, to my shame, googled the “for loop” syntax ...
I decided that it would not go further, and I needed to find some small projects on the main features of the language. This is where Processing came in handy. Boring training projects, he will help any newcomer (like me) to turn into visual installations. And then you can choose what to dig in depth - for example, the generation of fractals, particle rendering or data visualization. ')
I rewrote Scala and put a couple of examples on GitHub. On the screen just one of them - MSA Fluids . Interested please under the cat.
Once I met Scala, and I instantly liked this language. I leafed through several books, including Programming in Scala , which I strongly recommend to everyone. Even only theory allowed me to take a different look at the world of programming. Unfortunately, my main work with Scala is in no way connected, and in general the language I best speak in some places is quite different from it. And this language is not Processing.
Processing
So, here is just a great time to provide Processing, since few people have heard about it, and only touched their hands with it at all. Processing is a DSL for Java with its own micro IDE and different libraries. Used by so-called Visual Artists to generate all kinds of beautiful trashacks.
I recommend to click on Vimeo on the video with the processing tag and see what the people think of. Here are a couple of beautiful:
And I only briefly summarize everything in them written in my own words. Remember, in childhood they taught programming in BASIC or Logo , where did the line bug draw? The stump is clear, everyone here can still make a fuss and not such beauty in his favorite language. Yes, even in thirty layers of abstraction will wrap along the road. It’s hard for ordinary people to understand what kind of OOP, classes, libraries are, and they don’t need it.
So we decided to make such a simple Java-based language to help ordinary Visual Artists to implement their ideas in an imperative style. Like, draw a line here, and then a square. And off we go. Now Processing has already grown and is hard to ignore. Many excellent libraries have been written for it, and since this is essentially Java, you can also use wagon Java libraries.
And Scala is also running on the JVM ... well, you understand, right?
In fact, there is a whole project based on the Processing IDE that allows you to write on Scala - Spde . And about him already wrote on scala-lang.org . But this IDE seems to me small and frivolous. Try to write something big and the smut starts. So let's stop at something more convenient.
Installation and Setup
Most likely, you should already have some kind of IDE for development on Scala, so I will not focus on this. I use intelliJ IDEA with Scala plugin.
First you need to download and install Processing .
Next, in the Scala project, connect the main Processing library. On Windows, the path is about the same.
Code
Now create a new main class. I called it just Test.
In Processing, most of the functionality is wired into the PApplet class. By the way, it can also be launched by AppletViewer, if you set a profile in Run Configurations. Then the main method is not needed.
This code does little for now, but it already shows the usual structure of Processing programs (which are called sketches). I will expand this code a little further. He will draw black-and-white random flowers to follow the cursor. Something like this:
import processing.core._ import math._ import scala.util.RandomobjectTestextendsPApplet{ privatevar test:Test = _ defmain(args: Array[String]) = { test = newTestval frame = new javax.swing.JFrame("Test") frame.getContentPane().add(test) test.init frame.pack frame.setVisible(true) } } classTestextendsPApplet{ var angle:Int = 0; overridedefsetup() = { size(640, 360) background(102) smooth() noStroke() fill(0, 102) } overridedefdraw() = { angle += 10var value = cos(toRadians(angle)) * 6.0for (a <- 0 to 360 by 75) { val xoff = cos(toRadians(a)) * value val yoff = sin(toRadians(a)) * value fill(Random.nextInt(255)) ellipse( (mouseX + xoff).toFloat, (mouseY + yoff).toFloat, value.toFloat, value.toFloat ); } fill(255); ellipse(mouseX, mouseY, 2, 2) } }
What's next?
As an experiment, I translated a couple of interesting examples to Scala (including the famous Fluids from MSA Visuals) and put them on GitHub: scala-processing-examples . Now there are only two examples, but they are quite interesting.
Also, you will probably need help on available methods. She (and many other useful materials) can always be found on the website processing.org .
Personally, my plan is to take a book , sit down and come up with a small sketch on each topic in order to visualize and remember this topic.
A few more comments.
In order to run the examples from GitHub, you need to import the opengl library. And (important!) Extract the contents of .jar archives with native implementations of these libraries somewhere And then in the Run Configuration to prescribe the path to them Otherwise, the application will be strewed with an error, they say, the jogl library was not found.
I did not understand how to connect native libraries to IDEA more correctly. Perhaps in any way .
And finally
Agree, beautiful pictures to learn an interesting language are much better than dull text utilities. Successful experiments!