Docsity

Protected Interface in Object-Oriented Design: An Example using Java - Prof. Paul C. Grabo, Assignments of Software Engineering

Toggle sidebar
bg1
1
ProtectedInterfaces
Reference:pp.240ffinObject‐OrientedDesign&Patterns
Revised11/3/2008
1. Drawtheassociatedclassdiagramforthefollowingcode
2. Explainhowthecodeillustratesaprotectedinterface
public interface SceneShape
{
/**
Draws this item.
@param g2 the graphics context
*/
void draw(Graphics2D g2);
/**
Draws the selection adornment of this item.
@param g2 the graphics context
*/
void drawSelection(Graphics2D g2);
/**
Sets the selection state of this item.
@param b true if this item is selected
*/
void setSelected(boolean b);
/**
Gets the selection state of this item.
@return true if this item is selected
*/
boolean isSelected();
/**
Translates this item by a given amount.
@param dx the amount to translate in x-direction
@param dy the amount to translate in y-direction
*/
void translate(int dx, int dy);
/**
Tests whether this item contains a given point.
@param p a point
@return true if this item contains p
*/
boolean contains(Point2D p);
}
public abstract class SelectableShape implements SceneShape
{
public void setSelected(boolean b)
{
selected = b;
}
public boolean isSelected()
{
return selected;
}
pf3
pf4

Partial preview of the text

Download Protected Interface in Object-Oriented Design: An Example using Java - Prof. Paul C. Grabo and more Assignments Software Engineering in PDF only on Docsity!

Protected Interfaces

Reference : pp. 240ff in Object‐Oriented Design & Patterns

Revised 11/3/

  1. Draw the associated class diagram for the following code
  2. Explain how the code illustrates a protected interface

public interface SceneShape { /** Draws this item. @param g2 the graphics context / void draw(Graphics2D g2); /* Draws the selection adornment of this item. @param g2 the graphics context / void drawSelection(Graphics2D g2); /* Sets the selection state of this item. @param b true if this item is selected / void setSelected( boolean b); /* Gets the selection state of this item. @return true if this item is selected / boolean isSelected(); /* Translates this item by a given amount. @param dx the amount to translate in x-direction @param dy the amount to translate in y-direction / void translate( int dx, int dy); /* Tests whether this item contains a given point. @param p a point @return true if this item contains p */ boolean contains(Point2D p); }

public abstract class SelectableShape implements SceneShape { public void setSelected( boolean b) { selected = b; }

public boolean isSelected() { return selected; }

public void drawSelection(Graphics2D g2) { translate(1, 1); draw(g2); translate(1, 1); draw(g2); translate(-2, -2); }

private boolean selected; }

public abstract class CompoundShape extends SelectableShape { public CompoundShape() { path = new GeneralPath(); }

protected void add(Shape s) { path.append(s, false ); }

public boolean contains(Point2D aPoint) { return path.contains(aPoint); }

public void translate( int dx, int dy) { path.transform( AffineTransform. getTranslateInstance (dx, dy)); }

public void draw(Graphics2D g2) { g2.draw(path); }

private GeneralPath path; }

public class HouseShape extends CompoundShape { /** Constructs a house shape. @param x the left of the bounding rectangle @param y the top of the bounding rectangle @param width the width of the bounding rectangle */ public HouseShape( int x, int y, int width) { Rectangle2D.Double base

public void mouseDragged(MouseEvent event) { Point lastMousePoint = mousePoint; mousePoint = event.getPoint(); for (SceneShape s : shapes) { if (s.isSelected()) { double dx = mousePoint.getX() - lastMousePoint.getX(); double dy = mousePoint.getY() - lastMousePoint.getY(); s.translate((int) dx, (int) dy); } } repaint(); } }); }

Adds an shape to the scene. @param s the shape to add */ public void add(SceneShape s) { shapes.add(s); repaint(); }

Removes all selected shapes from the scene. */ public void removeSelected() { for (int i = shapes.size() - 1; i >= 0; i--) { SceneShape s = shapes.get(i); if (s.isSelected()) shapes.remove(i); } repaint(); }

public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; for (SceneShape s : shapes) { s.draw(g2); if (s.isSelected()) s.drawSelection(g2); } }

private ArrayList shapes; private Point mousePoint; }