Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Java Advanced Level
day 2 Part A
Design Patterns II
Athanasios Tsintsifas, LTR, University of Nottingham,
email : azt@cs.nott.ac.uk, http: www.cs.nott.ac.uk/~azt
Aims
Design patterns part II :
– Null,
– Singleton,
– Observer,
– Strategy,
– Template.
Quick review of the rest.
Combining Design patterns.
Null Object
Client
Interface
request()
RealObject
NullObject
request()
request()
do nothing
Provides a no-behaviour placeholder object to be used
instead of language specific keywords like “null” and
“nil”.
Null Object
Where it can be used :
Where an object requires a collaborator object which does nothing.
Where there is a need to ignore the difference between a
collaborator that does nothing and a real collaborator.
Examples of use :
Applications where null is to be handled as an object that does
nothing. (AWT’s layout policy?)
Smalltalk’s MVC (Model-View-Controller).
Benefits :
Code doesn’t have to specifically handle a null reference.
Null behaviour is reusable and the null object can be shared.
Null Object Example (1/2)
public interface Logger {
public void log(String msg);
}
import java.io.*;
class FileLogger implements Logger {
File myLogFile = new File("Applog.txt");
public void log(String msg) {
try {
FileWriter myFW = new FileWriter(myLogFile);
myFW.write(msg);
myFW.close();
} catch(IOException e) {
System.out.println("Exception while writing to
log");
}
}
}
Null Object Example (2/2)
class NullLogger implements Logger {
public void log(String msg) {
//log nothing
}
}
public class Example {
static Logger myLogger;
public static void main(String argv[]) {
myLogger = new FileLogger();
myLogger.log("Application Started");
myLogger = new NullLogger();
myLogger.log("Application about to terminate");
}
}
Observer
while (e.hasMoreElements()) {
Observer o = (Observer)
e.nextElement();
o.update(this);
}
Subject
attach()
detach()
notify()
Observer
observers
update()
ConcreteSubject
state = newState;
notify();
getState()
setState()
subject
update()
A one-to-many dependency between objects so that when
an object changes its state. It notifies its dependent
objects.
Observer
Where it can be used :
Where an abstraction has two aspects depending on each other.
Where a change to one object requires changing others.
Where an object should notify others without knowing what these
other objects are.
Examples of use :
Java’s JFC.
Smalltalk’s MVC (Model-View-Controller).
Benefits :
Subject and observers may vary independently.
Any number of observers can be defined and added.
Different observers provide different views of the change.
Observer Example (1/4)
public interface FileSystemObserver {
public final static int FILE_ADDED = 1;
public final static int FILE_REMOVED = 2;
public void notify(int application, int event);
}
import java.util.*;
class FileSystemNotifier {
private Vector myObservers = new Vector();
public void addObserver(FileSystemObserver observer) {
myObservers.addElement(observer);
}
public void removeObserver(FileSystemObserver observer) {
myObservers.removeElement(observer);
}
public void notify(int application, int event) {
for (Enumeration e = myObservers.elements();
e.hasMoreElements();) {
((FileSystemObserver)e.nextElement()).notify(application,
event);
}
}
Observer Example (2/4)
class FileSystemAdapter implements FileSystemObserver {
private FileSystemMonitor myFSMonitor;
FileSystemAdapter(FileSystemMonitor fsMonitor) {
myFSMonitor = fsMonitor;
}
public void notify(int application, int event) {
switch (event) {
case FILE_ADDED:
case FILE_REMOVED:
myFSMonitor.refresh(application);
break;
}
}
}
Observer Example (3/4)
import java.util.*;
public class FileSystemMonitor {
private Vector myApps = new Vector();
public void addApplication(int app) {
myApps.addElement(new Integer(app));
}
public void removeApplication(int app) {
myApps.removeElement(new Integer(app));
}
public void refresh(int application) {
for (Enumeration e = myApps.elements();
e.hasMoreElements();) {
Integer tempApp = (Integer)e.nextElement();
if (tempApp.intValue() == application) {
System.out.println("Sending refresh message
to application " + application);
}
}
}
}
Observer Example (4/4)
public class Example {
public static
int app1
int app2
int app3
void main(String argv[]) {
= 101;
= 203;
= 405;
FileSystemMonitor fsmonitor = new FileSystemMonitor();
fsmonitor.addApplication(app1);
fsmonitor.addApplication(app2);
fsmonitor.addApplication(app3);
FileSystemNotifier fsnotifier = new FileSystemNotifier();
FileSystemObserver fsobserver = new
FileSystemAdapter(fsmonitor);
fsnotifier.addObserver(fsobserver);
fsnotifier.notify(app1,FileSystemObserver.FILE_ADDED);
fsnotifier.notify(app3,FileSystemObserver.FILE_REMOVED);
}
}
Singleton
Singleton
static instance
method()
method()
static instance()
Ensures that a class has one instance only and provides
an access point to it for all other classes.
Singleton
Where it can be used :
Where there must be only one instance of a specific class.
Where that only one instance should be extensible by subclassing.
Examples of use :
Monitoring the usage of a class and its objects.
Ensure a single instance - Security Manager.
All kind of registries/managers.
Benefits
Security - Robustness - Reduced name space pollution.
Singleton Example (1/2)
public class ObjectMonitor {
private static ObjectMonitor instance = new
ObjectMonitor();
private int acceptedRQs = 0;
private int deniedRQs = 0;
private boolean inUse = false;
private ObjectMonitor() {}
public static ObjectMonitor getInstance() {
return instance;
}
public int numOfAcceptedRequests() {
return acceptedRQs;
}
public int numOfDeniedRequests() {
return deniedRQs;
}
Singleton Example (2/2)
public boolean requestLock() {
if (inUse == false) {
acceptedRQs++;
inUse = true;
return true;
} else {
deniedRQs++;
return false;
}
}
public void releaseLock() {
inUse = false;
}
}
Strategy
StrategyContext
algorithm()
strategy()
Strategy
algorithm()
ConcreteStrategyA
ConcreteStrategyB
Defines a family of algorithms (as objects) and makes
them interchangeable so that clients can vary their
execution.
Strategy
Where it can be used :
Where an object should be configurable with one of several
algorithms.
Examples of use :
Layout Policy for AWT/JFC containers.
Benefits
Greater flexibility and reuse; easy to add new algorithms/behaviours.
Dynamic switching of algorithms.
Strategies encapsulate private data of algorithms.
Strategy Example (1/4)
public abstract class Compilation {
abstract public String getCompilationCommand(String
program);
}
Strategy Example (2/4)
public class JavaCompilation extends Compilation {
public String getCompilationCommand(String program) {
String cmd;
String prop;
prop = System.getProperty("os.name");
String NTjavaCompiler = "c:\\jdk1.2\\bin\\javac.exe";
String UNIXjavaCompiler = "/usr/bin/jdk1.2/bin/javac";
if (prop.equals("Windows NT")) {
cmd = new String(NTjavaCompiler + " " + program);
}
else {
//Presumes UNIX
cmd = new String(UNIXjavaCompiler + " " + program);
}
return cmd;
}
}
Strategy Example (3/4)
public class CppCompilation extends Compilation {
public String getCompilationCommand(String program) {
String cmd;
String prop;
prop = System.getProperty("os.name");
String NTcppCompiler = "c:\\gnu\\bin\\g++.exe";
String UNIXcppCompiler = "/usr/bin/gnu/gcc";
if (prop.equals("Windows NT")) {
cmd = new String(NTcppCompiler + " " + program);
} else {
//Presumes UNIX
cmd = new String(UNIXcppCompiler + " -o " + program);
}
return cmd;
}
}
Strategy Example (4/4)
public class Example {
public static void main(String argv[]) {
String program = "Example.java";
String compilationCommand = getCommand(program);
System.out.println(compilationCommand);
program = "Example.cpp";
compilationCommand = getCommand(program);
System.out.println(compilationCommand);
}
public static String getCommand(String program) {
Compilation comp;
if (program.endsWith("java")) {
comp = new JavaCompilation();
return comp.getCompilationCommand(program);
}
if (program.endsWith("cpp")) {
comp = new CppCompilation();
return comp.getCompilationCommand(program);
}
return null;
}
}
Template Method
AbstractClass
method1()
method2()
method3()
primitive1()
primitive2()
…
Primitive1();
…
Primitive2();
DerivedClass()
primitive1()
primitive2()
Defines a skeleton of an algorithm in an operation while
deferring some steps to subclasses.
Template Method
Where it can be used :
Where a portion of an algorithm to be used in a class is dependant
on the type of an object of its subclasses.
In systems that go through a re-factoring stage.
Examples of use
Any algorithm that depends on the type of object it is applied to.
Benefits
Greater flexibility and reuse.
Easy to modify an algorithm to take into account other types of
objects (of the same hierarchy).
Template Method Example (1/7)
import java.io.*;
public abstract class Compilation {
String[] myFiles;
public boolean setupEnvironment(String[] files) {
myFiles = files;
// check that files exist
for (int i=0; i<myFiles.length; i++) {
File testFile = new File(myFiles[i]);
if (!testFile.exists()) {
return false;
}
}
return true;
}
Template Method Example (2/7)
abstract public String getCompilationCommand(String
program);
public void compile() {
for (int i=0; i< myFiles.length; i++) {
String compilationCommand =
getCommand(myFiles[i]);
System.out.println(compilationCommand);
// now compile
}
}
private String getCommand(String program) {
return getCompilationCommand(program);
}
}
Template Method Example (3/7)
public class JavaCompilation extends Compilation {
private String program;
public JavaCompilation(String prg) {
program = prg;
}
public JavaCompilation() { }
public String getCompilationCommand(String prg) {
program = prg;
return getCompilationCommand();
}
Template Method Example (4/7)
public String getCompilationCommand() {
String cmd;
String prop;
prop = System.getProperty("os.name");
String NTjavaCompiler = "c:\\jdk1.2\\bin\\javac.exe";
String UNIXjavaCompiler = "/usr/bin/jdk1.2/bin/javac";
if (prop.equals("Windows NT")) {
cmd = new String(NTjavaCompiler + " " + program);
}
else {
//Presumes UNIX
cmd = new String(UNIXjavaCompiler + " " + program);
}
return cmd;
}
}
Template Method Example (5/7)
public class CppCompilation extends Compilation {
private String program;
public CppCompilation(String prg) {
program = prg;
}
public CppCompilation() { }
public String getCompilationCommand(String prg) {
program = prg;
return getCompilationCommand();
}
Template Method Example (6/7)
public String getCompilationCommand() {
String cmd;
String prop;
prop = System.getProperty("os.name");
String NTcppCompiler = "c:\\gnu\\bin\\g++.exe";
String UNIXcppCompiler = "/usr/bin/gnu/gcc";
if (prop.equals("Windows NT")) {
cmd = new String(NTcppCompiler + " " + program);
} else {
//Presumes UNIX
cmd = new String(UNIXcppCompiler + " -o " +
program);
}
return cmd;
}
}
Template Method Example (7/7)
public class Example {
public static void main(String argv[]) {
String[] javaApp =new String[2];
javaApp[0] = "TestExample.java";
javaApp[1] = "TestMain.java";
Compilation javacomp = new JavaCompilation();
if (javacomp.setupEnvironment(javaApp)) {
javacomp.compile();
}
String[] cppApp = new String[3];
cppApp[0] = "TestMain.cpp";
cppApp[1] = "TestIO.cpp";
cppApp[2] = "TestLog.cpp";
Compilation cppcomp = new CppCompilation();
if (cppcomp.setupEnvironment(cppApp)) {
cppcomp.compile();
}
}
}
The Rest of the Design Patterns
– Flyweight, allows large number of small objects to be shared.
– Chain of Responsibility, gives a chance to many handlers to handle a request.
– Visitor, allows to append operations to an object structure without changing its
class structure.
– Mediator, encapsulates the way a set of objects interact.
– Façade, provides a single interface to a set of classes or to a subsystem.
– Builder, separates the building of a complex object from its representation.
– Bridge, separates an abstraction from its implementation.
– Prototype, provides a cheaper alternative to object creation.
– Proxy, plays the role of a placeholder for other objects.
– Interpreter, uses inheritance and composition to build the syntax tree.
– Iterator, encapsulates the traversal of a structure.
– State, encapsulates an object’s state in a separate object.
– Memento, captures an object’s state without violating encapsulation.
– Generation Gap, provides a clean way to customise generated source code.
Combining Design Patterns
Decorator
Mediator
Chain
Adapter
Proxy
Observer
Memento
Command
Iterator
Composite
Flyweight
Strategy
Builder
Visitor
Prototype
Interpreter
Factory
Template
State
Singleton
Abstract
Factory
Bridge
Facade
Summary
Design Patterns part II :
– Null,
– Singleton,
– Observer,
– Strategy,
– Template.
Quick review of the rest 13 design patterns.
Combining design patterns.
java.util
Vector
app
TestProgram
java.lang
Object
Quiz
figures
Printable
Figure
Drawable
Line
Circle
Rectangle
Can you detect anywhere a need for any of the following patterns :
Null, Singleton, Observer, Strategy,Template ?
Exercises
Choose one of the design patterns presented in this
section and implement it in the context of yesterday’s
figure drawing example. What would you have used
instead, to provide the same functionality? Can you detect
the advantages and disadvantages of using the pattern in
that context?
Design and implement a simple thermometer class that uses
the Observer pattern to register its views. On change, the
thermometer object should notify its dependent views to update
accordingly. You can use the command pattern to change the
temperature value of the thermometer.
15.3
15.3 observers
observable
Thermometer
fTemperature 15.3
15.3C
notifies on change
Java, Advanced Level
day 2 Part B
OO Frameworks and
Design patterns in Java’s APIs
Athanasios Tsintsifas, LTR, University of Nottingham,
email : azt@cs.nott.ac.uk, http: www.cs.nott.ac.uk/~azt
Aims
Introduction to OO Frameworks.
Design patterns in Java’s API’s :
– 1.java.awt :
•
•
•
•
Composite,
Strategy,
Abstract Factory,
Template,
– 2.java.io :
• Decorator,
– 3.java.util :
• Iterator, Observer.
OO Frameworks
“A Framework is a reusable, semi-complete application that can
be specialised to produce custom applications.
[Johnson - Foote 1988]
In contrast with Class Libraries, frameworks contain design
decisions for :
– particular business units,
– application domains.
Frameworks invert the control of the application and promote the
Hollywood principle : “Don’t call us, we’ll call you!”
Famous Frameworks :
– MacApp, InterViews, Unidraw, ET++, MFC, DCOM, RMI, CORBA.
OO Frameworks
Frameworks are represented by a set of interfaces, abstract
classes and collaboration specifications of their instances.
Applications can get built on a framework by :
• Overriding,
• Parameterising,
• Configuring,
• Modifying.
Benefits :
– Modularity, Reusability, Extensibility, Control Inversion.
OO Framework Types
Flexibility - Extendibility
White Box
–
–
–
–
–
Override to Customise,
Internal structure exposed,
Easier to design,
Harder to learn,
More implementation.
Encapsulation
Black Box
–
–
–
–
–
Configure to Customise,
Interfaces exposed,
Complex to design,
Easier to learn,
Less implementation.
Learning to use a Framework
Frameworks are complex.
Can’t learn one class at a time.
The important classes are the abstract classes and
interfaces.
Learning by example and using a Cookbook seem to
work best.
Creating Frameworks
Highly iterative process that requires a deep
understanding on the application domain :
– domain analysis needs to mature,
– anticipating change needs experience,
– it’s often not feasible to use a large number of application
examples to test the framework’s exploitability.
Using Patterns to Document Frameworks
Patterns are used in a Framework :
– to document design decisions,
– to aid the communication of design,
– to reuse proven solutions.
Application Instantiation
F
r
a
m
e
w
o
r
k
Application 1
Application 2
Application 3
Applications need to subclass or use Framework classes to suit
their needs.
Polymorphism is heavily used.
Frequently used patterns that allow framework based application
customisation include :
– Template, Factory, State, Strategy, Command, Decorator, Adapter,
Visitor, Mediator, Proxy, Iterator.
AWT Architecture
(1) Design for containment,
(2) Design for layout management,
(3) Design for platform independence,
(4) Design for embedding Applets in html.
(1) Design for Containment
Arbitrary deep containment hierarchy.
Component
paint()
deliverEvent()
getParent()
Container
add()
remove()
getComponent()
(2) Design for Layout Management
Positioning components by specifying absolute
coordinates is problematic.
We need to empower Containers with algorithms that
layout their components relatively.
Container
Naive Solution :
borderLayout()
flowLayout()
gridLayout()
Design for Layout Management
Another Naive solution :
Component
Container
layout()
BorderContainer
layout()
GridContainer
FlowContainer
layout()
layout()
Design for Layout Management
Java’s Solution (Strategy Design Pattern) :
Component
LayoutManager
addLayoutComponent()
removeLayoutComponent()
layoutContainer()
minimumLayoutSize()
preferredLayoutSize()
Container
layout()
- When we need
absolute Coordinates
we set the layout to
null.
BorderLayout
GridLayout
FlowLayout
Does LayoutManager reference its Container?
(3) Design for Platform Independence
How do we create a multiplatform GUI API?
– Using native implementations for each platform (AWT).
– Write all the GUI functionality in Java (SWING).
Peer interfaces decouple AWT components from a
specific platform.
GUI Components
Peer Interfaces
95/98/NT Motif
Peers
Peers
Mac
Peers
Designing the Component Peers
Component
Native Implementations
stay completely hidden.
ButtonPeer
Button
setLabel()
fLabel
getLabel()
setLabel()
MacButtonPeer
Win32ButtonPeer
MotifButtonPeer
Creating the Component Peers
The instantiation of a Peer initiates when a Component
is added to a Container.
Which Component Peer should a Component
instantiate?
Object creation is hidden from the Clients and
delegated to the Abstract Factory Class named Toolkit.
Creating the Component Peers
ButtonPeer
Toolkit
createButton()
createWindow()
createCanvas()
…..
getDefaultToolkit()
setLabel()
MotifButtonPeer
MotifToolkit
Win32Toolkit
Win32ButtonPeer
Are the Toolkits Hardcoded?
No, Object Creation by Name is preferred.
String myToolkitName =
System.getProperty(“awt.toolkit”,”sun.awt.motif.Mtoolkit”);
toolkit = (Toolkit) Class.forName(myToolkitName).newInstance()
Complete elimination of dependencies on concrete
Toolkit classes.
(4) Design for embedding Applets in html :
Applet’s init(), start(), stop(), and destroy(), methods
work as hooks to be called polymorphically by the
browser.
Can the Applet be considered to be an example of an
application framework class ?
HTML
Browser
Applet
MyApplet
init()
start();
stop()
destroy()
java.io
The input/output streams and readers/writers allow
reading and writing from arbitrary streams of data.
The Decorator pattern is used to achieve a Unix-like
pipes and filters functionality.
1
InputStream
read()
FilterInputStream
FileInputStream
read()
read()
BufferedInputStream
read()
PushBacknputStream
read()
StringBufferedInputStream
read()
LineNumberInputStream
getLinenumber()
Decorating Streams
FileInputStream is = new FileInputStream(“myfile.txt”)
BufferedInputStream bis = new BufferedInputStream(is);
LineNumberInputStream lnis =
new LineNumberInputStream (bis);
while((int ch=lnis.read())!=-1) {
...
line_number=lnis.getLineNumber();
...
InputStream
}
read()
lnis
read()
bis
is
read()
read()
File
Decorating Streams
FileOutpuStream fos = new FileOutpuStream(“myfile.txt”)
BufferedOutputStream bos = new BufferedOutputStream(fos);
EncryptedOutputStream eos = new EncryptedOutputStream(bos);
GZIPOutputStream gzipos = new GZIPOutputStream(eos);
InputStream
read()
gzipos
read()
eos
read()
bos
read()
fos
read()
File
3. java.util
Class Enumeration provides a way to access the elements of
structure sequentially without exposing its underlying
representation or its traversal strategy (iterator). It provides two
methods :
– hasMoreElements()
– nextElement()
Class Observable and interface Observer provide partial
implementation for the Observer design pattern :
Observable
addObserver(Observer o)
deleteObserver(Observer o)
hasChanged()
notifyObservers()
setChanged()
Observer
update(Observable o,Object o)
Summary
design architecture highlights in :
– 1.java.awt :
• Composite : Container,
• Strategy : Layout,
• Abstract Factory : Toolkit,
• Template: Applet.
– 2.java.io :
• Decorator : I/O Streams, Readers/Writers,
– 3.java.util :
• Iterator : Enumeration, Observer, Observable.
Quiz
Java uses two public fields in its FileSystem class to
differentiate between a file and a directory:
public static final int BA_REGULAR
= 0x02;
public static final int BA_DIRECTORY = 0x04;
Can you think of a better way to encapsulate this information
using Design Patterns? What would you get in return?
When we want absolute coordinates we have to set the layout
of a Container to null. Can you imagine what this implies for the
implementation of Container? Is there a better way?
Exercises
Use the Decorator pattern in order to design and
implement a stream encoder. The encoder should work
with DataInputStreams. The encoding that should be performed
is Rot-13, that is, characters that are read from the stream
should be rotated by 13 places (“A” becomes “N”, “W” becomes
“J” etc). Punctuation and numbers should remain unchanged.
Use the encoder in order to encode and then decode the file
“text.txt”. Hint: applying twice the encoding process, a Rot-13
encoded stream gets decoded.
Java, Advanced Level
day 2 Part C
Java Foundation Classes (JFC)
Athanasios Tsintsifas, LTR, University of Nottingham,
email : azt@cs.nott.ac.uk, http: www.cs.nott.ac.uk/~azt
Aims
Introduce Java Foundation Classes (JFC).
Mapping of AWT/JFC classes.
JFC classes :
–
–
–
–
–
JComponent widgets,
LayoutManager classes,
Model classes and Interfaces,
Manager classes,
Miscellaneous classes.
JFC’ s complex components :
– JTabbedPane, Jtable, JTree, JInternalWindow.
Introducing JFC
Java Foundation classes consists of
–
–
–
–
–
AWT and its components,
Swing components,
Accessibility API,
Java2D,
Drag&Drop.
Swing Components are built on top of AWT.
Swing belongs to 1.2 but you can get it as an external
API with 1.1 (without 2D though).
Mapping of awt Classes to Packages
java.awt.applet
: Applet & relevant Interfaces.
java.awt
: Components, Layouts, Graphics.
java.awt.color
: ColorSpaces.
java.awt.datatransfer : Clipboard, DataFlavor, Transferable.
java.awt.dnd
: DragSource, DropTarget, Events, Listeners.
java.awt.event
: Events, Listeners, Adapters.
java.awt.font
: Font graphics rendering classes.
java.awt.geom
: Shapes, AffineTransform, GeneralPath.
java.awt.im
: InputContext, InputMethodRequests.
java.awt.image
: Image Processing classes.
java.awt.print
: PrinterJob, Printable, PageFormat.
Mapping of swing Classes to Packages
javax.swing
javax.swing.border
javax.swing.colorchooser
javax.swing.event
javax.swing.filechooser
javax.swing.plaf
javax.swing.table
javax.swing.text
javax.swing.text.html
javax.swing.text.rtf
javax.swing.tree
javax.swing.undo
: Components, Managers, Models.
: 8 Types of Borders.
: Customisation classes.
: Listeners, Events, Adapters.
: Customisation classes.
: UI Abstractions for Plug&Play GUI.
: Customisation classes.
: Text Editor Classes.
: HTML Editor classes.
: RTF Editor kit.
: Customisation classes.
: Classes for undo functionality.
The Swing Components
Provide pluggable look-and-feel ability.
Lightweight implementation (no Peers).
JavaBeans compliance.
javax.swing.*
–
–
–
–
–
(1) JComponent widgets,
(2) LayoutManager classes,
(3) Model classes and Interfaces,
(4) Manager classes,
(5) Miscellaneous classes.
JComponent
The Parent of all Swing Components :
Component
Container
JComponent
Overrides and extends Container.
Gives the ability to define :
– Borders,
– ToolTipText,
– UI look&feel.
to every Swing Component.
(1) JComponent Widgets
JButton, JLabel,
JPanel, Box,
JMenu, JMenuItem, JSeparator,JCheckMenuItem,
JRadioButtonMenuItem, JMenuBar, JPopupMenu,
JToggleButton, JRadioButton, JCheckBox,
JFileChooser,
JComboBox, JList,
JInternalFrame, JDesktopPane, JDesktopIcon,
JOptionPane.
(1) JComponent Widgets
JProgressBar, JSlider, JScrollBar,
JScrollPane, JViewPort,
JSpiltPane, JTabbedPane,
JTable, JTree,
JTextField, JTextArea, JPasswordField, JEditorPane,
JToolBar, JToolTip,
JApplet,
JWindow, JDialog, JFrame.
A Simple Example Using JFC
import javax.swing.*;import java.awt.*;
import javax.swing.border.*;import java.awt.event.*;
public class ExampleUsingJFC extends JFrame {
public static void main(String[] argv) {
ExampleUsingJFC myExample = new ExampleUsingJFC("ExampleUsingJFC");
}
public ExampleUsingJFC(String title) {
super(title);
setSize(150,150);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();System.exit(0);
}});
without border
init(); setVisible(true);
}
A Simple Example Using JFC
private void init() {
JPanel my_panel = new JPanel();
my_panel.setLayout(new GridLayout(3,3));
for (int i=1;i<10;i++) {
ImageIcon icon = new ImageIcon(i+".gif");
JButton jb = new JButton(icon);
jb.setToolTipText(i+".gif");
my_panel.add(jb);
}
getContentPane().add(my_panel);
my_panel.setBorder(BorderFactory.createEtchedBorder());
}
Borders Available :
}
- Bevel, Compound, Empty, Etched,
- Line, Matte, Soft, Titled.
with Etched border
(2) Layout Managers
BoxLayout :
– Layouts horizontally or vertically.
OverlayLayout :
– Arranges components over the top of each other.
ScrollPanelLayout :
– JScrollPane’s layout.
ViewportLayout :
– JViewport’s layout.
SizeRequirements :
– Helper class for layout managers.
(3) Model Classes
MVC architectural pattern
Notification
Broadcast
Dates back to late 70’s.
Well known OO design solution.
The parts can be exchanged at
runtime. Can change the Look&Feel
very easily.
More than one view for a model
always in synchronisation.
Notification through events.
(+) Clean separation of concerns,
better maintainability and reusability.
(-) Many more classes involved.
In java View and Controller are
grouped to a UIdelegate.
Swing Model Classes
BoundedRangeModel
: Four int values -> value, extent, min, max.
ButtonModel
: Boolean armed.
ListModel
: A collection of objects.
ComboBoxModel
: A collection of objects and a selected object.
MutableComboBoxModel : A Vector of objects and a selected object.
ListSelectionModel
: Indices of selected list or table items.
SingleSelectionModel
: The index of the selection in a collection.
ColorSelectionModel
: A Colour.
TableModel
: A two dimensional array of objects.
TableColumnModel
: Many attributes.
TreeModel
: Objects that can be displayed in a tree.
TreeSelectionModel
: Selected rows.
Document
: Content in text or styled text and images.
Changing Look&Feel
public void changeLookTo(String cName) {
try { UIManager.setLookAndFeel(cName); }
catch (Exception e) { System.out.println("Could not change l&f"); }
SwingUtilities.updateComponentTreeUI(this);
this.pack();
}
ChooseFrom:
- "javax.swing.plaf.metal.MetalLookAndFeel";
- "javax.swing.plaf.motif.MotifLookAndFeel";
- “javax.swing.plaf.windows.WindowsLookAndFeel";
(4) Manager Classes
Managers accept customisations.
Managers include :
–
–
–
–
–
–
DesktopManager, DefaultDesktopManager,
FocusManager, DefaultFocusManager,
MenuSelectionManager,
RepaintManager,
ToolTipManager,
UIManager.
(5) Miscellaneous
BorderFactory,
ImageIcon, Icon,
LookAndFeel,
ProgressMonitor, ProgressMonitorInputStream,
SwingUtilities,
GrayFilter,
Timer.
The JTabbedPane Component
private void init() {
JTabbedPane jtb = new JTabbedPane();
for (int i=1;i<10;i++) {
ImageIcon icon = new ImageIcon(i+".gif");
ImageIcon icon2 = new ImageIcon(i+"a.jpg");
JScrollPane jsp = new JScrollPane(new JLabel(icon2));
jtb.addTab(i+"-tab",icon,jsp);
}
getContentPane().add(jtb);
jtb.setBorder(BorderFactory.createEtchedBorder());
}
The JTable Component
String data[][] = {
{"John","Sutherland","Student"},
{"George","Davies","Student"},
{"Melissa","Anderson","Associate"},
{"Stergios","Maglaras","Developer"},
};
String fields[] = {"Name","Surname","Status"};
private void init() {
JTable jt = new JTable(data,fields);
JScrollPane pane = new JScrollPane(jt);
getContentPane().add(pane);
}
The JTree Component
Many classes involved :
–
–
–
–
–
–
–
JTree,
TreeModel, DefaultTreeModel,
TreeNode, MutableTreeNode, DefaultMutableTreeNode,
TreeSelectionModel, TreeSelectionListener, TreeSelEvent,
TreePath,
TreeCellRenderer,
TreeNodeListener, TreeNodeEvent.
JTree Example
private void init() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Calendar");
DefaultMutableTreeNode months = new DefaultMutableTreeNode("Months");
root.add(months);
String monthLabels[] = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};
for (int i=0; i<monthLabels.length; i++)
months.add( new DefaultMutableTreeNode(monthLabels[i]));
DefaultMutableTreeNode weeks = new DefaultMutableTreeNode("Weeks");
root.add(weeks);
String weekLabels[] = {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
for (int i=0; i<weekLabels.length; i++)
weeks.add( new DefaultMutableTreeNode(weekLabels[i]));
JScrollPane js = new JScrollPane(new JTree(root));
getContentPane().add(js);
}
Internal Windows
JInternalFrames are placed on a JLayeredPane.
JLayeredPane provides Z ordering, but only for
lightweight components (order 0 to N-1).
JInternalFrames can be :
–
–
–
–
closed,
maximized,
iconified,
resized.
Using Internal Windows
private void init() {
JLayeredPane layers = new JDesktopPane();
setLayeredPane(layers);
for (int i=0; i<9; i++) {
ImageIcon icon = new ImageIcon((i+1)+"a.jpg");
JScrollPane jsp = new JScrollPane(new JLabel(icon));
jsp.setPreferredSize(new Dimension(120,140));
//JInternalFrame(title, resizable, closable,maximizable, iconifiable)
JInternalFrame jif = new JInternalFrame(i+" frame",true,true,true,true);
jif.setLocation((i%4)*140,(i/4)*180);
jif.getContentPane().add(jsp);
jif.pack();
layers.add(jif);
}
}
Summary
Introduced Java Foundation Classes (JFC).
Mapped AWT/JFC classes to packages.
Reviewed :
–
–
–
–
–
JComponent widgets,
LayoutManager classes,
Model classes and Interfaces,
Manager classes,
Miscellaneous classes.
Reviewed JFC’ s complex components :
– JTabbedPane, Jtable, JTree, JInternalWindow.
Quiz
How would you proceed to create Oval borders? Oval buttons?
Oval Windows?
Can you place AWT components inside JInternalWindows? And
if yes how you would expect them to behave and why?
Exercises
Get any simple example you have written that uses
AWT and change it so that it uses the respective SWING
components. Add borders and ToolTips where appropriate.
Change the UI style and notice the differences.
Adapt the drawing example so that it uses Internal Frames that
contain a JScrollPane. Design and implement so that you can
have multiple internal frames viewing the drawing model.
Java, Advanced Level
day 2 Part D
Exploring the 2Dapi
Athanasios Tsintsifas, LTR, University of Nottingham,
email : azt@cs.nott.ac.uk, http: www.cs.nott.ac.uk/~azt
Aims
Introduce Java’s 2Dapi and explain :
– Graphic operations :
• Drawing/Filling Shapes, Paints, Strokes, Textures, General Paths,
Compositing, Transforming.
– Text Processing :
• Font Support, rendering strings as shapes.
– Image Handling :
• BufferedImage for DoubleBuffering, Imageprocessing.
– Graphics Device Hooks :
• Graphics Configuration.
– Colour Management :
• Colour Representation and Conversion.
Introducing the 2Dapi
Huge improvement over the previous Graphics abilities.
Part of the Java media API, which contains :
– Java3D, Advanced Imaging, Media Framework,
– Sound, Speech, Telephony.
2Dapi is internal to jdk1.2 and contains classes for:
–
–
–
–
–
(1) Graphics Processing,
(2) Text Processing,
(3) Image Handling,
(4) Graphics Device Hooks,
(5) Colour Management.
(1) Graphics Processing
New Subclass of Graphics -> Graphics2D.
Drawing in Java involves 3 steps :
– Specify the drawing attributes :
• setColor(), setFont(), setPaintMode(),
setXORMode().
– Identify the Shape sh to draw :
• Line2D, Rectangle2D, RoundRectangle2D,
Ellipse2D, QuadCurve2D, CubicCurve2D, Arc2D,
GeneralPath.
– Draw it :
• draw(sh) -> uses the current Stroke,
• fill(sh) -> uses the current Paint.
Transformations & Compositing available.
Drawing Example Using the 2Dapi
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Example2D extends Frame {
public static void main(String[] argv) {
Example2D myExample = new Example2D();
}
public Example2D() {
setSize(300,180);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();System.exit(0);
}});
setVisible(true);
}
Drawing Example Using the 2Dapi
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.red);
Rectangle2D rect1 = new Rectangle2D.Double(32,42,100,100);
Rectangle2D rect2 = new Rectangle2D.Double(164,42,100,100);
g2d.fill(rect1);
g2d.draw(rect2);
}
}
Adding Paints and Strokes
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.red);
Rectangle2D rect1 = new Rectangle2D.Double(32,42,100,100);
Rectangle2D rect2 = new Rectangle2D.Double(164,42,100,100);
Paint gp = g2d.getPaint();
g2d.setPaint(new GradientPaint(32,42,Color.red,100,100,Color.white,true));
g2d.fill(rect1);
g2d.setPaint(gp);
g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND, 3f, new float[] {10f}, 0f));
g2d.draw(rect2);
}
* GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic)
* BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase)
Adding Textures
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
Rectangle2D rect1 = new Rectangle2D.Double(32,42,100,100);
Rectangle2D rect2 = new Rectangle2D.Double(164,42,100,100);
g2d.setPaint(loadTextureResource("trouble.gif"));
g2d.fill(rect1);
g2d.setStroke(new BasicStroke(10f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_MITER, 2f, new float[] {12f}, 0f));
g2d.draw(rect2);
}
Adding Textures (missing method)
public TexturePaint loadTextureResource(String absfilename) {
MediaTracker tracker = new MediaTracker(this);
Image imtexture = Toolkit.getDefaultToolkit().getImage(absfilename);
tracker.addImage(imtexture,0);
try { tracker.waitForID(0);
int width = imtexture.getWidth(this);
int height = imtexture.getHeight(this);
System.out.println("width" + width + " height =" + height);
BufferedImage buffImg = new
BufferedImage(width,height, BufferedImage.TYPE_INT_ARGB);
Graphics g = buffImg.getGraphics();
g.drawImage(imtexture,0,0,this);
return new TexturePaint(buffImg,new Rectangle2D.Double(0,0,width,height));
}
catch (Exception e) {
System.out.println("Exception on Image-Texture Loading");
}
return null;
}
General Paths
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
GeneralPath gp1 = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
GeneralPath gp2 = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
gp1.moveTo(20,30); gp1.lineTo(150,30);gp1.lineTo(150,130);gp1.closePath();
gp2.moveTo(180,30); gp2.lineTo(290,30);gp2.quadTo(200,75,180,130);
gp2.curveTo(110,50,220,100,180,30);
g2d.setPaint(loadTextureResource("Cork.jpg"));
g2d.fill(gp1);
g2d.fill(gp2);
}
Compositing
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
Rectangle2D rect1 = new Rectangle2D.Double(32,42,200,130);
Rectangle2D rect2 = new Rectangle2D.Double(164,42,100,100);
g2d.setColor(Color.cyan);
g2d.fill(rect1);
g2d.setPaint(loadTextureResource("Cork.jpg"));
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
g2d.fill(rect2);
}
Transformations
Class AffineTransform can be used for :
– translating, rotating, scaling, flipping, shearing
Just know the static methods of the class :
getRotateInstance(double theta)
getScaleInstance(double scaleX, double scaleY)
getShearInstance(double shiftX, double shiftY)
getTranslateInstance(double x, double y)
They return an Instance of AffineTransform
encapsulating the Geometry of the transformation.
Transformations
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
Rectangle2D rect1 = new Rectangle2D.Double(132,42,100,100);
AffineTransform at = AffineTransform.<XXX>
g2d.setTransform(at);
g2d.setColor(Color.red);
g2d.fill(rect1);
Use compose() to mix Transformations
}
getScaleInstance(1,1);
getRotateInstance((10*Math.PI)/180);
getScaleInstance(.7,.7);
getShearInstance(.5,0);
getTranslateInstance(150,0);
(2) Text Processing
Extended Font support :
import java.awt.*;
public class FontLister {
public static void main(String[] argv) {
Font fonts[] =
GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (int i =0; i<fonts.length; i++) {
System.out.println(i + " font=" + fonts[i]);
}
Font myFont = fonts[2].deriveFont(Font.BOLD, 32);
}
}
(2) Text Processing
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
FontRenderContext frc = g2d.getFontRenderContext();
Font font = new Font("Helvetica",Font.ITALIC | Font.BOLD, 72);
TextLayout tl = new TextLayout("Excellent!",font,frc);
Shape myShape =
tl.getOutline(AffineTransform.getTranslateInstance(50,100));
Paint myPaint = loadTextureResource("Cork.jpg");
g2d.setPaint(myPaint);
g2d.fill(myShape);
}
(3) Image Handling
BufferedImage :
– For animation (double buffering),
– For optimising internal representation.
Using BufferedImage :
– instantiate :
• new BufferedImage(width,height,type))
– use :
• getGraphics() to obtain a Graphics,
• or createGraphics() to obtain a Graphics2D.
– You could also ask DeviceConfguration for the optimal
type using createCombatibleImage().
Image Processing
BufferedImageFilter and BufferedImageOp :
– AffineTransformOp : for all Affine Transformations.
– ColorConvertOp
: for Colour mappings.
– ConvoleOp
: for weighted convolutions.
– LookupOp
: for non uniform modifications.
– RescaleOp
: for special scaling.
JPEG encoding and decoding :
– com.sun.image.codec.jpeg,
– ask the JPEGCodec for a decoder or encoder,
– read or write on the streams.
(4) Graphics Device Hookups
Class GraphicsDevice contains
GraphicsConfiguration that
encapsulates the configuration of the Output
Device.
You use GraphicsConfiguration to :
–
–
–
–
get the current ColorModel,
get the available Fonts,
register a new Font,
optimise drawing operations.
(5) Colour Management
Classes Color and ColorSpace.
A map of device dependent colour spaces to device
independent colour spaces is represented by class
ICC_Profile :
– ICC_ProfileGray,
– ICC_ProfileRGB.
Colour Conversions between spaces
– RGB, GRAY, LINEAR_RGB, PYCC, CIEXYZ
– CIEXYZ is preferred (check www.color.org)
Summary
Reviewed java’s 2Dapi :
– Graphic operations :
• Drawing/Filling Shapes, Paints, Strokes, Textures, General Paths,
Compositing, Transforming.
– Text Processing :
• Font Support, rendering strings as shapes.
– Image Handling :
• BufferedImage for DoubleBuffering, Imageprocessing.
– Graphics Device Hooks :
• Graphics Configuration.
– Colour Management :
• Colour Representation and Conversion.
Quiz
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
GeneralPath gp1 = new GeneralPath();
gp1.moveTo(100,200); gp1.curveTo(110,60,190,60,200,200);
gp1.lineTo(180,200);
gp1.curveTo(180,150,160,150,160,200);
gp1.lineTo(140,200);
gp1.curveTo(140,150,120,150,120,200);
gp1.closePath();
g2d.setColor(Color.blue);
What will be drawn?
g2d.fill(gp1);
g2d.setColor(Color.white);
Shape el1 = new Ellipse2D.Double(125,120,30,30);
Shape el2 = new Ellipse2D.Double(145,120,30,30);
g2d.fill(el1); g2d.fill(el2);
g2d.setColor(Color.black);
el1 = new Ellipse2D.Double(135,130,15,15);
el2 = new Ellipse2D.Double(155,130,15,15);
g2d.fill(el1);g2d.fill(el2);
}
Exercises
Use the AffineTransform to animate a ball falling
from the top of a panel to the bottom.
Provide randomly different transformations including scaling,
shearing and rotating.
Use BufferedImage for the drawing to avoid flickering and
Graphics’s drawImage() to instantly commit all the changes to
the screen.
Java, Advanced Level
day 2 Part E
Continuing from here
Athanasios Tsintsifas, LTR, University of Nottingham,
email : azt@cs.nott.ac.uk, http: www.cs.nott.ac.uk/~azt
Very Important
JavaSoft (SUN)
– www.javasoft.com -> register to jdc.
JavaWorld
– www.javaworld.com -> read java articles.
Latest JDK is the 1.3 beta, containing :
– Java Sound, JNDI, RMI/IIOP, JPDA,
– Enhancements to many packages & tools,
– Improved hotspot and graphic device hooks.
How to Improve
Study design patterns very carefully :
– It usually takes a year or two to design comfortably using
design patterns.
Make sure you understand the infrastructure thoroughly :
– Language, JVM, Class format, APIs.
Read Java’s source code and try to understand the reasoning
behind a design decision.
Spend time reflecting on a design, identify parts that worked well
and felt nice. Read framework documentation for inspiration.
Follow famous international OO conferences such as OOPSLA,
OT, ECOOP, TOOLS and try always to get your hands at the
proceedings.
Free Resources
Free Java Tools :
– www.apl.jhu.edu/~hall/java/
GNU Java Tools + Freebuilder :
– www.gnu.org/software/java/java.html
Thinking in Java :
– www.eckelobjects.com/
Caltech’s Links :
– www.infospheres.caltech.edu/resources/java.html
References on Infrastructure
BOOKS :
The Java Language Specification, 1996, James Gosling, Bill Joy, and Guy
Steele, ISBN: 0-201-63451-1, Addison-Wesley: The Java Series.
The Java Virtual Machine Specification, 1997, Tim Lindholm and Frank Yellin,
ISBN: 0-201-63452-X, Addison-Wesley: The Java Series.
Inside the Java2 VM, 1999, Bill Venners, ISBN: 0-07-135093-4, McGraw Hill.
Java2 Performance and idioms guide, 1999, Craig Larman and Rhett Guthrie,
ISBN: 0-13-014260-3, Prentice Hall PTR.
LINKS :
http://www.artima.com
RESOURCES :
Java Spec: www.patterndepot.com/put/8/JavaPatterns.htm
JVM Spec: java.sun.com/docs/books/vmspec/html/VMSpecTOC.doc.html
References on Design Patterns
BOOKS :
Design Patterns, E. Gamma, J. Vlissides, R. Helm, R. Johnson, 1995, ISBN
0-201-63361-2, Addison Wesley.
Patterns Languages of Program Design 1,2,3, 1995-1998, Addison Wesley.
Patterns in Java I,II,III, Mark Grand, 1998-2000, Addison Wesley.
LINKS :
http://hillside.net/patterns/
http://www.cetus-links.org/oo_patterns.html
http://st-www.cs.uiuc.edu/cgi-bin/wikic/wikic
RESOURCES :
Free Book : www.patterndepot.com/put/8/JavaPatterns.htm
References on Frameworks
BOOKS :
Building Application Frameworks : Object Oriented Foundations of
Framework Design, M. Fayad, D. Schmidt, R. Johnson, 1999,
ISBN : 0-471-24875-4, Wiley.
Object Oriented Application Frameworks, T. Lewis et al, 1995,
ISBN, 1-884777-06-6 Manning publications Co.,
LINKS :
www.ibm.com/java/education/oobuilding/index.html
st-www.cs.uiuc.edu/users/johnson/frameworks.html
www.cetus-links.org/oo_frameworks.html
RESOURCES :
Taligent Books :
http://hpsalo.cern.ch/TaligentDocs/TaligentOnline/DocumentRoot/1.0/Do
cs/books/index.html
References on Advanced OO
BOOKS :
The Art of the Metaobject Protocol , G. Kiczales, J. Rivieres, D. Bobrow,
1991, ISBN 0-262-61074-4, MIT Press.
Refactoring : Improving the Design of Existing Code, M. Fowler, 1999,
ISBN : 0-201-48567-2, Addison-Wesley, Object Technology Series.
LINKS :
www.xprogramming.com/books/xp_recommended_reading_prog.htm
www.parc.xerox.com/csl/groups/sda/publications.shtml
RESOURCES :
AspectJ : http://www.aspectj.org
HyperJ : http://www.alphaworks.ibm.com/tech/hyperj
Contact Info
If you have :
–
–
–
–
any java/oo/patterns queries,
thought of an interesting design,
stuck in a very hard design decision,
or just want to say hello…
Don’t hesitate… email me at :
– azt@cs.nott.ac.uk
– check my future web-site at www.thanassis.com for updates,
resources and news on
• java, OO, patterns, CS, diagramming.
Java Advanced Level day2