Outline • Packages – Declaration & Import – CLASSPATH – Visibility
• Event-Driven Programming • AWT/Swing: Containers
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
1
OOP Principles • Encapsulation – Methods and data are combined in classes – Not unique to OOP: Ada supports class-based programming
• Information Hiding – Implementation of a class can be changed without affecting clients – Not unique to OOP: Ada supports information hiding through packages – Java: Packages (set of cooperating classes) Visibility declarations
• Inheritance – Classes can be adapted through inheritance
• Polymorphism – Substitution rule: Subtypes must fulfill the contract of its base type 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
2
Packages • Definition – Package = collection of cooperating classes
• Purpose – Organization of programs – Control of access rights (who may access what) – Prevention of name conflicts
• Examples Package – java.lang – java.io – java.util
Contained Classes String, Math, Object, System, … File, Reader, Writer, InputStream, OutputStream, … ArrayList, LinkedList, Date, …
List of all packages:
http://docs.oracle.com/javase/8/docs/api/
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
3
Packages: Declaration • Declaration – Package declaration at beginning of each compilation unit Figure.java package ch.ethz.jdraw; class Figure { ... }
package ch.ethz.jdraw;
Circle.java package ch.ethz.jdraw; class Circle extends Figure { ... }
– Convention: inverted internet domain names – When package declaration is missing, then the class belongs to a nameless standard package (=> every class belongs to a package) – Package names may be nested (but no special relation between them) java.util java.util.concurrent 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
4
Packages: Folder Structure • Mapping – Packages are mapped to directories – Classes are mapped to files Each public class has to be declared in its own source file package ch.ethz.jdraw; public class Figure { ... }
package ch.ethz.jdraw; public class Circle extends Figure { ... }
ch ethz jdraw
Figure.java Circle.java
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
5
Packages: Folder Structure ch.ethz.Test ch Test.class
ethz Complex.class
tools
Model.class
jdraw
Test.class figures
ch.ethz.tools.Test
Rect.class Circle.class
ch.ethz.jdraw.figures.Circle 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
6
Packages: Folder Structure • Compilation of a class in a package javac ch\ethz\tools\Complex.java javac ch\ethz\tools\*.java
• Execution of a class defined in a package java ch.ethz.tools.Test java ch/ethz/tools/Test
• CLASSPATH – classpath has to refer to root directories which contain the packages – default classpath: current directory (“.”) javac -classpath .;tools.jar;classes *.java java -cp .;inout.jar;classes Test 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
7
Packages: Namespaces • Name Conflicts package one;
package two;
class C { ... }
class C { ... }
class D { C ref }
– Same name for class C in both packages is not a problem Fully qualified names of the two classes are one.C and two.C Programmers need not be aware of names used in other packages If another class is references without full qualification (as C in class one.D), then the class declared in the same package is used
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
8
Packages: Import of Elements • Using classes from another package – Qualified name
ch.ethz.tools.Complex
– Import of a class
import ch.ethz.tools.Complex; After the import declaration this class can be accessed with the name Complex
– Import of all public classes of a package import ch.ethz.tools.*; Classes of sub-packages are not imported!
• Remark – Package java.lang is imported automatically – Java packages are not that powerful than Ada packages or Modula modules. New classes can be added at any time! 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
9
Packages: Import of Elements • Qualified Name package ch.ethz.jdraw.test; class Test { ch.ethz.jdraw.Figure f = new ch.ethz.jdraw.Circle(); }
• Import of a class package ch.ethz.jdraw.test; import ch.ethz.jdraw.Figure; import ch.ethz.jdraw.Circle class Test { Figure f = new Circle(); }
• Import of all public classes of a package package ch.ethz.jdraw.test; import ch.ethz.jdraw.*; class Test { Figure f = new Circle(); } 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
10
Visibility • private – access only from within the same class (but across objects of that class) – fields are usually declared private
• internal (no declaration modifier) – access only from within the same package
• protected – access from within the same package and from all subclasses (even if they are declared in other packages)
• public – access from anywhere
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
11
Visibility • Classes – Classes can be declared public or internal Each public class has to be declared in its own source file Internal classes can be declared in any source file
• Fields, Methods – Methods and fields can be declared private, internal, protected or public
• Constructors – Constructors can be declared private, internal, protected or public – A class with private constructors only Cannot be extended Can only be instantiated from within the class itself
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
12
Package A
Package B
public class SuperClass { public void function1() {} void funciton2() {} protected void function3 () {} private void function5 () {} }
public class SuperClass {
}
public class SubclassA extends SuperClass {
public class SubclassB extends SuperClass {
}
}
public class SubclassA extends A.SuperClass { }
public class SubclassB extends SuperClass { }
class OtherClass {
class OtherClass {
}
}
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
Package A
Package B
public class SuperClass { public void function1() {} void funciton2() {} protected void function3 () {} private void function5 () {} }
public class SuperClass {
}
public class SubclassA extends SuperClass {
public class SubclassB extends SuperClass {
}
}
public class SubclassA extends A.SuperClass { }
public class SubclassB extends SuperClass { }
class OtherClass {
class OtherClass {
}
}
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
Package A
Package B
public class SuperClass { public void function1() {} void funciton2() {} protected void function3 () {} private void function5 () {} }
public class SuperClass {
}
public class SubclassA extends SuperClass {
public class SubclassB extends SuperClass {
}
}
public class SubclassA extends A.SuperClass { }
public class SubclassB extends SuperClass { }
class OtherClass {
class OtherClass {
}
}
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
Package A
Package B
public class SuperClass { public void function1() {} void funciton2() {} protected void function3 () {} private void function5 () {} }
public class SuperClass {
}
public class SubclassA extends SuperClass {
public class SubclassB extends SuperClass {
}
}
public class SubclassA extends A.SuperClass { }
public class SubclassB extends SuperClass { }
class OtherClass {
class OtherClass {
}
}
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
Package A
Package B
public class SuperClass { public void function1() {} void funciton2() {} protected void function3 () {} private void function5 () {} }
public class SuperClass {
}
public class SubclassA extends SuperClass {
public class SubclassB extends SuperClass {
}
}
public class SubclassA extends A.SuperClass { }
public class SubclassB extends SuperClass { }
class OtherClass {
class OtherClass {
}
}
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
Package A
Package B
public class SuperClass { public void function1() {} void funciton2() {} protected void function3 () {} private void function5 () {} }
public class SuperClass {
}
public class SubclassA extends SuperClass {
public class SubclassB extends SuperClass {
}
}
public class SubclassA extends A.SuperClass { }
public class SubclassB extends SuperClass { }
class OtherClass {
class OtherClass {
}
}
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
Package A
Package B
public class SuperClass { public void function1() {} void funciton2() {} protected void function3 () {} private void function5 () {} }
public class SuperClass {
}
public class SubclassA extends SuperClass {
public class SubclassB extends SuperClass {
}
}
public class SubclassA extends A.SuperClass { }
public class SubclassB extends SuperClass { }
class OtherClass {
class OtherClass {
}
}
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
Visibility: Remarks • Private – Different semantics than private for humans class Person { private double assets; public static void clearAssets(Person c) { c.assets=0; } } private in Java means, that the field is accessible inside the class on all instances
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
20
Visibility: Remarks • Private – The following implementation of equals is possible class Rectangle { private Point origin; private int width, height; public boolean equals(Object x) { if(x instanceof Rectangle) { Rectangle r = (Rectangle) x; return this.width == r.width && this.height == r.height && this.origin.equals(r.origin); } else { return false; } } }
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
21
Puzzle • How is the following identifier to interpret? A.B.C.D
– We ignore the usual Java programming convention that only class names start with capital letters. – Several interpretations are possible !!!
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
23
Outline • Packages • Event-Driven Programming – – – –
Introduction Events & Listener AWT / Swing Components Implementing Listener
• AWT/Swing: Containers
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
24
Graphical User Interfaces (GUI) • GUI contains elements – … which can be presented in a window – … which can react on events (mouse, keys)
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
25
Java GUIs • AWT (Abstract Windowing Toolkit)
since JDK 1.0
– Uses native controls Appearance / behavior depends on platform Least common denominator of functionality
• Swing
since JDK 1.2
– Implemented completely in Java (light weight) Several “Look & Feel” modes on all platforms available More features (tool tips, icons, table- & tree-control)
• JavaFX
since JDK 8
– JavaFX 2.0 is written as native library – Provided on a wide variety of devices – Intended to replace Swing as standard GUI
• SWT – Standard Widget Toolkit, Eclipse 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
26
Event Driven Programming • Events = User Interactions – Mouse click – Key pressed – Menu selection
• Event Driven Programming – Definition of actions to be performed upon events
11 March 2016
key pressed?
handle event
mouse pressed?
handle event
mouse moved?
handle event
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
27
Event Driven Programming with AWT/Swing • Events class XXXEvent extends EventObject { … }
• Event Handler interface XXXListener { void handleEvent(XXXEvent e); }
• Registration in a GUI control addXXXListener(XXXListener l); removeXXXListener(XXXListener l);
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
28
Event Driven Programming with AWT/Swing • Events class ActionEvent extends EventObject { … }
• Event Handler interface ActionListener { void actionPerformed(ActionEvent e); }
• Registration in a GUI control addActionListener(ActionListener l); removeActionListener(ActionListener l);
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
29
Example: ActionEvent import java.awt.*; import java.awt.event.*; public class ButtonApp extends Frame { public static void main(String[] args) { Frame f = new Frame(); Button b = new Button("Beep"); b.addActionListener(new ButtonActionListener()); f.add(b); f.pack(); f.setVisible(true); } }
class ButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent e){ System.out.println("Beep pressed"); } } 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
30
Component Component • JComponent – abstract base class of the non-menu-related AWT components class Component JComponent{{ getWidth getHeight get/setSize get/setLocation get/setBounds() is/setEnabled is/setVisible get/setCursor getPreferrredSize get/setPreferrredSize getMinimumSize get/setMinimumSize getMaximumSize get/setMaximumSize get/setToolTipText
: int : int : : : : : : : : : :
Dimension Point Rectangle boolean boolean Cursor Dimension Dimension Dimension String
} 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
31
Components and their Listeners (AWT) ComponentListener FocusListener
Button
CheckBox
Component
Choice
Label
KeyListener M ouseListener M ouseM otionListener
List
Scrollbar
Canvas
TextComponent
TextArea
TextField
ItemListener TextListener AdjustmentListener
ActionListener 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
32
Components and their Listeners (Swing) ComponentListener FocusListener
AbstractButton
JComponent
JTextComponent
JButton JToggleButton
JTextArea
KeyListener M ouseListener M ouseM otionListener
JLabel
JTextField
JList
JScrollBar
JComboBox
PopupMenuListener
JEditorPane
JTextPane JCheckBox
JRadioButton
AdjustmentListener
InputMethodListener ItemListener
ListSelectionListener
ActionListener 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
33
Implementing Listeners • Implementing listeners directly public class StackFrame extends Frame implements ActionListener { Button push = new Button("Push"); Button pop = new Button("Pop"); public void actionPerformed(ActionEvent e) { if(e.getSource() == push) handlePush(); else if(e.getSource() == pop) handlePop(); }
StackFrame() { push.addActionListener(this); pop.addActionListener(this); … } }
11 March 2016
ActionListener
StackFrame actionPerformed handlePush handlePop
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
34
Implementing Listeners • Implementing listeners using adapter classes public class StackFrame extends Frame { Button push = new Button("Push"); Button pop = new Button("Pop"); StackFrame() { push.addActionListener(new PushListener()); pop.addActionListener(new PopListener()); … }
} class PushListener implements ActionListener { public void actionPerformed(ActionEvent e){ … } } class PopListener implements ActionListener { public void actionPerformed(ActionEvent e){ … } } 11 March 2016
ActionListener
PushListener actionPerformed
ActionListener
PopListener actionPerformed StackFrame handlePush handlePop (C) Hochschule für Technik Fachhochschule Nordwestschweiz
35
Implementing Listeners • Implementing listeners using lambda expressions public class StackFrame extends Frame { Button push = new Button("Push"); Button pop = new Button("Pop"); StackFrame(){ push.addActionListener(e -> handlePush(e)); pop.addActionListener(e -> handlePop(e)); … }
}
• Lambda expressions – An interface with exactly one abstract method is a functional interface – Functional interfaces can be instantiated using lambda expressions Avoids bulky anonymous class implementation. 11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
36
Example: BallGame class BallField1 extends JComponent implements MouseListener { private int x = 50, y = 50, r = 10; public BallField1() { addMouseListener(this); } public void paintComponent (java.awt.Graphics g) { g.setColor(java.awt.Color.red); g.fillOval(x-r, y-r, 2*r, 2*r); } @Override public void mouseClicked(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited (MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); repaint(); } @Override public void mouseReleased(MouseEvent e) { } }
11 March 2016
(C) Hochschule für Technik Fachhochschule Nordwestschweiz
37
Example: BallGame • repaint – Causes a call to this component's paint method as soon as possible (=> application triggered painting)
• Painting Guidelines – – – –
Client paint code should be placed within the paint method Programs may trigger a future call to paint by invoking repaint Programs should not call paint directly On components with complex output, repaint() should be invoked with arguments which define the rectangle that needs updating – Swing supports double buffering by default – Default implementation of paint(Graphics g) paintComponent(g) paintBorder(g) paintChildren(g) 11 March 2016
//