35
Object Oriented Programming and Design in Java Session 16 Instructor: Bert Huang

Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Object Oriented Programming and Design

in Java

Session 16Instructor: Bert Huang

Page 2: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Announcements

• Homework 3 out. Due Monday, Apr. 5th

• Office hour change

Sun Mon Tue Wed Thu Fri

John 1-3 Class 11-12:15

Class 11-12:15Bert 2-4

Yipeng 4-6

Lauren 11-1

Page 3: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Review• Frameworks

• The Applet Framework

• Extend Applet, override init, start, stop, destroy, and paint

• The Collections Framework

• Collections interface implements Iterable

• Subinterfaces List and Set

• AbstractCollection (AbstractList, AbstractSet)

Page 4: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Todayʼs Plan

• (resolve Font drawing confusion)

• Horstmannʼs graph editor framework

• Prototype pattern

• Example usage of the framework

Page 5: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

import java.applet.*;import java.awt.*;import java.awt.event.*;import java.awt.font.*;import java.awt.geom.*;import javax.swing.*;

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, new ActionListener() { public void actionPerformed(ActionEvent 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;}

Page 6: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

import java.applet.*;import java.awt.*;import java.awt.event.*;import java.awt.font.*;import java.awt.geom.*;import javax.swing.*;

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, new ActionListener() { public void actionPerformed(ActionEvent 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;}

Page 7: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

getStringBounds vs drawString

• java.awt.Font public Rectangle2D getStringBounds(String str, FontRenderContext frc)Returns the logical bounds of the specified String in the specified FontRenderContext. The logical bounds contains the origin, ascent, advance, and height, which includes the leading. The logical bounds does not always enclose all the text. For example, in some languages and in some fonts, accent marks can be positioned above the ascent or below the descent...

• java.awt.Graphics:public abstract void drawString(String str, int x, int y)Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.

Page 8: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

getStringBounds vs drawString

• java.awt.Font public Rectangle2D getStringBounds(String str, FontRenderContext frc)Returns the bounding box of text if you call drawString(0, 0) using frc

• java.awt.Graphics:public abstract void drawString(String str, int x, int y)Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.

-bounds.getY() Hello, World!bounds.getY() Hello, World!

Page 9: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Graphs• Collections of nodes connected by edges

• edges may be directed or undirected

• Useful for modeling networks, relationships, states and transitions

• Nodes can represent people, computers, routers, electrical components, etc

• Edges can represent friendship, network connectivity, circuits, etc

• Weʼll look at an application framework for building graph editing programs

Page 10: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Graph Editor Framework

Page 11: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Responsibilities of the Framework vs. Application

• Each application should be responsible for node/edge drawing and hit testing

• Since nodes and edges can represent varying things

• The application should also provide the set of allowed node and edge types

Page 12: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Prototype Pattern• A system needs to create several kinds of objects whose classes

are not known when the system is built

• You donʼt want to require a separate class for each kind of object

• You want to avoid a separate hierarchy of classes whose responsibility it is to create the objects

• Define a prototype interface common to all created objects

• Supply a prototype object for each kind of object that the system creates

• Clone the prototype object whenever a new object of the given kind is required

Con

text

Solu

tion

Page 13: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Prototype Pattern

Page 14: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Framework Javadoc

• The framework Horstmann writes is quite extensive already, despite missing some desired features

• Weʼll examine the classes necessary to write an instance application

• Node, Edge, Graph

Page 15: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

interface Node extends Serializable, Cloneable

Page 16: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

getConnectionPoints

Page 17: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

interface Edge extends Serializable, Cloneable

Page 18: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

abstract class graph implements Serializable

Page 19: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change
Page 20: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Simple Graph Editor

Page 21: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

SimpleGraphEditorimport javax.swing.*;

/** A program for editing UML diagrams.*/public class SimpleGraphEditor{ public static void main(String[] args) { JFrame frame = new GraphFrame(new SimpleGraph()); frame.setVisible(true); }}

Page 22: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

SimpleGraphimport java.awt.*;import java.util.*;/** A simple graph with round nodes and straight edges.*/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; }}

Page 23: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

LineEdge/** An edge that is shaped like a straight line.*/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; }}

Page 24: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

import java.awt.*;import java.awt.geom.*;

/** A circular node that is filled with a color.*/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;}

SimpleGraph

Page 25: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

import java.awt.*;import java.awt.geom.*;

/** A circular node that is filled with a color.*/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;}

Page 26: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

import java.awt.*;import java.awt.geom.*;

/** A circular node that is filled with a color.*/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;}

Page 27: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

import java.awt.*;import java.awt.geom.*;

/** A circular node that is filled with a color.*/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;}

Page 28: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Framework Generics

• The framework code does not need to know the concrete node, edge or graph types

• Important operations can use the template-method pattern and let the concrete classes handle the specifics

Page 29: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Adding a Node

Page 30: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change
Page 31: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Mouse Pressed

Page 32: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

mousemotionlisten

er

MouseEvent

GraphPanel Graph Node

Mouse Dragged

Page 33: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

mouselistener Mouse

Event ToolBar Graph

Mouse Released

Page 34: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change
Page 35: Object Oriented Programming and Design in Javabert/courses/1007/slides/Lecture16.pdf · 2010-03-30 · Announcements •Homework 3 out.Due Monday, Apr. 5th •Office hour change

Reading

• Horstmann Ch. 8.4