API
UMBC CMSC 331 Java
A Tour of the Java API
• An API User’s Guide, in HTML, is bundled with
Java
• Much of the “learning curve” is in the API
• Let’s look at some packages
See http://java.sun.com/j2se/1.3/docs/api/
for the current APIs
UMBC CMSC 331 Java 3
UMBC CMSC 331 Java 4
UMBC CMSC 331 Java 5
UMBC CMSC 331 Java 6
The package java.lang
• The class Object
– The root class in Java
– Example methods: clone(), equals(),
toString()
– Subclasses may override these methods
• The class Class
– Example methods: getName(),
getSuperClass()
java.ext.dirs=C:\JDK1.2\JRE\lib\ext
java.io.tmpdir=C:\WINDOWS\TEMP\
os.name=Windows 95
java.vendor=Sun Microsystems Inc.
java.awt.printerjob=sun.awt.windows.WPrinterJob
java.library.path=C:\JDK1.2\BIN;.;C:\WINDOWS\SYSTEM;C:\...
java.vm.specification.vendor=Sun Microsystems Inc.
sun.io.unicode.encoding=UnicodeLittle
file.encoding=Cp1252
java.specification.vendor=Sun Microsystems Inc.
user.language=en
user.name=nicholas
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
java.vm.name=Classic VM
java.class.version=46.0
java.vm.specification.name=Java Virtual Machine Specification
sun.boot.library.path=C:\JDK1.2\JRE\bin
os.version=4.10
java.vm.version=1.2
java.vm.info=build JDK-1.2-V, native threads, symcjit
java.compiler=symcjit
path.separator=;
file.separator=\
user.dir=C:\UMBC\331\java
sun.boot.class.path=C:\JDK1.2\JRE\lib\rt.jar;C:\JDK1.2\JR...
user.name=nicholas
user.home=C:\WINDOWS
C:\UMBC\331\java>
Java GUI
• The awt class allows you to create
– frames
– buttons
– menus and menubars
– checkboxes
– text areas
– scrolling lists
// The text
g.setColor(Color.black);
g.setFont(font);
g.drawString(message, 40, 75);
}
}
Scribble
• Graphics objects can be added to applets, e.g.
buttons and menus
• Events, such as mouse clicks, are handled
// Called when the user scribbles with the mouse button down
public boolean mouseDrag(Event e, int x, int y) {
Graphics g = this.getGraphics();
g.setColor(current_color);
g.drawLine(last_x, last_y, x, y);
last_x = x;
last_y = y;
return true;
}
// Called when the user clicks the button or chooses a color
public boolean action(Event event, Object arg) {
// Read and print out the first five lines of the URL.
System.out.println("First five lines:");
DataInputStream in = new DataInputStream(u.getInputStream());
for(int i = 0; i < 5; i++) {
String line = in.readLine();
if (line == null) break;
System.out.println(" " + line);
}
}
}
UDP Examples
• UDP - Unreliable datagram packet
• Examples from Flanagan
– Send a UDP packet
– Receive a UDP packet
import java.io.*;
import java.net.*;
import java.awt.*;
Thread thisThread;
int thisThreadID;