1 / 21

Object-Oriented Frameworks

Object-Oriented Frameworks. Supplementary reading: Java text (e.g. Budd Ch. 21, Ch. 22). Frameworks. Framework classes. A framework is a set of classes that cooperate closely and collectively embody a reusable solution to a common problem

Download Presentation

Object-Oriented Frameworks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Frameworks Supplementary reading: Java text (e.g. Budd Ch. 21, Ch. 22)

  2. Frameworks Framework classes • A framework is a set of classes that cooperate closely and collectively embody a reusable solution to a common problem • A framework is intended to be extended to meet the needs of a particular application • Extension is carried out by subclassing, overriding methods, and implementing interfaces << interface >> Application classes COMPSCI 230 Software Design & Construction

  3. Reuse and specialisation Window setTitle( title : String ) : void setSize( width : int, height : int ) : void moveTo( x : int, y : int ) : void addMenu( menu : Menu ) : void repaint( ) : void mouseDown( x : int, y : int ) : void keyPress( c: char ) : void Paint( ) : void } Code reuse. Methods called by class users and subclasses. } Concept reuse. Methods that are applicable to any kind of Window, but for which meaningful implementation is deferred to subclasses. A windowing framework will expect any kind of Window object to understand, for example, a paint( ) message. However, the windowing framework won’t know the application-specific response. Such responses are implemented by subclassing and method overriding. SpecialWindow COMPSCI 230 Software Design & Construction

  4. Inversion of control Time Application calls an I/O library routine Application calls a sort routine Application code Application code Framework code Library code Framework generates a window resize event Framework generates a window deiconification event Time Framework generates a button click event Control flow is dictated by the framework. The application programmer implements routines invoked by the framework, but doesn’t change the solution’s overall structure. The traditional case where the application code defines the flow of execution. COMPSCI 230 Software Design & Construction We’ve already come across the idea of inverted control structures. Where?

  5. Java applets: a simple framework Applet init( ) start( ) paint( ) stop( ) destroy( ) init( ) start( ) destroy( ) paint( ) stop( ) Application code Applet framework (web browser) COMPSCI 230 Software Design & Construction

  6. Generalisation add( ) is a polymorphic method; it can take as actual argument any kind of Component. paint( ) is a hook method. A Container is a special component that can nest other components, be they simple components (e.g. Buttons, Labels) or themselves Containers (e.g. Panels). Specialisation Object Java’s AWT framework A Component is something that can be displayed on a GUI and with which a user can interact. repaint( ) schedules a Component instance for repainting. The AWT framework will subsequently call paint( ) on the Component object. Component getSize( ) : int setSize( width : int, height : int ) : void setVisible( visible : boolean ) : void repaint( g : Graphics ) : void paint( g : Graphics ) : void addMouseListener( listener : MouseListener ) : void addKeyListener( listener : KeyListener ) : void Button Checkbox Choice Label List Scrollbar Container setLayout( mgr : LayoutManager ) : void add( c : Component ) : void TextComponent Panel Window toFront( ) toBack( ) ScrollPanel A Window is a special Container that can be stacked, and moved to the front/back. TextArea TextField Frame setTitle( title : String ) : void setCursor( c : int ) : void setResizable( resizable : boolean ) : void setMenuBar( mb : MenuBar ) : void A Frame is a special Window with a title, menubar, border and cursor. Interpret this diagram and write down at least 3 points of interest. COMPSCI 230 Software Design & Construction

  7. Panel TextField Component Frame Label Label Button Panel Container add( c : Component ) : void Button Panel Frame instance Panel instance Panel instance Panel instance Label instance TextField instance Label instance Button instance Button instance class Container extends Component { private List<Component> children; … public void add( Component c ) { children.add( c ); } } COMPSCI 230 Software Design & Construction

  8. 1: paint( ) 2: paint( ) 7 Frame instance 5 Panel instance Panel instance Panel instance 9 3: paint( ) 4: paint( ) 6 8 Label instance TextField instance Label instance Button instance Button instance Traversing the nested structure class Container extends Component { private List<Component> children; … public void add( Component c ) { children.add( c ); } public void paint( Graphics g ) { for( Component c : children ) c.paint( ); } } COMPSCI 230 Software Design & Construction

  9. Layout management Why is this design for layout management so effective? Component << interface >> LayoutManager layoutContainer( c : Container ) minimumLayoutSize( c : Container ) : Dimension preferredLayoutSize( c : Container ) : Dimension Container setLayout( mgr : LayoutManager ) : void doLayout( ) add( c : Component ) : void BorderLayout BoxLayout FlowLayout GridLayout COMPSCI 230 Software Design & Construction

  10. Layout management Alternative design: for each special kind of container, implement further subclasses, one for each kind of layout strategy. Container Panel Frame … PanelWithBorderLayout … PanelWithBoxLayout FrameWithBorderLayout … FrameWithBoxLayout COMPSCI 230 Software Design & Construction

  11. Layout management LayoutManager instance public class Container extends Component { … private List< Component > children; private LayoutManagerlayoutManager; public void setLayout( LayoutManager lm ) { layoutManager = lm; } public void doLayout( ) { layoutManager.layout( this ); } } 2: layoutContainer( this ) 1: doLayout( ) Frame instance Panel instance Panel instance Panel instance Label instance TextField instance Label instance Button instance Button instance COMPSCI 230 Software Design & Construction

  12. Components generate events in response to user interaction To hook in application-specific responses to events, listeners are used A listener is an object that is registered with a component; in handling user interaction the component informs any registered listeners of the event Event handling Registered listener executes Application code Framework code Framework generates button click event Framework generates next event Framework waits for next user interaction COMPSCI 230 Software Design & Construction

  13. Button addActionListener( listener : ActionListener ) Event handling << interface >> ActionListener actionPerformed( e : ActionEvent ) : void << interface >> KeyListener keyPressed( e : KeyEvent ) : void keyReleased( e : KeyEvent ) : void keyTyped( e : KeyEvent ) : void << interface >> MouseListener mouseReleased( e : MouseEvent ) : void mouseClicked( e : MouseEvent ) : void mouseEntered( e : MouseEvent ) : void mouseExited( e : MouseEvent ) : void AWT framework ApplicationListener Application listener Button instance ActionListener Instance (an instance of a class that implements the ActionListener interface) COMPSCI 230 Software Design & Construction

  14. addActionListener( this ) actionPerformed( ) public class PalindromeGUI extends Frame implements ActionListener { private TextField text; private Button isPalindrome, quit; private Label output; public PalindromeGUI() { super( "Palindrome detector" ); … isPalindrome.addActionListener( this ); quit.addActionListener( this ); } public void actionPerformed( ActionEvent e ) { if( e.getSource( ) == isPalindrome ) { // isPalindrome pressed. String enteredText = text.getText( ); if( enteredText.length( ) > 0 ) { if( isPalindromic( enteredText ) ) { output.setText( enteredText + " is palindromic" ); } else { output.setText( enteredText + " is not palidromic" ); } } } else { // quit pressed. System.exit( 0 ); } } } PalindromeGUI / ActionListener instance isPalindrome quit listener listener Button instance Button instance COMPSCI 230 Software Design & Construction

  15. public class PalindromeGUI extends Frame { private TextFieldtext; private Button isPalindrome, quit; private Label output; public PalindromeGUI( ) { … isPalindrome.addActionListener(new ActionListener( ) { public void actionPerformed( ActionEvent e ) { String enteredText = text.getText( ); if( enteredText.length( ) > 0 ) { if( isPalindromic( enteredText ) ) { output.setText( enteredText + " is palindromic" ); } } else { output.setText( enteredText + " is not palindromic" ); } } }); quit.addActionListener(new ActionListener( ) { public void actionPerformed( ActionEvent e ) { System.exit( 0 ); } }); … } PalindromeGUI instance isPalindrome quit Button instance listener listener Button instance actionPerformed( ) ActionListener instance ActionListener instance COMPSCI 230 Software Design & Construction

  16. MouseListener public class MouseMain extends Frame { public MouseMain() { super( “MouseListener" ); Panel mousePanel = new MousePositionPanel( ); add( mousePanel ); setBounds( 100, 100, 200, 200 ); setVisible( true ); } public static void main( String[ ] args ) { MouseMain mouseMain = new MouseMain( ); } } class MousePositionPanel extends Panel implements MouseListener { private intmouseX, mouseY; public MousePositionPanel( ) { mouseX = 100; mouseY = 100; addMouseListener( this ); } public void paint( Graphics g ) { g.drawString( "(" + mouseX + ", " + mouseY + ")", mouseX, mouseY ); } public void mousePressed( MouseEvent e ) { mouseX = e.getX( ); mouseY = e.getY( ); repaint( ); } public void mouseReleased( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } } COMPSCI 230 Software Design & Construction

  17. Custom painting in AWT Component paint( g : Graphics ) : void repaint( ) : void << interface >> MouseListener • The MouseListener application is an example where custom painting is required • The MousePositionPanel component is painted by displaying the position where the mouse was clicked • Each time the mouse is clicked, the application updates its state and requests that its MousePositionPanel component be repainted Container Panel MousePositionPanel Overrides paint( ) To carry out custom painting in a subclass of Component, you should override method paint( ). Component subclasses should not override method repaint( ). COMPSCI 230 Software Design & Construction

  18. Swing • Swing is a more complex and powerful replacement for the AWT • Swing includes: • A richer set of GUI components, many of which are located in package javax.swing • By convention, Swing component names are prefixed by J (e.g. JButton, JLabel etc.) and are subclasses of JComponent, which is a subclass of the AWT’s Component class • Components that have separate models; the Swing framework originates from the Model-View-Control (MVC) paradigm • Components that have a pluggable look-and-feel • Cosmetic improvements – e.g. buttons and labels can show images rather than just text, and components can be decorated with a variety of borders • Swing’s painting model improves the AWT’s but is slightly more complex • Swing uses the AWT’s layout management and event handling infrastructure COMPSCI 230 Software Design & Construction

  19. Pluggable look and feel With a Swing application, it is possible to change the look-and-feel at run-time. What technique do you think has been used in implementing this feature? JButton setUI( delegate : ButtonUI ) : void << interface >> ButtonUI paint( g : Graphics ) : void Implemented by one class for each different look-and-feel, e.g. Windows, Metal, Motif. COMPSCI 230 Software Design & Construction

  20. Custom painting in Swing Component paint( g : Graphics ) : void repaint( ) : void << interface >> MouseListener public void paint ( Graphics g ) { paintComponent( g ); // Code to paint border. // Code to paint children. … } public void paintComponent( Graphics g ) { // Code to paint the background. } JComponent paintComponent( g : Graphics ) JPanel public void paintComponent( Graphics g ) { // Make sure the background is painted. super.paintComponent( g ); // Now do custom painting. g.drawString ( "(" + mouseX + ", " + mouseY + ")", mouseX, mouseY ); } MousePositionPanel For custom painting in Swing, JComponent subclasses should override paintComponent( ) (and, in general, make a super.paintComponent( ) call). JComponent subclasses should not override paint( ) or repaint( ). COMPSCI 230 Software Design & Construction

  21. Custom painting in Swing import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingMouseMain extends JFrame { public SwingMouseMain( ) { super( "Mouse Listener" ); JPanelmousePanel = new MousePositionPanel( ); add( mousePanel ); setBounds( 100, 100, 200, 200 ); setVisible( true ); } public static void main( String[ ] args ) { SwingMouseMainmouseMain = new SwingMouseMain( ); } } class MousePositionPanel extends JPanel { private intmouseX, mouseY; public MousePositionPanel( ) { mouseX = 100; mouseY = 100; addMouseListener( new MouseAdapter( ) { public void mousePressed( MouseEvent e ) { mouseX = e.getX( ); mouseY = e.getY( ); repaint( ); } } ); } public void paintComponent( Graphics g ) { super.paintComponent( g ); g.drawString( "(" + mouseX + ", " + mouseY + ")", mouseX, mouseY ); } } COMPSCI 230 Software Design & Construction

More Related