Download java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
java
Spring 2009
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
Using PImage’s filter() method
Here are the images that you need as a zip file
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);
}
PImage’s mask() method
background(255);
PImage img = loadImage("airport.jpg");
PImage maskImg =
loadImage("airportmask.jpg");
img.mask(maskImg);
image(img, 0, 0);
Modifying pixels
PImage 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);
}
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?
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)
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.
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.
Running Java outside of
Processing
Follow these steps to run HelloWorld
1.
2.
3.
4.
5.
6.
7.
Find the editor named TextEdit and set its Preferences to Plain text
Copy and paste HelloWorld from the previous slide into the TextEdit
window.
Save it to your home directory in a file named HelloWorld.java
Capitalization matters.
Open a terminal window; you can find this under Utilities
In the terminal window, use the command cd to change to the
directory where HelloWorld.java is stored.
In the terminal window, type javac HelloWorld.java to compile your
program to byte code
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.
In-class exercise
Run 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
The Dog
public 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
Pets
public 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);
}
}