? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
?????????????????????????????????? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
? |
|
? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Processing is a context for exploring the emerging conceptual space enabled by electronic media. It is an environment for learning the fundamentals of computer programming within the context of the electronic arts and it is an electronic sketchbook for developing ideas. www.Proce55ing.net The Processing environment is the easiest Java compiler / interactive graphics and multimedia programming environment known to man. The system can be used to produce locally run pieces, as well as web-embeddable Java applets. Quite deliberately, the system is also designed to bridge the gap between educational graphics programming environments, and "real Java." Processing can be used like training wheels, but does not have to be. The goal of this tutorial is to introduce users of Macromedia Flash and Director to the Processing environment by comparing and contrasting the systems. The theory is that the knowledge gained from these Macromedia tools can easily transfer, reducing the amount of required teaching. It assumes you have a basic understanding of either Macromedia product. By the end of this tutorial, you should be able to produce and publish your own Processing (Java) pieces, and communicate through a serial port with a BX-24 chip. Table of Contents Introduction Obtaining the Processing Software A tour of the interface Lower Level Media Manipulation Syntax Structure Static 2D Drawing Time and Motion Mouse & Keyboard Presentation / Exporting Drawing Image Files 3D Form Pixels Typography Serial The Future |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Currently in on-screen interaction design classes, the dominant teaching vehicle has been Flash or Director. Students are beginning to produce geometrically dynamic and more algorithmically complex pieces influenced by works done in environments other than their own. At ITP, I witnessed an experiment in one such class (although since then, they've been using Processing). In the middle of teaching Director Lingo, a one-week excursion into Java programing introduced students to a language other than Lingo - in hopes that they would get a more diverse view of different programming systems. A template was given to them - and they simply changed the code with a quick rosetta stone reference. After this week of confusion, several students were left with an empty yearning to learn more Java. There was no easy way to tell them that the average college Java course usually has you working in a text console, and has little or no relationship to "Applet Graphics" unless it is a course specifically targeted to teach that. In the following lesson, I hope to bridge this academic gap with the help of the Processing environment. It is not meant as a replacement to those Java courses; it is a supplement which takes care of the logistics - without getting deep into syntax nuances. Also, Processing and Java are not being presented as the next level beyond Macromedia, nor are they being presented as a lower level system. They are simply an alternative, capable of doing different things in different ways. If you are currently attending a Java course, it may be possible to use Processing for your assignments, depending on how flexible your instructor is. This tutorial is a mixture of my own writing and images - with those found on one state of the Processing website, maintained by Casey Reas and Ben Fry. You will get to know these two names if you are a Processing user. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Processing is free (free as in free beer, free as in free speech, free as in free country), and still in development stages. It will continue to be free even after it is finished. The software is currently in ALPHA phase, which is the thing that comes before BETA. Bugs are being fixed, and features are being added. In order to download the cross-platform install program you can email its developers to join the testing community. On the Processing website, click Download for further instructions. Additionally, there is an avid messaging system amongst the testers. It is highly recommended that you create a login for yourself. This community is the best way to receive help on any topic - from other testers, from the authors themselves, and from lurking geeks such as the author of this tutorial. It is also important to note that this is the online community that helps develop Processing by discussing features in those forums. On the Processing website, click Discourse. It is also important to note that the Processing software and website are constantly being updated. Check back for new additions to the reference, and new versions of the software. Right now is an exciting time! |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For detailed and advanced information about the Processing environment, see the Processing Environment reference.
|
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In Director, one imports or creates media into a cast, then drag it onto a stage where it will exist as a sprite. In Flash, one also imports or creates media into a library, then instances them as movieclips on a similar stage. In Processing (and in Java) this media importing is all done in code, similar to the way HTML works. Additionally, any custom media that you invent (vector systems, DNA data, color samples from the film, Fargo) can all be embedded as part of the Java code. In fact, you are not restricted to having any external images or sounds if you want to keep everything in one tidy file - because the pixels of an image can also be converted to be part of your code, and sound data can also be stored as a large array of data. The benefit of having a library or cast is for better control over formatting, in order to save disk space / memory, and in order to add specific point-&-click features onto a common file system metaphor. The benefit of a sprite or movieclip is so that a visible, tangible object can sit on the screen and be an easy way for people to make buttons, videogame characters, individual graphic elements, and other visual, controllable, positive space elements. However, in creating cooperative groups of elements, and objects that are neither positive nor negative space - this metaphor has become a burden to some. In Processing, this complex layer does not exist; there is only mouse/keyboard/serial events in conjunction with basic drawing routines. One takes care of redrawing the scene repeatedly, in response to input changes and time. In a matter of speaking, it is your responsibility to write your own sprite or movieclip system, but you are not required to. Invent another metaphor that would be more useful to you as an artist. It also affords the opportunity to deeply diversify the aesthetic from those works we often spot as Macromedia-influenced. In the coming sections within this guide, I introduce methods for rendering imagery to the screen. Then I will introduce time and animation. Finally, I will show you how to add interaction with the mouse, keyboard, and serial port. These are the basic building blocks for anything you wanted to do in high-level tools. You will be capable of doing them in Java if you put your mind to constructing them yourself. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
And Java variables are as follows:
Flash people: there is no such thing as var. For in-depth information on variables, consult the official Java language tutorial.
Things are a bit different from Lingo when it comes to comparison. The programmer uses a single "=" to assign a variable some value. One uses a "==" (double equal) when trying to determine whether or not a number equals another number. Additionally, "not equal to" is no longer "" - it is now "!=" - and the rest are the same ("" , ">=" , and "consult the authority.
Lingo people, this is the same as "repeat with i = 0 to 4." Inside those parentheses, there are three special statements separated by two semicolons. The first statement creates a temporary variable. The second statement specifies a condition which allows the loop to continue looping. As soon as i is no longer smaller than 5, then the loop will stop. The third statement gives you a chance to incrementally change i however you want. "i++" is shorthand for "i = i + 1". Sun can tell you much more about for loops.
If you are curious about getting heavy on the flow-control syntax, here is a broader link to the Java language tutorial, the part about flow-control. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Let us break this code down, line for line.
Now, here are some pre-fab shapes.
triangle will draw a three-pointed polygon. It has six parameters. Parameters 1 and 2 are the first X,Y coordinate. Parameters 3 and 4 are the second X,Y coordinate. Parameters 5 and 6 are the third X,Y coordinate.
Now, you can see the quad underneath the oval because only the strokes are being drawn. Similarly, there is noStroke, which disables the outline from being drawn. To enable stroke or fill once more, you must call stroke or fill, specifying a color.
Although this is the case, you can still look up some examples on the web that will tell you how to construct anything you want from convex polygons.
For more detailed information about vector drawing, see the Processing Form Examples, the Processing Shape reference. There is much more to draw and render to the screen, but I have only given you the 2D drawing routines so that we can cover animation and interactivity. Then we will return to the other drawing methods. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In Director, there is the score. You are given a playback head, and tweening methods for sprites. Things like video, embedded Flash, QTVRs, and sound seem to animate in their own time-space. If you are working on more of a dynamic animation, you might only use one frame and code in responses to an ExitFrame or PrepareFrame event. In Flash, you are given a timeline, and a bit more complex tweening support than director. Those who choose to work completely in ActionScript commonly use two frames - one for setup and to call a looping frame, and the other to loop forever. Actionscript also allows you to respond in code in an onClipEvent (enterFrame). Processing has no timeline, score, or tweening methods, unless you choose to structure your code as such. Like Lingo and Actionscript, Processing allows you to respond to a frame-progression event handler with your own drawing routine. Up until now, I have been showing you Processing code in Basic Mode. This mode is for drawing static images. It is merely a shopping list of visual elements. Processing has three modes of operation: basic, standard, and advanced. Advanced Mode is conventional Java, without training wheels. In order to begin with time and motion, we will now move forward to Standard Mode. If you had been clicking my tangential links in my text, you may have seen one or two standard mode Processing programs. Here is a simple example:
The optional setup( ) section runs once when the program begins. The loop( ) section runs forever until the program is stopped. In Lingo, setup( ) is similar to beginSprite or startMovie - and loop( ) is similar to ExitFrame or PrepareFrame. In Flash, setup( ) is similar to the first frame of the animation that only executes once, then calls the loop. Setup( ) and loop( ) are both functions. You can also write your own functions for organization and encapsulation of complexity. For more information on writing custom functions in Java, see Sun's Java language tutorial - Implementing Methods section. Once you have written the first function in Processing, that program will automatically switch to standard mode. Any statements outside of your function that are not variable initializes will no longer work when you press play. You can move this code to either setup( ) or loop( ). If you want a variable to be global (meaning that it retains its value outside the scope of the functions) then declare it at the top of the program, outside of both loop( ) and setup( ). In the example above, the variable x was declared global. In Processing, the framerate(n) function can be used to slow down or speed up the entire sketch, but it is certainly possible to move things are differing speeds simply by varying the amount that you increment, or by using floats and only adding a fraction to them. For more long term and more time-precise control, Processing gives you full access to Western time measurement. Processing has several methods for getting the date and time from your computer's clock. year( ) // current year, i.e. 2002, 2003, etc. month( ) // returns the current month, from 1..12 day( ) // returns the day of month, from 1..31 hour( ) // the current hour, from 0..23 minute( ) // the current minute, from 0..59 second( ) // the current second, from 0..59 A special function called millis( ) returns the number of milliseconds (thousandths of a second) since starting the applet. This is often used for timing animation sequences. millis( ) // number of milliseconds since starting applet. It is also possible to make your applet wait by using the delay function. Using this function can effectively adjust frame rates. delay(40); // takes a nap for 40 milliseconds
This is not a very pretty example, but it's simple to understand. For some nicer (and more complex) examples of time, see Clock, by Mescobosa, and Milliseconds, by REAS. These two were done in Processing. Also, for more examples of animated motion, see the Processing Motion Examples. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Access to the mouse and keyboard are both similar to the way Flash and Director do it. In Lingo, the mouse is addressed with the mouseLoc, the mouseH, and the mouseV. Additionally, there are also mouse event handlers such as on mouseDown. In Flash, there is onClipEvent (mouseDown), etc. In Processing, the mousePressed( ) function is called every time the mouse is pressed and the mouseReleased( ) method is called every time the mouse if released. All you have to do is add the function to your code, just like loop( ).
For further information on the mouse, see the Processing Mouse reference, and don't forget to check out these exquisite Processing Mouse examples. Keyboard input is equally similar to Flash and Director.
The keyboard input can also be delivered to you in the form of an event handling function.
For further information on keyboard input, see the Processing Keyboard reference, and don't forget to check out these exquisite Processing Keyboard examples. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Any Processing program can be "published" as a Java applet. First make sure your sketch is saved, then choose File -> Export to Web, or press Ctrl+E (or press the export button). You will see the Processing message area say "Exporting for web . . ." for just a moment, and then it will say "Done Exporting." In order to get the web files, venture into the Processing sketch folder. Look for the folder with the name of your sketch and open it up. In that folder, you will see another folder called applet. This folder can be uploaded to the web. I highly encourage editing the default index.html that is generated from Processing. You must keep all the included files relative to the HTML in the applet folder, as they are linked the same way any other HTML media is linked. Processing Folder/ ??????????????? sketchbook/ ??????????????????? ????????default/ ????????????????????????????????? your_sketch_name/ ?????????????????????????????????????????????????????????????? applet/ ?????????????????????????????????????????????????????your_sketch_name.java ?????????????????????????????????????????????????????your_sketch_name.class ?????????????????????????????????????????????????????your_sketch_name.jar ?????????????????????????????????????????????????????index.html save( ) and saveFrame( ) If you need to export to non-interactive formats, it is possible to make .tif files of the Processing window by using the saveFrame( ) function. Placing this method at the end of the loop( ) will save the image on the screen. If saveFrame( ) is called multiple times, it will create an image sequence as follows: screen-0001, screen-0002, screen-0003, etc. Using save( ) will let you choose a file name. It is simple to import these images into Quicktime or other video programs to make an animated documentation of a Processing program. Although Processing has built in this easy image saving function, it is also possible to export to other formats with a bit more work. For example, here is a Processing program that exports to Adobe Illustrator. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I would save it in the appropriate folder:
BImage is an object that will hold your loaded file until you draw it. b is what I chose to call this one. image( ) is what actually draws the image to the screen. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?
vertex(x, y, z); line(x1, y1, z1, x2, y2, z2); bezierVertex(x, y, z); curveVertex(x, y, z); ? box(size); box(width, height, depth); sphere(size);
Note that box and sphere do not ask you to specify position coordinates! In these examples, it is necessary to use translate and rotate. There is also scale, and a pair of functions called push and pop which allow you to bookmark your translations in a very organized fashion. To learn the details on this useful way to organize your drawing, see the Processing Transform Reference and the Processing Transform Examples. Of course, if you do not care for these transformations, then there is always a solution. Also note the use of lights( ) and noLights( ). Using lights will render the 3D shape in a manner which suggests shading. For more information concerning lighting, see the Processing Lights Reference. "What? that's it for 3D?" If you think that this is not enough 3D to allow you to make interesting things, then check out all the wonderful art that has already been created at Processing Software. And this is only the beginning. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Control over the pixels is currently far from Flash. SetPixel and GetPixel have just been added to Director (and already, a well known Director-loving interactive artist has adopted the nickname SetPixel). However, Director is quite possibly the slowest pixel addressing system you will ever work with. ITP Students attend a class taught by Danny Rozin called The World - Pixel by Pixel, and continue to program in C because it is the only thing fast enough for them to achieve their conceptual goals (Lingo and MAX being the only alternatives). It is popular for such ITP students to prepare for Danny's class by attending a C course. Working with Processing pixels is considerably faster than Image Lingo, and arguably less complex. Although Java does not compare to the speed of C, soon Danny's students might explore the possibility of using Processing to speed the learning curve. get(x, y); // Returns an integer set(x, y, color); pixels[index]; // Array containing the display window
With control over the pixels, you can also implement your very own drawing routines. For example, here is transparency. Writing the rest of the Director inks would not be so hard. Here is a dotted line function. For more information about pixel play, see the Processing Image reference and the Processing Image Examples. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The typographic rendering system currently uses a Processing-specific font file format. The makers of the software have included a font import menu item to help you out, and even then - they have provided the Processing programmer with a wealth of fonts to choose from. Click here to see all of the currently included fonts. These fonts are stored as bitmap images. Here is a very simple example of rendering text to the sketch. Go ahead and run this program - and expect to get an error.
|
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Through a serial port, a computer can communicate with anything from Palm Pilots to medical equipment. It is common in electronic arts to use the serial port to talk to custom built devices. Director is able to do this with the help of third party Xtras. Flash does not support serial ports, nor does it have a third-party plugin system. Processing has serial communication built in. In this example, we will make a turning knob interact with a Processing sketch. A word of warning, however; this section is technically more advanced than the previous sections, as it requires a working knowledge of basic electronic circuitry. This circuit uses a BX-24, which is a common prototyping integrated circuit widely used at ITP, but also at other similar places. For more information on setting up a BX-24, see Tom Igoe's Physical Computing reference as well as the lab assignments from his Physical Computing class at ITP. Here is a picture of the circuit, excluding the +5v power supply - in order to simplify the photo. It uses a 10K potentiometer with a 1K resistor at pin 13. For details on setting this up, see the ITP Intro to BX-24.
Processing allows you to choose which serial port it will work with through the interface Sketch -> Serial Port submenu.
For more information about serial, see the Processing serial reference. |
? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
last updated September 19, 2004 |
? |