Week 10 -- Secret Java
back to syllabusFinal Project Proposal
Your final project proposal is due next week. Post a web page and include the following elements:Java Mode
The Processing environment is written in Java and gives us a simplified syntax for developing basic computer graphics applications. The reference page for processing provides a concise one page overview of the available data structures, control structures, and functions within the processing environment.We can also, however, use any classes available to us in the Java API. This reference is, well, a little longer than one page. Take a look here: JavaTM 2 Platform, Standard Edition, v 1.4.2 API Specification.
To do this, we will write or processing program in "Java Mode", which looks like this:
public class JavaMode1 extends BApplet {
void setup() {
size(100,100);
}
void loop() {
background(0);
stroke(255);
noFill();
rect(25,25,25,25);
}
}
What's new here? "public class JavaMode extends BApplet" This is the normal Java class
definition, which in our usual processing applets is invisible to us. But here we reveal this layer, saying that we our
writing our own public java class called JavaMode, which is a "child" of BApplet. BApplet is the
specialized applet class created by the developers of processing. Once here, we can import
other java libraries.
Let's say we want to use the java class ArrayList, which we found in java.util. The reference page is here: http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html.
First, we import the package java.util by adding the following line at the top:
import java.util.*Now that we have this package available, we can declare a variable of type ArrayList and call various functions on it.
ArrayList myList = new ArrayList(); myList.add(myObject); println(myList.size());How do we know what these functions are and what arguments they take? Here, rather than referring to the processing reference, we consult the java API.
Here's an example that uses the functionality of a java ArrayList (a special kind of array that allows for dynamic resizing) in combination with a particle "class" written within the processing environment.
CLICK MOUSE TO LAUNCH PARTICLES, RELEASE TO REMOVE PARTICLES
/*
import statements
These are only required if you want to import non-processing imported Java functions
basically this means that any Java classes that are not imported by processing need to be
imported here. In the case of java.util, we don't necessarily need the import, but
it's a useful example.
*/
import java.util.*;
/*
This is a normal Java class declaration. You normally don't need this. You will need
it if you use any "import" statements as above.
*/
public class JavaMode extends BApplet {
//declaring a global variable of type ArrayList
ArrayList a;
void setup() {
size(400,300);
colorMode(RGB,255,255,255,100);
//creating an instance of ArrayList and storing it in our global variable
//constructor is defined in the java API
//http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
a = new ArrayList();
}
void loop() {
background(0);
if (mousePressed) {
//add a new particle into the arraylist
//particle is our own class defined below
a.add(new Particle());
} else if (a.size() > 0) {
//delete the last particle out of the list
a.remove(a.size()-1);
}
//iterate through our arraylist and get each particle
for (int i = 0; i < a.size(); i++) {
//when something comes out of the arraylist via "get" we have to
//remind ourselves what type it is. In this case, it's an instance of "Particle"
Particle p = (Particle) a.get(i);
p.run();
p.gravity();
p.render();
}
}
//our Particle Class
class Particle {
float x;
float y;
float xspeed;
float yspeed;
Particle() {
x = width/2;
y = height/2;
xspeed = random(-2,2);
yspeed = random(-5,0);
}
void run() {
x = x + xspeed;
y = y + yspeed;
}
void gravity() {
yspeed += 0.1;
}
void render() {
fill(200,100,255,75);
noStroke();
ellipse(x,y,10,10);
}
}
}
It should be noted that for the java.util package, we don't have to introduce
java mode in our processing applet. It is automatically available to us. However,
this is a good example in that if we wanted to use classes from another package, the strategy
outlined above would be required.
Rectangle
Let's take a look at the class Rectangle:http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Rectangle.html.
A Rectangle specifies an area in a coordinate space that is enclosed by the Rectangle object's top-left point (x, y) in the coordinate space, its width, and its height. In processing, we have a function available to us to draw a rectangle, i.e. rect. But this java Rectangle has other useful functionality, such as "contains" (finally, we have a way to check intersection w/o writing a whole set of if statements ourselves!).
Here is an example:
simple rollover example
And here's an example (thanks to Dan O'Sullivan) that uses both ArrayLists and Rectangles
http://stage.itp.tsoa.nyu.edu/%7Edano/icm/RectangleTarget/applet/
Java without Processing
This may come as a shock to you all, but it's also possible to write java code without the processing environment whatsoever!Let's see if we can decipher the following java code:
public class HelloWorld
{
int x = 0;
public static void main(String[] args)
{
int x = 0;
System.out.println("Hello Java World, I can count!");
while (x < 10) {
System.out.println(x);
x++;
}
}
}
Compile: javac ClassName.java
Run: java ClassName Take the above code and make a text file called HelloWorld.java (using notepad, textpad, bbedit, etc.) Congratulations, you've written your first java program. However, unlike with processing, we don't have a "Run" or "Play" button. You have to compile and run the program yourself.
On a PC, it will look something like this (one a Mac, use "terminal"):
Holy more than one file, batman
Let's take a look at an object oriented example where our class (Bank Account) is kept in its own file and a "driver" program accesses it. Here are the two files you need:BankAccount.java
BankAccountTest.java
As long as these files are both in the same directory, we can compile BankAccountTest.java, which, since it uses the BankAccount.java class, will instigate the compilation of that class.
There's a lot more we can do with java, of course. Here's a more complex example that Shawn Van Every put together which emulates one of our earlier simple processing examples (using only Java).
Emulating Processing: http://stage.itp.nyu.edu/~sve204/wiki/wiki.pl?EmulatingProcessing
For more information and other examples, visit Shawn Van Every's page:
http://stage.itp.nyu.edu/~sve204/wiki/wiki.pl?ProcessingProcessing.
back to syllabus