Software Design: Figures
Today ColorPicker Layout Manager Observer Pattern Radio Buttons Prelimiary Discussion Exercise 5
ColorPicker They don't teach you the facts of death, Your mum and dad. They give you pets. Terry Pratchett (at age 13)
ColorPicker : Layout JPanel(new GridLayout(1, 2, 5, 5)) BorderLayout.NORTH rows=1 cols=2 hgap=5 vgap=5 JPanel(new FlowLayout()) BorderLayout.CENTER
ColorPicker : Layout JPanel(new GridLayout(3, 1, 3, 3)) JPanel(new GridLayout(3, 2)) JPanel(new GridLayout(2, 1, 5, 5)) JPanel(new GridLayout(0, 1))
ColorPicker UML : ColorModel public class ColorModel { private Color color; private List<ColorListener> listeners = new LinkedList<ColorListener>(); public void addcolorlistener(colorlistener l) { listeners.add(l); public void setcolor(color color) { if (!color.equals(this.color)) { this.color = color; for (ColorListener l : listeners){ l.colorvaluechanged(color); Avoid cycles! infinite recursion
has ColorPicker UML Example: Scrollbar <<interface>> Java.util.EventListener <<interface>> ColorListener CScrollBar <<interface>> AdjustmentListener JScrollBar 0..n 0..n updates contained in 1 1 ColorModel 1 1 ColorFrame JComponent etc.
ColorPicker UML : CScrollBar public interface ColorListener { void colorvaluechanged (Color c); <<interface>> ColorListener CScrollBar class CScrollBar extends JScrollBar implements AdjustmentListener, ColorListener { <<interface>> AdjustmentListener CScrollBar(ColorModel model, int type, int orientation, int val) { super(orientation, val, 0, 0, 255); this.model = model; this.type = type; addadjustmentlistener(this); model.addcolorlistener(this); public void adjustmentvaluechanged(adjustmentevent e) { // update model -> model.setcolor(c); public void colorvaluechanged(color color) { // set scrollbar value, e.g., setvalue(color.getred());
RadioButtons in Java/Swing (1) Definition: Radio buttons are groups of buttons in which, by convention, only one button at a time can be selected. JRadioButton yellow = new JRadioButton("yellow"); JRadioButton black = new JRadioButton("black"); JRadioButton white = new JRadioButton("white"); ButtonGroup bm = new ButtonGroup(); bm.add(yellow); bm.add(black); bm.add(white); How can we unselect a button?
RadioButtons in Java/Swing (2) ButtonGroup JavaDoc: "There is no way to turn a button programmatically to "off", in order to clear the button group. " Implementation with setselected without ButtonGroup
AWT vs. SWING Abstract Windowing Toolkit (ab java 1.0) Uses components (Button,...) of the operating system Swing (ab java 1.2) Only the windows are from the operating system The content of the window is drawn by java All component classes start with "J" (JButton,...) Mixing AWT and SWING Swing 'recycles' some classes from AWT (Graphics, LayoutManager, etc) Do not mix components Heavyweight / Leightweight
Exercise 5 Overview 1. Implementation of DrawModel 2. Implementation of some figures Line Oval... 3. Implementation of the corresponding tools LineTool, OvalTool,... 4. Registration of the Tools in class StdContext doregisterdrawtools () (Line 151) 5. Extraction of the commonalities from the figure and tool classes into abstract base classes.. ex. 4 ex. 5
Exercise 5: Classes to create <<interface>> jdraw.framework.drawmodel <<interface>> FigureListener DrawView DrawModelListener * * 1 1 DrawModel 0..1 * <<interface>> Figure your job New Change prepared in ex. 4 Line Rect Oval creates creates LineTool RectTool OvalTool StdContext registrates «interface» DrawTool
1. Tool selection Drawing of figures deactivate() method of previous tool is called activate() method of next tool is called Call to showstatustext ( Line Tool selected ) of DrawEditor sets Statusbar 2. Mouse-Down Click (inside DrawTool::mouseDown()) Creates new figure with current mouse position as origin (widht & height are zero). Adds new figure to draw model Calls showstatustext ( Adding Line ) in DrawEditor to set status bar 3. Dragging the mouse (inside DrawTool::mouseDrag()) Calls setbounds(mousedown-coordinates, current mouse coordinates) Calls showstatustext (currentfigure.tostring()) displays current figure properties in status bar Careful: overwrite tostring() 4. Release of mouse button (inside DrawTool::mouseUp()) Delete figure if mousedown == mouseup coordinates Call showstatustext ( Line Tool selected );
Implementation of own figure class 1. Figure must implement the Figure interface 2. Definition of figures Line: Start and end points Oval/Ellipse: Defined over corner points of the bounding box Question: What have those two figures in common? They are both describable by their bounding box 3. Figures draw themselfes in the draw(graphics g) method 4. Figure is an Observable Implements notification: E.g. in move(x,y) notifies listeners (your DrawModel) via FigureListener method 5. Figure::clone() and Figure::getHandles() may return null in this exercise (will be part of a later exercise).
Implementation of figure tools Tools create figures and are visible in the toolbar with an icon. Tools implement the interface DrawTool see RectTool. Tools know the controller (StdContext) are activated/deactivated set mouse cursor and status bar process mouse events have a name have an icon Icons for ellipse, line, and rectangle are in res/images see RectTool for an example
Registration of the tool Add them near line 151 in StdContext @Override protected void doregisterdrawtools() { DrawTool rectangletool = new RectTool(this); addtool(rectangletool); // add more tools here new tools then appear in the tool bar
Tips Reuse existing AWT classes such as java.awt.geom.line2d and java.awt.geom.ellipse2d. Think about how you can extract common behaviour into common base classes to avoid code duplication