Session 1: Classes and Applets
Literature Sprechen Sie Java, ISBN 3-89864-117-1, dpunkt deutsch Java für Studenten, ISBN 3-8273-7045-0, PearsonStudium deutsch Java in a Nutshell, ISBN: 0-59600-283-1, O'Reilly english Java Examples in a Nutshell, ISBN 0-59600-039-1, O'Reilly english 3
Selbsttest: Klassen Wozu dienen Klassen? Vor-/ Nachteile von Datenabstraktion Was ist beim Aufruf einer Methode immer automatisch dabei, wenn auch versteckt? Was passiert, wenn ein Objekt mit dem Aufruf Point p = new Point(); angelegt wird? Import und Export Was kann importiert und exportiert werden? Wie wird es importiert bzw. exportiert? Welches Problem könnte man bei statischen Initialisierungsblöcken erzeugen? 4
Programmieraufgabe: Klassen Implementieren Sie eine Klasse Time zur Speicherung von Uhrzeiten. Jedes Time-Objekt soll eine Zeit in Form von Stunden und Minuten speichern. Es soll folgende Methoden geben: Sinnvoller Konstruktor Addieren einer Zeit zu einer Anderen (z.b.: 5h 42min + 3h 27min = 9h 9min) Berechnung der Differenz zwischen zwei Zeiten in Minuten. (z.b.: 5h 20min 3h 10min = 130min) Umrechnung einer Zeit in Minuten (z.b.: 3h 20min = 200min) 5
Applets Motivation? We introduce Applets first, in order to enhance the visual input and output capabilities of our sample programs! Applets can run within a Web-Browser or inside the appletviewer AppletContainer.html: <html> <head> <title>title of the web page</title> </head> <body> <applet code= AppletClassName.class width="100" height="100"></applet> </body> </html> Start with Appletviewer: 6
Applets: GUI Container An applet is derived from java.awt.container i.e. it is a container for assembling GUI-widgets: setlayout(layoutmanager mgr) add(component guicomp) remove(component guicomp) removeall() 7
Applets: AWT Example import java.applet.*; import java.awt.*; public class HelloWorldApplet extends Applet { public void init() { setlayout(new BorderLayout()); add(new Label("Hello World!")); 8
Applets: AWT The AWT package offers following GUI-widgets: Button Canvas Checkbox Choice Label List Panel Scrollbar TextArea TextField LayoutManagers (can be combined to create new layout styles) BorderLayout FlowLayout GridLayout GridBagLayout 9
Applets: AWT Events and Listeners To react on GUI-events, it is necessary to register as an EventListener EventListener has to implement an EventListenerInterface Example: Listen on button clicks Create Widget: Button b = new Button( Click me! ); Register listener with Button: b.addactionlistener(actionlistenerobject); Class of actionlistenerobject implements the ActionListener interface: XYZClass implements ActionListener { public void actionperformed(actionevent eve) { // react on event 10
Applets: Painting Two dimensional painting of points, lines, circles, images,... Painting on Applet surface with method paint(graphics g) import java.applet.*; import java.awt.*; public class PaintApplet extends Applet { public void paint (Graphics g) { g.drawline(100,100,200,200); 11
Applets: Lifecycle import java.applet.*; import java.awt.*; public class Lifecycle extends Applet { // Called when the applet has to paint its surface (Resize,...) public void paint(graphics g) { System.out.println("paint"); // Called when the applet initializes its resources public void init() { System.out.println("init"); // Here the work happens public void start() { System.out.println("start"); // Called when the applet stops (user surfs to another page,...) public void stop() { System.out.println("stop"); // Called when the applet has to free its resources (Browser closes,...) public void destroy() { System.out.println("destroy"); 12
Applets: Sandbox Applets are code that comes from the net and then runs locally at the client side. Client side code could harm the client computer (format c:, send email to everybody,...) To avoid harmful operations, Applets run under the control of a SecurityManager ("in a Sandbox") From within the Sandbox the following is not allowed: Read, write or change the local harddisk Open network connections to hosts (except to the origin host) Load any libraries from the client side... 13
Applets: 2D-Drawing Demo During development you only need to start the appletviewer once! "Reload" updates the displayed applet class. 14