Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields...

13
java Spring 2009

Transcript of Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields...

Page 1: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

java

Spring 2009

Page 2: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

PImage

• Let’s look at the PImage class in Processing– What are the fields (i.e., variables)?– What methods are available?– What about the constructor---how many

parameters does it have?• There are 4 constructors each with a different

number of parameters

Page 3: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

Using PImage’s filter() method

PImage img;

void setup() { size(100,200); img = new PImage(100,100); img = loadImage("forest.jpg"); image(img, 0, 0); img.filter(INVERT); image(img, 0, 100);}

Here are the images that you need as a zip file

Page 4: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

PImage’s mask() method

background(255);

PImage img = loadImage("airport.jpg");

PImage maskImg = loadImage("airportmask.jpg");

img.mask(maskImg);

image(img, 0, 0);

Page 5: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

Modifying pixelsPImage arch;

void setup() { size(100, 100); arch = loadImage("arch.jpg");}

void draw() { background(204); int mx = constrain(mouseX, 0, 99); int my = constrain(mouseY, 0, 99); arch.loadPixels(); arch.pixels[my*width + mx] = color(0); arch.updatePixels(); image(arch, 50, 0);}

Page 6: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

PImage revisited

• Here’s an alternative way of looking at the documentation for PImage

• This documentation is produced by Javadoc

• Processing is implemented as a set of Java classes that accompany those normally provided with Java

• Can you find the line() method?

Page 7: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

Java

• Java is a programming language developed by Sun Microsystems in 1995.

• Java is one of the first languages to be platform independent.– Java is compiled to byte-code that runs on a Java

interpreter • A Java program never executes directly (i.e.,

natively) on a machine; instead the Java interpreter reads the byte code and executes the corresponding native machine instructions.

• To run Java programs on a computer, all that is needed is the interpreter (java virtual machine) and some library routines (the API)

Page 8: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

The Java API

• The Java API is the code that comes with the java interpreter (a.k.a. the java virtual machine) when you download java.

• The java API is a collection of Java classes---take a look at the API for Java 1.5.0

• See if you can find the String class.

Page 9: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

A simple Java program

public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); }}• This program prints Hello World! to the monitor• Things to notice:

– All programs are classes– All programs have a method called main() that has

one array of Strings parameter.

Page 10: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

Running Java outside of Processing

Follow these steps to run HelloWorld

1. Find the editor named TextEdit and set its Preferences to Plain text2. Copy and paste HelloWorld from the previous slide into the TextEdit

window. 3. Save it to your home directory in a file named HelloWorld.java

Capitalization matters.4. Open a terminal window; you can find this under Utilities5. In the terminal window, use the command cd to change to the

directory where HelloWorld.java is stored.6. In the terminal window, type javac HelloWorld.java to compile your

program to byte code7. In the terminal window, type java HelloWorld to run your compiled

program on the java virtual machine.• If you saw the message: Exception in thread "main"

java.lang.NoClassDefFoundError:• First, be sure that HelloWorld.class is in the current directory. • Be sure to type java HelloWorld without a trailing .class or .java. • If this was not your problem, it's possible that your CLASSPATH is

set incorrectly.

Page 11: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

In-class exerciseRun the following program and explain what happens; how

is the output generated?

public class Cat{ public void hiss () { System.out.println ("Hiss!"); } public void scratch (Dog victum) {

System.out.println ("I'm scratching the dog"); victum.growl ();

} public void bite (Dog sillyDog) { System.out.println ("I'm bitting the dog");

sillyDog.yelp (); scratch (sillyDog);

}} // continued

Page 12: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

The Dogpublic class Dog{ public void bark () { System.out.println ("Arf!"); System.out.println ("Arf!"); } public void growl () { System.out.println ("Grrrr!"); } public void yelp () { System.out.println ("Awooo!"); } public void beenBittenBy (Cat sillyCat) {

System.out.println ("I've been bitten by a cat with a mean hiss:"); sillyCat.hiss ();

} // continued

Page 13: Java Spring 2009. PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.

Petspublic class Pets{ /* Creates a Cat and a Dog */ public static void main (String [] args) { Cat tom = new Cat (); // create Cat object Dog spike = new Dog; // create Dog object

// demonstrate Cat behavior tom.bite (spike);

System.out.println (); // Skip a line of output

// demonstrate Dog behavior spike.beenBittenBy (tom);

}}