Contents [0/63] |
Object-Oriented Design & Patterns [1/63] |
Chapter Topics [2/63] |
Frameworks [3/63] |
Application Frameworks [4/63] |
Applets [5/63] |
Applets [6/63] |
Applets [7/63] |
<applet code="BannerApplet.class" width="300" height="100">
<param name="message" value="Hello, World!"/>
<param name="fontname" value="Serif"/>
<param name="fontsize" value="64"/>
<param name="delay" value="10"/>
</applet>
Example Applet [8/63] |
file:horstmann/ch08_applet/BannerApplet.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package horstmann.ch08_applet; import java.applet.Applet; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; import javax.swing.Timer; @SuppressWarnings("serial") public class BannerApplet extends Applet { public void init() { message = getParameter("message"); String fontname = getParameter("fontname"); int fontsize = Integer.parseInt(getParameter("fontsize")); delay = Integer.parseInt(getParameter("delay")); font = new Font(fontname, Font.PLAIN, fontsize); Graphics2D g2 = (Graphics2D) getGraphics(); FontRenderContext context = g2.getFontRenderContext(); bounds = font.getStringBounds(message, context); timer = new Timer(delay, event -> { start--; if (start + bounds.getWidth() < 0) start = getWidth(); repaint(); }); } public void start() { timer.start(); } public void stop() { timer.stop(); } public void paint(Graphics g) { g.setFont(font); g.drawString(message, start, (int) -bounds.getY()); } private Timer timer; private int start; private int delay; private String message; private Font font; private Rectangle2D bounds; }
Example Applet [9/63] |
Applets as a Framework [10/63] |
Collections Framework [11/63] |
Collections Framework: Interface Types [12/63] |
Collections Framework: Classes [13/63] |
Collections Framework [14/63] |
Collection<E> Interface Type [15/63] |
boolean add(E obj)
boolean addAll(Collection c)
void clear()
boolean contains(E obj)
boolean containsAll(Collection c)
boolean equals(E obj)
int hashCode()
boolean isEmpty()
Iterator iterator()
boolean remove(E obj)
boolean removeAll(Collection c)
boolean retainAll(Collection c)
int size()
E[] toArray()
E[] toArray(E[] a)
Iterator<E> Interface Type [16/63] |
AbstractCollection Class [17/63] |
AbstractCollection Class [18/63] |
Adding a new Class to the Framework [19/63] |
file:horstmann/ch08_queue/BoundedQueue.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
package horstmann.ch08_queue; import java.util.AbstractCollection; import java.util.ArrayList; import java.util.Iterator; /** A first-in, first-out bounded collection of objects. */ public class BoundedQueue<E> extends AbstractCollection<E> { /** Constructs an empty queue. @param capacity the maximum capacity of the queue <p><b>Precondition:</b> capacity > 0</p> */ public BoundedQueue(int capacity) { elements = new ArrayList<E>(capacity); count = 0; head = 0; tail = 0; } public Iterator<E> iterator() { return new Iterator<E>() { public boolean hasNext() { return visited < count; } public E next() { int index = (head + visited) % elements.size(); E r = elements.get(index); visited++; return r; } public void remove() { throw new UnsupportedOperationException(); } private int visited = 0; }; } /** Remove object at head. @return the object that has been removed from the queue <p><b>Precondition:</b> size() > 0</p> */ public E remove() { E r = elements.get(head); head = (head + 1) % elements.size(); count--; return r; } /** Append an object at tail. @param anObject the object to be appended @return true since this operation modifies the queue. (This is a requirement of the collections framework.) <p><b>Precondition:</b> !isFull()</p> */ public boolean add(E anObject) { elements.set(tail,anObject); tail = (tail + 1) % elements.size(); count++; return true; } public int size() { return count; } /** Checks whether this queue is full. @return true if the queue is full */ public boolean isFull() { return count == elements.size(); } /** Gets object at head. @return the object that is at the head of the queue <p><b>Precondition:</b> size() > 0</p> */ public E peek() { return elements.get(head); } private ArrayList<E> elements; private int head; private int tail; private int count; }
file:horstmann/ch08_queue/QueueTester.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
package horstmann.ch08_queue; import java.util.ArrayList; import java.util.Collections; public class QueueTester { public static void main(String[] args) { BoundedQueue<String> q = new BoundedQueue<String>(10); q.add("Belgium"); q.add("Italy"); q.add("France"); q.remove(); q.add("Thailand"); ArrayList<String> a = new ArrayList<String>(); a.addAll(q); System.out.println("Result of bulk add: " + a); System.out.println("Minimum: " + Collections.min(q)); } }
Adding a new Class to the Framework [20/63] |
Sets [21/63] |
Lists [22/63] |
List Iterators [23/63] |
List Classes [24/63] |
List Classes [25/63] |
Optional Operations [26/63] |
Views [27/63] |
Views [28/63] |
Graph Editor Framework [29/63] |
Graph Editor Framework [30/63] |
User Interface [31/63] |
User Interface [32/63] |
Mouse Operations [33/63] |
Division of Responsibility [34/63] |
Adding Nodes and Edges [35/63] |
Adding Nodes and Edges [36/63] |
PROTOTYPE Pattern [37/63] |
PROTOTYPE Pattern [38/63] |
PROTOTYPE Pattern [39/63] |
Name in
Design Pattern |
Actual name
(graph editor) |
Prototype |
Node |
ConcretePrototype1 |
CircleNode |
Creator |
The GraphPanel that
handles the mouse operation for adding new nodes |
Framework Classes [40/63] |
Node Connection Points [41/63] |
Framework Classes [42/63] |
file:horstmann/ch08_graphed/Node.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package horstmann.ch08_graphed; import java.awt.Graphics2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.io.Serializable; /** A node in a graph. */ public interface Node extends Serializable, Cloneable { /** Draw the node. @param g2 the graphics context */ void draw(Graphics2D g2); /** Translates the node by a given amount. @param dx the amount to translate in the x-direction @param dy the amount to translate in the y-direction */ void translate(double dx, double dy); /** Tests whether the node contains a point. @param aPoint the point to test @return true if this node contains aPoint */ boolean contains(Point2D aPoint); /** Get the best connection point to connect this node with another node. This should be a point on the boundary of the shape of this node. @param aPoint an exterior point that is to be joined with this node @return the recommended connection point */ Point2D getConnectionPoint(Point2D aPoint); /** Get the bounding rectangle of the shape of this node @return the bounding rectangle */ Rectangle2D getBounds(); Object clone(); }
file:horstmann/ch08_graphed/Edge.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package horstmann.ch08_graphed; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.io.Serializable; /** An edge in a graph. */ public interface Edge extends Serializable, Cloneable { /** Draw the edge. @param g2 the graphics context */ void draw(Graphics2D g2); /** Tests whether the edge contains a point. @param aPoint the point to test @return true if this edge contains aPoint */ boolean contains(Point2D aPoint); /** Connects this edge to two nodes. @param aStart the starting node @param anEnd the ending node */ void connect(Node aStart, Node anEnd); /** Gets the starting node. @return the starting node */ Node getStart(); /** Gets the ending node. @return the ending node */ Node getEnd(); /** Gets the points at which this edge is connected to its nodes. @return a line joining the two connection points */ Line2D getConnectionPoints(); /** Gets the smallest rectangle that bounds this edge. The bounding rectangle contains all labels. @return the bounding rectangle */ Rectangle2D getBounds(Graphics2D g2); Object clone(); }
file:horstmann/ch08_graphed/AbstractEdge.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package horstmann.ch08_graphed; import java.awt.Graphics2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; /** A class that supplies convenience implementations for a number of methods in the Edge interface type. */ @SuppressWarnings("serial") public abstract class AbstractEdge implements Edge { public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException exception) { return null; } } public void connect(Node s, Node e) { start = s; end = e; } public Node getStart() { return start; } public Node getEnd() { return end; } public Rectangle2D getBounds(Graphics2D g2) { Line2D conn = getConnectionPoints(); Rectangle2D r = new Rectangle2D.Double(); r.setFrameFromDiagonal(conn.getX1(), conn.getY1(), conn.getX2(), conn.getY2()); return r; } public Line2D getConnectionPoints() { Rectangle2D startBounds = start.getBounds(); Rectangle2D endBounds = end.getBounds(); Point2D startCenter = new Point2D.Double( startBounds.getCenterX(), startBounds.getCenterY()); Point2D endCenter = new Point2D.Double( endBounds.getCenterX(), endBounds.getCenterY()); return new Line2D.Double( start.getConnectionPoint(endCenter), end.getConnectionPoint(startCenter)); } private Node start; private Node end; }
Framework Classes [43/63] |
file:horstmann/ch08_graphed/Graph.java [source] [doc-public] [doc-private]
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package horstmann.ch08_graphed; import java.awt.Graphics2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** A graph consisting of selectable nodes and edges. */ public abstract class Graph implements Serializable { private static final long serialVersionUID = 2008L; /** Constructs a graph with no nodes or edges. */ public Graph() { nodes = new ArrayList<Node>(); edges = new ArrayList<Edge>(); } /** Adds an edge to the graph that joins the nodes containing the given points. If the points aren't both inside nodes, then no edge is added. @param e the edge to add @param p1 a point in the starting node @param p2 a point in the ending node */ public boolean connect(Edge e, Point2D p1, Point2D p2) { Node n1 = findNode(p1); Node n2 = findNode(p2); if (n1 != null && n2 != null) { e.connect(n1, n2); edges.add(e); return true; } return false; } /** Adds a node to the graph so that the top left corner of the bounding rectangle is at the given point. @param n the node to add @param p the desired location */ public boolean add(Node n, Point2D p) { Rectangle2D bounds = n.getBounds(); n.translate(p.getX() - bounds.getX(), p.getY() - bounds.getY()); nodes.add(n); return true; } /** Finds a node containing the given point. @param p a point @return a node containing p or null if no nodes contain p */ public Node findNode(Point2D p) { for (int i = nodes.size() - 1; i >= 0; i--) { Node n = nodes.get(i); if (n.contains(p)) return n; } return null; } /** Finds an edge containing the given point. @param p a point @return an edge containing p or null if no edges contain p */ public Edge findEdge(Point2D p) { for (int i = edges.size() - 1; i >= 0; i--) { Edge e = edges.get(i); if (e.contains(p)) return e; } return null; } /** Draws the graph @param g2 the graphics context */ public void draw(Graphics2D g2) { for (Node n : nodes) n.draw(g2); for (Edge e : edges) e.draw(g2); } /** Removes a node and all edges that start or end with that node @param n the node to remove */ public void removeNode(Node n) { for (int i = edges.size() - 1; i >= 0; i--) { Edge e = edges.get(i); if (e.getStart() == n || e.getEnd() == n) edges.remove(e); } nodes.remove(n); } /** Removes an edge from the graph. @param e the edge to remove */ public void removeEdge(Edge e) { edges.remove(e); } /** Gets the smallest rectangle enclosing the graph @param g2 the graphics context @return the bounding rectangle */ public Rectangle2D getBounds(Graphics2D g2) { Rectangle2D r = null; for (Node n : nodes) { Rectangle2D b = n.getBounds(); if (r == null) r = b; else r.add(b); } for (Edge e : edges) r.add(e.getBounds(g2)); return r == null ? new Rectangle2D.Double() : r; } /** Gets the node types of a particular graph type. @return an array of node prototypes */ public abstract Node[] getNodePrototypes(); /** Gets the edge types of a particular graph type. @return an array of edge prototypes */ public abstract Edge[] getEdgePrototypes(); /** Gets the nodes of this graph. @return an unmodifiable list of the nodes */ public List<Node> getNodes() { return Collections.unmodifiableList(nodes); } /** Gets the edges of this graph. @return an unmodifiable list of the edges */ public List<Edge> getEdges() { return Collections.unmodifiableList(edges); } private ArrayList<Node> nodes; private ArrayList<Edge> edges; }
Framework UI Classes [44/63] |
A Framework Instance [45/63] |
Programmer responsibilities [46/63] |
A Framework Instance [47/63] |
A Framework Instance [48/63] |
file:horstmann/ch08_graphed/SimpleGraph.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package horstmann.ch08_graphed; import java.awt.Color; /** A simple graph with round nodes and straight edges. */ @SuppressWarnings("serial") public class SimpleGraph extends Graph { public Node[] getNodePrototypes() { Node[] nodeTypes = { new CircleNode(Color.BLACK), new CircleNode(Color.WHITE) }; return nodeTypes; } public Edge[] getEdgePrototypes() { Edge[] edgeTypes = { new LineEdge() }; return edgeTypes; } }
file:horstmann/ch08_graphed/SimpleGraphEditor.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
package horstmann.ch08_graphed; import javax.swing.JFrame; /** A program for editing UML diagrams. */ public class SimpleGraphEditor { public static void main(String[] args) { JFrame frame = new GraphFrame(new SimpleGraph()); frame.setVisible(true); } }
file:horstmann/ch08_graphed/CircleNode.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package horstmann.ch08_graphed; import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; /** A circular node that is filled with a color. */ @SuppressWarnings("serial") public class CircleNode implements Node { /** Construct a circle node with a given size and color. @param aColor the fill color */ public CircleNode(Color aColor) { size = DEFAULT_SIZE; x = 0; y = 0; color = aColor; } public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException exception) { return null; } } public void draw(Graphics2D g2) { Ellipse2D circle = new Ellipse2D.Double( x, y, size, size); Color oldColor = g2.getColor(); g2.setColor(color); g2.fill(circle); g2.setColor(oldColor); g2.draw(circle); } public void translate(double dx, double dy) { x += dx; y += dy; } public boolean contains(Point2D p) { Ellipse2D circle = new Ellipse2D.Double( x, y, size, size); return circle.contains(p); } public Rectangle2D getBounds() { return new Rectangle2D.Double( x, y, size, size); } public Point2D getConnectionPoint(Point2D other) { double centerX = x + size / 2; double centerY = y + size / 2; double dx = other.getX() - centerX; double dy = other.getY() - centerY; double distance = Math.sqrt(dx * dx + dy * dy); if (distance == 0) return other; else return new Point2D.Double( centerX + dx * (size / 2) / distance, centerY + dy * (size / 2) / distance); } private double x; private double y; private double size; private Color color; private static final int DEFAULT_SIZE = 20; }
file:horstmann/ch08_graphed/LineEdge.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
package horstmann.ch08_graphed; import java.awt.Graphics2D; import java.awt.geom.Point2D; /** An edge that is shaped like a straight line. */ @SuppressWarnings("serial") public class LineEdge extends AbstractEdge { public void draw(Graphics2D g2) { g2.draw(getConnectionPoints()); } public boolean contains(Point2D aPoint) { final double MAX_DIST = 2; return getConnectionPoints().ptSegDist(aPoint) < MAX_DIST; } }
Generic Framework Code [49/63] |
Add New Node [50/63] |
public void mousePressed(MouseEvent event)
{
Point2D mousePoint = event.getPoint();
Object tool = toolBar.getSelectedTool();
...
if (tool instanceof Node)
{
Node prototype = (Node) tool;
Node newNode = (Node)prototype.clone();
graph.add(newNode, mousePoint);
}
...
repaint();
}
Add New Node [51/63] |
Add New Edge [52/63] |
Add New Edge [53/63] |
Add New Edge [54/63] |
Enhancing the Framework [55/63] |
Enhancing the Framework [56/63] |
Enhancing the Framework [57/63] |
Using the Framework Enhancement [58/63] |
Another Framework Instance [59/63] |
Another Framework Instance [60/63] |
Edge Properties [61/63] |
Enhancing the Framework II [62/63] |
Enhancing the Framework II [63/63] |
Revised: 2007/09/11 16:38