1 / 56

Object-Oriented Programming (Java), Unit 19

Object-Oriented Programming (Java), Unit 19. Kirk Scott. Panels, Adapter Classes, Anonymous Inner Classes, and Application Classes. 19.1 Panels and Adapter Classes 19.2 Anonymous Inner Classes 19.3 Application Classes. 19.1 Panels. Echo7.

Download Presentation

Object-Oriented Programming (Java), Unit 19

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 Programming (Java), Unit 19 Kirk Scott

  2. Panels, Adapter Classes, Anonymous Inner Classes, and Application Classes • 19.1 Panels and Adapter Classes • 19.2 Anonymous Inner Classes • 19.3 Application Classes

  3. 19.1 Panels

  4. Echo7 • The visible difference between Echo6 and Echo7 is that the text field is given a fixed width.

  5. Structural and syntactical changes in Echo7: • The text field is given default text. • The text field is given a size. • The text field is put into its own panel in the frame. • If the text field is not put into its own panel, giving it a size has no effect. • The units for the width of the field do not correspond to the number of characters of the default font that are displayed in the field.

  6. Structural and syntactical changes in Echo7, cont’d.: • In Echo7 the application frame has an instance of the WindowCloser class. • The window closer is a kind of listener. • This listener defines what is to be done when the application is closed. • The WindowCloser class is a subclass of the WindowAdapter class, not an implementer of the WindowListener interface.

  7. The Adapter classes • An interface contains a signature for a method. • public void someMethod(parameter); • The corresponding adapter class contains an empty implementation. • public void someMethod(parameter) • { • }

  8. The Adapter classes, cont’d.: • Java provides an adapter class that corresponds to a listener class. • If you extend the adapter class, you inherit the empty implementations of all of the other methods. • You only need to override the methods that your class will need.

  9. Unlike with an interface, you do not have to include implementations of unneeded methods. • In this example only the windowClosing() method is overridden. • If a listener interface like ActionListener only has one method, there is no need to use an adapter.

  10. The UML diagram: • The UML diagram for Echo7 reflects all of the changes mentioned above. • The listeners, the window closer, and the text field listener continue to be inner classes of the application frame.

  11. The UML sequence diagram: • Java’s event handling mechanism is reminiscent of looping. • The sequence diagram on the next overhead shows that part of the application which is conceptually the body of the loop. • It outlines the full sequence of calls triggered by a call to actionPerformed(), the method in the text field listener.

  12. The code for Echo7: • import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • public class Echo7 • { • public static void main(String[] args) • { • Echo7Frame myframe = new Echo7Frame(); • myframe.setVisible(true); • } • }

  13. class Echo7Frame extends JFrame • { • private JTextField myField; • private Echo7Panel myOutputPanel; • private JPanel myInputPanel; • private final int FRAMEW = 500; • private final int FRAMEH = 500;

  14. public Echo7Frame() • { • setTitle("Echo7 Frame"); • setSize(FRAMEW, FRAMEH); • myOutputPanel = new Echo7Panel(); • myField = new JTextField("xyz", 24); • TextFieldListener myListener = new TextFieldListener(); • myField.addActionListener(myListener); • myInputPanel = new JPanel(); • myInputPanel.add(myField); • Container contentPane = getContentPane(); • contentPane.add(myOutputPanel, "Center"); • contentPane.add(myInputPanel, "North"); • addWindowListener(new WindowCloser()); • }

  15. private class TextFieldListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • String inputString = myField.getText(); • myOutputPanel.setString(inputString); • myField.setText(""); • myOutputPanel.repaint(); • } • }

  16. private class WindowCloser extends WindowAdapter • { • public void windowClosing(WindowEvent event) • { • System.exit(0); • } • } • }

  17. class Echo7Panel extends JPanel • { • private String stringInQuestion = ""; • private final int STRINGX = 140; • private final int STRINGY = 240; • public Echo7Panel() • { • } • public void paintComponent(Graphics g) • { • Graphics2D g2 = (Graphics2D) g; • super.paintComponent(g2); • g2.drawString(stringInQuestion, STRINGX, STRINGY); • } • public void setString(String stringIn) • { • stringInQuestion = stringIn; • } • }

  18. 19.2 Anonymous Inner Classes

  19. Echo8 • Anonymous inner classes can be used when a single instance of a class is needed and there is no reason for the class to have a name. • Anonymous classes are cryptic. • This syntax will not be used in further example programs.

  20. It is worth hearing about anonymous inner classes because you may encounter code where they have been used. • No screenshot is given for this example because the code still functions like the previous example.

  21. There is no official UML notation for anonymous inner classes: • The inability to include anonymous inner classes as labeled elements in a diagram is a (slight) drawback. • A small UML diagram is shown below which suggests that the Echo8Frame class has an anonymous inner class which implements the ActionListener interface.

  22. The code for Echo8: • The code below shows the syntax for making the action listener an anonymous inner class. • The listener is an inner class of the frame class and it is constructed inside the constructor for the frame class.

  23. When constructing the listener the keyword new is followed by a call to a default constructor. • This is followed by an opening brace and the class definition. • The class definition contains the implementation of the actionPerformed() method. • The class definition ends with a closing brace and this is followed by the semicolon which terminates the construction.

  24. import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • public class Echo8 • { • public static void main(String[] args) • { • Echo8Frame myframe = new Echo8Frame(); • myframe.setVisible(true); • } • }

  25. class Echo8Frame extends JFrame • { • private JTextField myField; • private Echo8Panel myOutputPanel; • private JPanel myInputPanel; • private final int FRAMEW = 500; • private final int FRAMEH = 500;

  26. public Echo8Frame() • { • setTitle("Echo8 Frame"); • setSize(FRAMEW, FRAMEH); • myOutputPanel = new Echo8Panel(); • myField = new JTextField("xyz", 24);

  27. /* A call is made to construct an instance of a class that implements the interface. The definition of the class immediately follows the constructor, and is part of the same line of code. */ • ActionListener myListener = new ActionListener() • { • public void actionPerformed(ActionEvent event) • { • String inputString = myField.getText(); • myOutputPanel.setString(inputString); • myField.setText(“”); • myOutputPanel.repaint(); • } • };

  28. myField.addActionListener(myListener); • JPanel myInputPanel = new JPanel(); • myInputPanel.add(myField); • Container contentPane = getContentPane(); • contentPane.add(myOutputPanel, "Center"); • contentPane.add(myInputPanel, "North"); • addWindowListener(new WindowCloser()); • }

  29. private class WindowCloser extends WindowAdapter • { • public void windowClosing(WindowEvent event) • { • System.exit(0); • } • } • }

  30. class Echo8Panel extends JPanel • { • private String stringInQuestion = ""; • private final int STRINGX = 140; • private final int STRINGY = 240; • public Echo8Panel() • { • } • public void paintComponent(Graphics g) • { • Graphics2D g2 = (Graphics2D) g; • super.paintComponent(g2); • g2.drawString(stringInQuestion, STRINGX, STRINGY); • } • public void setString(String stringIn) • { • stringInQuestion = stringIn; • } • }

  31. 19.3 Application Classes

  32. Echo9 • Echo9 introduces a cup class. • A cup contains a seed count and it also has attributes that describe it as a rectangle with x and y coordinates in the frame. • The application accepts an integer value in the text field which is used to set the seedCount of the cup. • When the seedCount is set, the application has to be repainted in order to reflect the change.

  33. The Echo9 screenshot: • In the screenshot a cup is represented by a rectangle. • The value of the seedCount variable is given using Unicode.

  34. The UML structure diagram differs from the previous one only by the presence of the Cup class instead of a String in the output panel.

  35. The UML Static Structure Diagram: • You can identify an input side and an output side of the application. • Actions which take in a value and change the state of the application are input. • Actions that are triggered by the call to repaint() and follow it, which cause the state of the application to be shown, are output.

  36. More on the UML Static Structure Diagram: • The text field listener has access to the instance of the Echo9Panel, the output panel, which is an instance variable of the frame. • When a seed count is entered into the text field, the listener calls the setCup() method on the panel, passing in the count. • The setCup() method calls the setSeedCount() method on the cup belonging to the panel, passing in the count. • The listener then calls repaint() on the panel.

  37. UML Sequence Diagrams for the Application • The first sequence diagram shows the actions that occur on the input side of the application:

  38. The second sequence diagram shows the actions that occur on the output side of the application: • In other words, this is the sequence of method calls triggered by the call to repaint().

  39. The call to repaint() triggers a call to the paintComponent() method of Echo9Panel. • Inside paintComponent() a call is made to drawCup(). • The graphics parameter is passed from paintComponent() to drawCup(), where methods to draw the rectangle and display the seedCount of the cup are called on the graphics parameter.

  40. The code for Echo9: • Notice these things in the code • There is now a cup class. • The cup is represented by an instance of the Rectangle class.

  41. Instances of the Rectangle are constructed when the cup is displayed. They are not saved as instance variables. • Whether or not things like the rectangle are saved as instance variables or constructed at display time becomes an important issue in future versions of the application

  42. import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • import java.awt.Graphics2D; • import java.awt.Rectangle; • public class Echo9 • { • public static void main(String[] args) • { • Echo9Frame myframe = new Echo9Frame(); • myframe.setVisible(true); • } • }

  43. class Echo9Frame extends JFrame • { • private JTextField myField; • private Echo9Panel myOutputPanel; • private JPanel myInputPanel; • private final int FRAMEW = 500; • private final int FRAMEH = 500;

  44. public Echo9Frame() • { • setTitle("Echo9 Frame"); • setSize(FRAMEW, FRAMEH); • myOutputPanel = new Echo9Panel(); • myField = new JTextField("4", 24); • TextFieldListener myListener = new TextFieldListener(); • myField.addActionListener(myListener); • JPanel myInputPanel = new JPanel(); • myInputPanel.add(myField); • Container contentPane = getContentPane(); • contentPane.add(myOutputPanel, "Center"); • contentPane.add(myInputPanel, "North"); • addWindowListener(new WindowCloser()); • }

  45. private class TextFieldListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • String inputString = myField.getText(); • myOutputPanel.setCup(Integer.parseInt(inputString)); • myField.setText(""); • myOutputPanel.repaint(); • } • }

More Related