1 / 48

Object-Oriented Programming (Java), Unit 18

Object-Oriented Programming (Java), Unit 18. Kirk Scott. Wisdom the Albatross. From news.discovery.com

chana
Download Presentation

Object-Oriented Programming (Java), Unit 18

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 18 Kirk Scott

  2. Wisdom the Albatross • From news.discovery.com • Not many moms in their 60s can boast of raising 30 offspring while having a new one in the nest. But such is the claim of the aptly named Wisdom, a Laysan albatross nesting in the South Pacific that is also the oldest known wild bird in the 90-year history of North American bird banding.

  3. “She looks great,” said Bruce Peterjohn, chief of the North American Bird Banding Program in a USGS news release.

  4. From seattletimes.com • Albatrosses mate for life, suggesting that Wisdom probably had to find a new, younger mate maybe twice down the line. They work at a relationship, first by getting their groove on. “They dance together,” said Chandler Robbins, a retired senior scientist at USGS.

  5. Inner Classes, Integrated Text Fields, Listeners, and Event Handling • 18.1 Inner Classes • 18.2 Integrated Text Fields, Listeners, and Event Handling • 18.3 A More Flexible Design

  6. This unit consists of 3 example programs, Echo4 through Echo6. • These programs continue the process of integrating graphical user interface features into application programs.

  7. 18.1 Inner Classes • Inner classes were mentioned in a previous unit. • Formally, a class is an inner class if the code defining it is completely contained in the code of another class. • Inner classes have access to the instance variables of the class that contains them.

  8. Echo4 • This example does nothing except illustrate the fact that inner classes have access to the instance variables of the classes that contain them. • There is no screen shot for it, because there’s nothing to see. • In future examples it will be seen how inner classes can be used when writing code for graphical user interfaces.

  9. In the following code, note the default constructor for the inner class. • Inside that constructor, the instance variable of the outer class is used when initializing the instance variable of the inner class. • No get method is called, illustrating that the inner class has direct access to the instance variable of the outer class.

  10. public class Echo4 • { • public static void main(String[] args) • { • MyOuterClass myOuterObject = new MyOuterClass(5); • myOuterObject.makeInnerObject(); • } • }

  11. class MyOuterClass • { • private int outerClassVariable; • public MyOuterClass(int inval) • { • outerClassVariable = inval; • } • public void makeInnerObject() • { • MyInnerClass innerObject = new MyInnerClass(); • } • private class MyInnerClass • { • private int innerClassVariable; • public MyInnerClass() • { • innerClassVariable = outerClassVariable; • } • } • }

  12. 18.2 Integrated Text Fields, Listeners, and Event Handling • A listener is a piece of code designed to detect an event that has occurred in a graphical user interface. • If the listener is implemented as an inner class, its code can be simpler, because it has access to the instance variables of the class that contains it. • Echo5 illustrates the use of a listener in an application with an integrated text field.

  13. Echo5

  14. The screenshot illustrated that Echo5 is a genuine echoing program. • The difference with Echo4 is that it has an integrated text field. • Entering data into the text field triggers an event. • The following UML diagram shows that the text field has a listener, but the listener is implemented as an inner class of the JFrame of the application overall.

  15. Key points of this example relating to the text field and listener: • The use and positioning of an integrated text field in order to do input. • The use of a listener, which is the code which acts on the text field input. • The implementation of the listener as an inner class of the frame class, so that it can easily pass around the input string.

  16. The characteristics of the listener are defined by the fact that it implements a given interface. • The code for the listener can be placed so that it’s an inner class of the frame class. • At the same time, an instance of the listener can be created and associated with a text field, so the listener becomes a component that is within the containment hierarchy of the frame class in the application.

  17. Key points of this example relating to the panel: • The panel is also implemented as an inner class of the frame. • This emphasizes the characteristics of inner classes, and the direct access to instance variables that they afford, • Implementing the panel in this way means that fewer method calls are needed in order to assign values to variables than would otherwise be needed.

  18. In the long run, this is not a flexible design choice. • In future examples the panel will not be implemented as an inner class. • This will make it necessary to supply methods which allow the values of variables to be passed from one component to another as parameters.

  19. ☺☻☼♀♂♠♣♥♦♪♫♯Pay Attention: This is the most important point of this example overall:☺☻☼♀♂♠♣♥♦♪♫♯ • The previous echoing program examples all contained loops. • If you search the code for Echo5, you will not find a loop. • However, the application behaves as if it contained a loop. • Once begun, the application continues to run, and will echo successive inputs entered by the user.

  20. This behavior is the result of the Java event handling mechanism. • Effectively, this results from the use of a listener in the code. • The user code never calls the listener. • The Java system loops internally, detecting events. • When it detects the type of event which the listener is designed to handle, the listener is called.

  21. Calling the listener consists of calling the actionPerformed() method which the listener implements • The actionPerformed() method contains the application specific code which is to be executed when the listener is called.

  22. It is important to understand that the call to the listener code in the application comes from outside of the program. • It comes from the system. • When the listener is finished running, the application is idle, but alive.

  23. The Java system continues to check for events and will trigger the listener if one occurs. • The application comes to an end when its top level container, the frame, is closed. • A sequence diagram of how this happens is shown on the following overhead.

  24. The code for Echo5 is shown on the following overhead. • Note the implementation of the JPanel and the listener as inner classes. • Especially note the definition of the listener and the actionPerformed() method that it contains.

  25. The point is this: The listener implements the ActionListener interface. • This is what makes it a listener. • The ActionListener interface requires that the implementing class have an actionPerformed() method.

  26. import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • public class Echo5 • { • public static void main(String[] args) • { • Echo5Frame myframe = new Echo5Frame(); • myframe.setVisible(true); • } • } • class Echo5Frame extends JFrame • { • private String stringInQuestion = ""; • private JTextField myField; • private Echo5Panel myPanel; • private final int FRAMEW = 500; • private final int FRAMEH = 500; • private final int STRINGX = 140; • private final int STRINGY = 240;

  27. public Echo5Frame() • { • setTitle("Echo5 Frame"); • setSize(FRAMEW, FRAMEH); • myField = new JTextField(); • TextFieldListener myListener = new TextFieldListener(); • myField.addActionListener(myListener); • myPanel = new Echo5Panel(); • Container contentPane = getContentPane(); • contentPane.add(myPanel, "Center"); • contentPane.add(myField, "North"); • }

  28. private class TextFieldListener implements ActionListener • { • public void actionPerformed(ActionEvent event) • { • stringInQuestion = myField.getText(); • myField.setText(""); • myPanel.repaint(); • } • }

  29. private class Echo5Panel extends JPanel • { • public Echo5Panel() • { • } • public void paintComponent(Graphics g) • { • Graphics2D g2 = (Graphics2D) g; • super.paintComponent(g2); • g2.drawString(stringInQuestion, STRINGX, STRINGY); • } • } • }

  30. 18.3 A More Flexible Design • The graphical appearance of Echo6 doesn’t differ from that of Echo5. • The difference is that the panel is no longer implemented as an inner class of the frame. • This requires the addition of a new method and a call to it in order to pass around the String to be echoed. • In spite of this extra work, not having the JPanel be an inner class is a more flexible design in the long run.

  31. Echo6

  32. The UML diagram shown on the next overhead is the similar to the UML diagram for Echo5 • The JPanel class is not an inner class. • Also, the setString() method that is needed is shown as an operation in the JPanel class.

  33. The sequence diagram on the following overhead shows how a call to setString() is needed in the process of echoing input from the text field as output in the panel. • Except for this call, the rest of the calls remain the same as before.

  34. The code for Echo6 follows. • It is quite similar to the code for Echo5. • Assuming Echo5 was reasonably clear, the main point is identifying where the code for Echo6 differs from it.

  35. import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • public class Echo6 • { • public static void main(String[] args) • { • Echo6Frame myframe = new Echo6Frame(); • myframe.setVisible(true); • } • } • class Echo6Frame extends JFrame • { • private JTextField myField; • private Echo6Panel myPanel; • private final int FRAMEW = 500; • private final int FRAMEH = 500;

  36. public Echo6Frame() • { • setTitle("Echo6 Frame"); • setSize(FRAMEW, FRAMEH); • myField = new JTextField(); • TextFieldListener myListener = new TextFieldListener(); • myField.addActionListener(myListener); • myPanel = new Echo6Panel(); • Container contentPane = getContentPane(); • contentPane.add(myPanel, "Center"); • contentPane.add(myField, "North"); • }

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

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

  39. Unit 18 Assignment • The previous unit ended with a description of the assignment. • That unit and that assignment began the middle part of the course, where versions of Wari may or may not be given, and the assignment for every unit is to implement Togiz Kumalak with the features discussed in the unit.

  40. Having been shown the plan in Unit 17, for this unit and the following ones, typically, there is no need to preview the assignment in the overheads. • You know, in principle, what it will always be…

  41. The End

More Related