tony valderrama, sipb iap 2009 · initialize your applet. called when the applet first loads....
TRANSCRIPT
Tony Valderrama, SIPB IAP 2009
Tony Valderrama, SIPB IAP 2009Programming in Java
• Review of Object-Oriented Programming (OOP)
• Applets!
• Threads
• tvald.applet.AnimatedApplet (on website)
• Swing/AWT
• Layout
• Event model
Announcements
Course website: http://stuff.mit.edu/iap/2009/java/
Email: [email protected]
I need suggestions for advanced topics to cover on Thursday! (see website)
Today
Tony Valderrama, SIPB IAP 2009Programming in Java
Software
Java Development Kit (JDK) - http://java.sun.com/javase/downloads/index.jsp
Eclipse Platform - http://www.eclipse.org/
Reference
The Java Tutorial - http://java.sun.com/docs/books/tutorial/index.html
Java Language API - http://java.sun.com/javase/reference/api.jsp
Java SE Documentation - http://java.sun.com/javase/downloads/index.jsp
Java SE Source Code - http://java.sun.com/javase/downloads/index.jsp
Wake up and smell the coffee!
Tony Valderrama, SIPB IAP 2009Programming in Java
Java 1.1
Microsoft develops
own VM
Timeline
1991 1992 1994 1996 1998 2000 2003 2004 2005 2006 20071993 1995 1997 1999 2001 2002
James Gosling
creates Oak
Java 1.0
goes public
Java 2 (v1.2)
Java Foundation
Class (Swing)
Java 2 (v1.3.1)
Hotspot VM
Java 2 (v1.4.2)
extensive support
packages
Java 5.0
generics, optimizations,
support packages
Java 6
open source,
optimizations
Tony Valderrama, SIPB IAP 2009Programming in Java
There were five primary goals in the creation of the Java language:
1) It should be "simple, object oriented, and familiar".
2) It should be "robust and secure".
3) It should be "architecture neutral and portable".
4) It should execute with "high performance".
5) It should be "interpreted, threaded, and dynamic".
Java was designed to be safe, simple, and powerful.
Not your daddy’s cup of Joe…
Java whitedocs, as quoted in wikipedia
Tony Valderrama, SIPB IAP 2009
Tony Valderrama, SIPB IAP 2009Programming in Java
Plato
5th-4th century B.C.
Athens, Greece
...there is a form for every object or quality in reality: forms of dogs, human beings,
mountains, colors, courage, love, and goodness. Form answers the question
"what is that?" ...the object was essentially or "really" the Form and that the
phenomena were mere shadows mimicking the Form; that is, momentary
portrayals of the Form under different circumstances. The problem of universals -
how can one thing in general be many things in particular - was solved by
presuming that Form was a distinct singular thing but caused plural
representations of itself in particular objects.
A bit of philosophy…
http://en.wikipedia.org/wiki/Theory_of_Forms
Tony Valderrama, SIPB IAP 2009Programming in Java
A class is a template, blueprint, or prototype from which objects are created.
<modifiers> class name {
// members: fields, methods, constructors
}
What is a Class?
Tony Valderrama, SIPB IAP 2009Programming in Java
Inheritance
abstract
changeGear()
getSpeed()
setSeatHeight() addSecondRider()
instances
ObjectgetClass()
finalize()
...
Tony Valderrama, SIPB IAP 2009Programming in Java
Constructors
class Bicycle {
int gear,speed;
public Bicycle(int startSpeed, int startGear) {
gear = startGear;
speed = startSpeed;
}
public Bicycle() { this(1, 0); }
public static void main(String[] args) {
Bicycle myBike = new Bicycle(0, 8);
Bicycle otherBike = new Bicycle();
}
}
modifiers classname (parameter list) throws exceptionlist {
// method body
}
Tony Valderrama, SIPB IAP 2009Programming in Java
Plato
5th-4th century B.C.
Athens, Greece
A Form is aspatial (outside the world) and atemporal (outside time). Atemporal
means that it does not exist within any time period. It did not start, there is no
duration in time, and it will not end. It is neither eternal in the sense of existing
forever or mortal, of limited duration. It exists outside time altogether. Forms are
aspatial in that they have no spatial dimensions, and thus no orientation in space,
nor do they even (like the point) have a location… A Form is an objective
"blueprint" of perfection.
A bit of philosophy…
http://en.wikipedia.org/wiki/Theory_of_Forms
Tony Valderrama, SIPB IAP 2009Programming in Java
From beans to brew
Images from the Java Tutorial
Tony Valderrama, SIPB IAP 2009Programming in Java
• instantiation
• do stuff
• instantiate and manipulate other objects
• serialize and send over network
• interact with system libraries
• garbage collection
• public void finalize()
Life cycle of an object
Tony Valderrama, SIPB IAP 2009Programming in Java
Java is Object-Oriented from the ground up.
EVERYTHING is an Object. Even primitives can be wrapped in Objects.
Objects can have a lifetime greater than the object that created them.
An Object-Oriented language should support:
• Encapsulation - information hiding and modularity (abstraction)
• Polymorphism - behavior is dependent on the nature of the object receiving a
message
• Inheritance - new classes are defined based on existing classes to obtain
code re-use and organization
• Dynamic binding - objects could come from anywhere, possibly across the
network. Send messages to objects without knowing their specific type at the
time you write your code.
Object-Oriented Programming
Tony Valderrama, SIPB IAP 2009Programming in Java
int i = 2;
Integer intObject;
intObject = i; // autoboxing automatically converts primitives
intObject = new Integer(i); // equivalent, but unnecessary
i = new Integer(4); // unnecessarily circuitous, but it works
// also: Boolean, Byte, Double, Character, etc.
Autoboxing
Tony Valderrama, SIPB IAP 2009Programming in Java
Inheritance vs. Composition
Tony Valderrama, SIPB IAP 2009Programming in Java
An interface is a contract between a class and the outside world. When a class implements
an interface, it promises to provide the behavior published by that interface.
interface Bicycle {
// constant declarations, if any
public static final MAX_GEAR = 10;
void changeGear(int newValue); //interfaces are completely abstract
void speedUp(int increment);
public abstract void applyBrakes(int decrement); // implied
}
class ACMEBicycle implements Bicycle {
// remainder of this class implemented as before
}
What is an Interface?
Tony Valderrama, SIPB IAP 2009Programming in Java
Interfaces
Tony Valderrama, SIPB IAP 2009
Tony Valderrama, SIPB IAP 2009Programming in Java
A special kind of Java application that can be loaded into a web browser.
Applets
<html>
<body>
<applet code="HelloWorld.class" width="200" height="200"></applet>
<body>
</html>
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawRect(0, 0,
getSize().width - 1,
getSize().height - 1);
g.drawString("Hello world!", 5, 15);
}
}
demo.html
HelloWorld.java
Tony Valderrama, SIPB IAP 2009
Graphics
java.awt.Graphics;
...
public void paint(Graphic g) {
g.drawLine(x1,y1,x2,y2);
g.drawRect( x, y, w, h);
g.fillRect( x, y, w, h);
g.drawOval( x, y, w, h);
g.fillOval( x, y, w, h);
g.drawString(x, y);
g.setColor(new Color(r, g, b));
}
x
y
Tony Valderrama, SIPB IAP 2009Programming in Java
init:
Initialize your applet. Called when the applet first loads.
start:
Called whenever the user visits the page containing the applet.
paint:
This method updates the graphical interface.
stop:
Called whenever the user moves away from the page containing applets.
destroy:
Clean up (like finalize). Called when the applet is unloaded.
Life cycle of an Applet*
* Not really
Tony Valderrama, SIPB IAP 2009Programming in Java
init, start: Called upon navigation to page
paint: Called when display is stale
stop, destroy: Called upon leaving page
Static fields may persist between visits.
(Yes, browsers suck at compatibility and at following specifications…)
The real life cycle of an Applet
Tony Valderrama, SIPB IAP 2009Programming in Java
// retrieve an Image
java.awt.Image image = getImage(getCodeBase(), "imgDir/a.gif");
g.drawImage(image, x, y);
// retrieve a file
File f = new File("config.txt");
// all file paths are resolved from the Applet source directory
Files
Tony Valderrama, SIPB IAP 2009Programming in Java
• http://scb.freehomepage.com/games.html
• http://athena.dialup.mit.edu/ssh.html
• http://web.mit.edu/tvald/www/Applets/Game_of_Life.html
Examples
Tony Valderrama, SIPB IAP 2009Programming in Java
Process: a self-contained execution environment, usually with its own memory
space.
Thread: a lightweight process; a thread exists within a single process, sharing
processor resources
http://www.doc.ic.ac.uk/~jnm/concurrency/classes/ThreadDemo/ThreadDemo.html
Threads
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
Tony Valderrama, SIPB IAP 2009Programming in Java
Process: a self-contained execution environment, usually with its own memory
space.
Thread: a lightweight process; a thread exists within a single process, sharing
processor resources
http://www.doc.ic.ac.uk/~jnm/concurrency/classes/ThreadDemo/ThreadDemo.html
Threads
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
Tony Valderrama, SIPB IAP 2009Programming in Java
Thread.sleep(long millis
t.join(); // wait for the thread to finish
// synchronization:
// neither of these can be called at the same time.
// a second call will cause the calling Thread to
// block until it execution has completes
public synchronized foo();
public synchronized bar();
// lock an object
public void foo() {
// locks object
synchronized(object) {
statement(s)...
}
statement(s)...
}
Dealing with traffic
Tony Valderrama, SIPB IAP 2009
Tony Valderrama, SIPB IAP 2009Programming in Java
What’s in the cup?
Images from the Java Tutorial
http://java.sun.com/docs/books/tutorial/ui/features/index.html
http://java.sun.com/docs/books/tutorial/uiswing/index.html
Tony Valderrama, SIPB IAP 2009