Design for Button5
Design of Button5 Program
Main:
- Instance Variables: none
- Methods: none
- Responsibilities:
- Set up the main window
- Create main objects we need: MyCanvas, Application, 2 control panels
- Lay out the main window (canvas, 2 control panels)
MyCanvas:
- Instance Variables:
- Methods:
- paintComponent() callback, overloaded from JComponent
- MouseListener callbacks, overloaded from MouseListener
- (setApplication(), to avoid forward reference)
- Responsibilities:
- Catch paintComponent() callback and delegate to Application
- Catch mousePressed() callback and delegate to Application
- Uses:
- Application: which does the redraw by calling Square
- Application: which does the input by calling Square
Application:
- Instance Variables:
- Pointer to MyCanvas
- Application data: List of squares, boolean drawX
- [and setX()]
- Current square
- [and getCurrent()]
- Methods:
- drawApplication()
- doMouse()
- Responsibilities:
- Keep list of Squares
- Handle functions that iterate over list (drawApplication, doMouse)
- Remember current square (really belongs in UI though)
- Maintain and handle drawX option
- Uses:
- MyCanvas: to redraw after change drawX
- Square: to redraw themselves
- Square: to input themselves
Square (is a part of the "application" data)
- Instance Variables:
- Pointer to MyCanvas
- Info about the square: loc
- Methods:
- draw()
- left(), right(), bigger(), smaller()
- boolean isInside()
- Responsibilities:
- Initialize to unique starting position
- Initialize and maintain "application data"
- Draw yourself (given g and isCurrent)
- Know how to move left/right/bigger/smaller
- Answer question is Point inside you?
- Uses:
- MyCanvas: for redrawing everything after move
ControlGlobal:
- (Inherited from ControlPanel)
- Methods:
- Responsibilities:
- Set up JPanel
- Create button
- Uses:
- Application: just to pass to Button* constructors
ControlSingle:
- (Inherited from ControlPanel)
- Methods:
- Responsibilities:
- Set up JPanel
- Create buttons
- Uses:
- Application: just to pass to Button* constructors
ButtonLeft, Right, Bigger, Smaller
- (Inherited from MyButton)
- Methods:
- actionPerformed() callback
- Responsibilities:
- Set up the JButton
- Provide the callback
- Uses:
- Application: for getCurrent
- Square: for action on getCurrent
ButtonX
- (Inherited from MyButton)
- Instance Variables:
- Methods:
- actionPerformed() callback
- Responsibilities:
- Set up the JButton
- Provide the callback
- Remember current state of toggle
- Uses:
- Application: to execute setX
Aggregation Hierarchy
Main
- MyCanvas
- Application
- ControlGlobal
- ControlSingle
- ButtonBigger
- ButtonLeft
- ButtonRight
- ButtonSmaller
Class Hierarchy
Main
MyCanvas
Application
Square
ControlPanel
- ControlGlobal
- ControlSingle
MyButton
- ArrowButton
- TextButton
- ButtonBigger
- ButtonSmaller
- ButtonX