Download presentation
Presentation is loading. Please wait.
Published byJewel Carr Modified over 9 years ago
1
java Spring 2009
2
PImage Let’s look at the PImage class in ProcessingPImage –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
3
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 fileimages that you need as a zip file
4
PImage’s mask() method background(255); PImage img = loadImage("airport.jpg"); PImage maskImg = loadImage("airportmask.jpg"); img.mask(maskImg); image(img, 0, 0);
5
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); }
6
PImage revisited Here’s an alternative way of looking at the documentation for PImagePImage This documentation is produced by Javadoc Javadoc Processing is implemented as a set of Java classes that accompany those normally provided with Java Can you find the line() method?
7
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)
8
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.0API for Java 1.5.0 See if you can find the String class.
9
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.
10
Running Java outside of Processing Follow these steps to run HelloWorld 1.Find the editor named TextEdit and set its Preferences to Plain text 2.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 Utilities 5.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 code 7.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.
11
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
12
The Dog public class Dog { public void bark () { 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
13
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); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.