Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.

56
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1

Transcript of Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.

Object-Oriented Programming (Java), Unit 19

Kirk Scott

1

Panels, Adapter Classes, Anonymous Inner Classes, and Application Classes

• 19.1 Panels and Adapter Classes

• 19.2 Anonymous Inner Classes

• 19.3 Application Classes

2

19.1 Panels

3

Echo7

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

4

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.

5

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.

6

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)• {• }

7

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.

8

• 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.

9

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.

10

Echo7

Echo7Frame

ContentPane

JFrame

+paintComponent()+setString()

Echo7Panel

+paintComponent()+repaint()

JPanel

-has1

1

-has1

1

-has added1

1

JTextFieldGraphics

-has added1

1

-has1

1

+actionPerformed()

TextFieldListener

String

-has

1

1

WindowCloser

-has

11WindowAdapter

1

-has instance variable1

-has1

1

JPanel

-has added1

1

1

-has instance variable1

1

-has instance variable1

«interface»ActionListener 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

myListener myField myOutputPanel

actionPerformed()

getText()

inputString

setString(inputString)

setText("")

repaint()

13

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);• }• }

14

• class Echo7Frame extends JFrame• {• private JTextField myField;• private Echo7Panel myOutputPanel;

• private JPanel myInputPanel;• private final int FRAMEW = 500;• private final int FRAMEH = 500;

15

• 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());• }

16

• private class TextFieldListener implements ActionListener

• {• public void

actionPerformed(ActionEvent event)• {• String inputString =

myField.getText();•

myOutputPanel.setString(inputString);• myField.setText("");• myOutputPanel.repaint();• }• }

17

• private class WindowCloser extends WindowAdapter

• {• public void windowClosing(WindowEvent event)

• {• System.exit(0);• }• }• }

18

• 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;• }• }

19

19.2 Anonymous Inner Classes

20

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.

21

• 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.

22

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.

Echo8Frame

«interface»ActionListener

23

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.

24

• 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.

25

• 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);• }• }

26

• class Echo8Frame extends JFrame• {• private JTextField myField;• private Echo8Panel myOutputPanel;

• private JPanel myInputPanel;• private final int FRAMEW = 500;• private final int FRAMEH = 500;

27

• public Echo8Frame()• {• setTitle("Echo8 Frame");• setSize(FRAMEW, FRAMEH);• myOutputPanel = new Echo8Panel();

• myField = new JTextField("xyz", 24);

28

/* 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();• }• };

29

• myField.addActionListener(myListener);• JPanel myInputPanel = new JPanel();• myInputPanel.add(myField);• Container contentPane =

getContentPane();• contentPane.add(myOutputPanel,

"Center");• contentPane.add(myInputPanel,

"North");• addWindowListener(new WindowCloser());• }

30

• private class WindowCloser extends WindowAdapter

• {• public void windowClosing(WindowEvent event)

• {• System.exit(0);• }• }• }

31

• 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;• }• }

32

19.3 Application Classes

33

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.

34

The Echo9 screenshot:

• In the screenshot a cup is represented by a rectangle.

• The value of the seedCount variable is given using Unicode.

35

• 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.

36

Echo9

Echo9Frame

ContentPane

JFrame

+paintComponent()+setCup()

Echo9Panel

+paintComponent()+repaint()

JPanel

-has1

1

-has1

1

-has added1

1

JTextFieldGraphics

-has added1

1

-has1

1

+actionPerformed()

TextFieldListener

+setCupValue()+drawCup()

Cup

-has

1

1

WindowCloser

-has

11WindowAdapter

1

-has instance variable1

-has1

1

JPanel

-has added

1

1

1

-has instance variable1

1

-has instance variable1

«interface»ActionListener 37

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.

38

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.

39

UML Sequence Diagrams for the Application

• The first sequence diagram shows the actions that occur on the input side of the application:

40

myListener myField myOutputPanel

actionPerformed()

getText()

inputString

setCup(Integer.parseInt(inputString))

setText("")

repaint()

myCup

setSeedCount()

41

• 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().

42

• 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.

43

myOutputPanel myCup g2

repaint()

drawCup(g2)

draw(Rectangle...)

drawString(Integer...)

update()

paintComponent()

super.paintComponent()

44

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.

45

• 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

46

• 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);• }• }

47

• class Echo9Frame extends JFrame• {• private JTextField myField;• private Echo9Panel myOutputPanel;

• private JPanel myInputPanel;• private final int FRAMEW = 500;• private final int FRAMEH = 500;

48

• 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());• }

49

• private class TextFieldListener implements ActionListener

• {• public void actionPerformed(ActionEvent event)• {• String inputString = myField.getText();•

myOutputPanel.setCup(Integer.parseInt(inputString));• myField.setText("");• myOutputPanel.repaint();• }• }

50

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

51

• class Echo9Panel extends JPanel• {• private Echo9Cup myCup;• public Echo9Panel()• {• myCup = new Echo9Cup(0, 200, 200, 40, 40);• }• public void paintComponent(Graphics g)• {• Graphics2D g2 = (Graphics2D) g;• super.paintComponent(g2);• myCup.drawCup(g2);• }• public void setCup(int inValue)• {• myCup.setSeedCount(inValue);• }• }

52

• class Echo9Cup• {• private int seedCount;• private int x;• private int y;• private int w;• private int h;• private int textX;• private int textY;

53

• public Echo9Cup(int inValue, int cupX, int cupY, int cupW, int cupH)

• {• seedCount = inValue;• x = cupX;• y = cupY;• w = cupW;• h = cupH;• textX = cupX + (int) (.25 * cupW);• textY = cupY + (int) (.6 * cupH);• }

54

• public void setSeedCount(int inValue)• {• seedCount = inValue;• }• public void drawCup(Graphics2D g2)• {• g2.draw(new Rectangle(x, y, w, h));•

g2.drawString(Integer.toString(seedCount), textX, textY);

• }• }

55

The End

56