1 object-oriented programming (java), unit 22 kirk scott

33
1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

Upload: kimberly-marion-ryan

Post on 24-Dec-2015

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

1

Object-Oriented Programming (Java), Unit 22

Kirk Scott

Page 2: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

2

Keystrokes, Menus, and Multiple Frames

• 22.1 Keystrokes

• 22.2 Menus

• 22.3 Multiple Frames

Page 3: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

3

22.1 Keystrokes

• ClickKey• The idea of this example is that the seeds will

jump between cups if the keys A or B are pressed, if the corresponding cup is active.

• A screenshot of this application would look no different from that of the previous example.

• The complete UML diagram of the new application follows.

Page 4: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

4

ClickKey

ClickKeyFrame

ContentPane

+paintComponent()+setCup()

ClickKeyPanel

+paintComponent()+repaint()

JPanel

-has1

1

-has1

1

-has added1

1

SeedCup

-has1

2

MouseHandler

-has1

1

WindowCloser

-has

11WindowAdapter

1

-has instance variable

1

Rectangle

-has

1

1

MouseAdapter

JFrame

ArrayList

-has1

1

Seed

-has1

0..4

MouseMotionHandlerMouseMotionAdapter

-has1

1

KeyHandler KeyAdapter

1

1

1

1

Page 5: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

5

This is the only part of the UML diagram that is new.

+paintComponent()+setCup()

ClickHandPanel

KeyHandler KeyAdapter

1

1

1

1

+paintComponent()+setCup()

ClickKeyPanel

KeyHandler KeyAdapter

Page 6: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

6

Changes in the panel class:

• The panel has an instance of the KeyHandler class added to it using the addKeyListener() method.

• An important additional change is that it is necessary to make the panel focusable.

• That means making it independently able to receive input.

• This is accomplished by a call to the setFocusable() method.

Page 7: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

7

• class ClickKeyPanel extends JPanel• {• private SeedCup myCupA;• private SeedCup myCupB;• private SeedCup whichCupIsActive;• public ClickKeyPanel()• {• myCupA = new SeedCup(4, 200, 200, 40, 40);• myCupB = new SeedCup(0, 250, 200, 40, 40);• whichCupIsActive = myCupA;• addMouseListener(new MouseHandler());• addMouseMotionListener(• new MouseMotionHandler());• addKeyListener(new KeyHandler());• setFocusable(true);• }

Page 8: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

8

The code for the KeyHandler class:

• Its basic logic is the same as that of the mouse handler.• The difference is that the event which it detects is a

keystroke which is not entered into a text field.• Just as it’s possible to get the location of a mouse click,

it is possible to get the keystroke that was entered when the panel has focus.

• In the application, the cups are known as cup A and cup B.

• Pressing either of the two keys A or B has the effect of selecting the corresponding cup if it’s active.

Page 9: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

9

• private class KeyHandler extends KeyAdapter• {• public void keyTyped(KeyEvent event)• {• char keyChar = event.getKeyChar();• if(keyChar == 'a' && whichCupIsActive == myCupA)• {• myCupA.moveCupValueTo(myCupB);• whichCupIsActive = myCupB;• repaint();• }• else if(keyChar == 'b' && whichCupIsActive == myCupB)• {• myCupB.moveCupValueTo(myCupA);• whichCupIsActive = myCupA;• repaint();• }• else• {• }• }• }

Page 10: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

10

22.2 Menus

• ClickMenu

• This program includes a simple menu with two options.

• One causes the program to exit.

• The other causes the application to restart, or reset itself to its initial state.

Page 11: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

11

ClickMenu Screenshot:

Page 12: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

12

ClickMenu Listeners:• The code for the application includes RestartListener and

ExitListener classes.• These two classes implement the ActionListener interface, which

has just one method in it, actionPerformed().• The following UML diagram shows the structure of menus, menu

items, and the listeners that go with them.• Unlike the handlers associated with the mouse, which belong to the

panel, the menu and the listeners for its two options belong to the frame.

• A menu bar is created and added to the frame with a call to setJMenuBar().

• A menu is then added to the menu bar, various menu items are added to it, and listeners are added to the items.

• The UML diagram follows.

Page 13: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

13

ClickMenu

ClickMenuFrame

ContentPane

+paintComponent()+setCup()

ClickMenuPanel

+paintComponent()+repaint()

JPanel

-has1

1

-has11

-has added1

1

SeedCup

-has

1

2

MouseHandler

-has

1

1

WindowCloser

-has

11WindowAdapter

1

-has instance variable

1

Rectangle

-has1

1

MouseAdapter

JFrame

ArrayList

-has1

1

Seed

-has1

0..4

MouseMotionHandlerMouseMotionAdapter

-has1

1

JMenuBar

JMenu

JMenuItem JMenuItem

RestartListener ExitListener

-has

1

1

-has11

-has11

-has11

-has11

-has11

«interface»ActionListener

«interface»ActionListener

Page 14: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

14

Here is the code for the frame class, showing how the menu elements shown in the UML diagram are constructed and assembled:

• class ClickMenuFrame extends JFrame• {• private ClickMenuPanel myPanel;• private final int FRAMEW = 500;• private final int FRAMEH = 500;• public ClickMenuFrame()• {• setTitle("ClickMenu Frame");• setSize(FRAMEW, FRAMEH);• myPanel = new ClickMenuPanel();• Container contentPane = getContentPane();• contentPane.add(myPanel, "Center");• addWindowListener(new WindowCloser());

Page 15: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

15

• JMenuBar menuBar = new JMenuBar();• setJMenuBar(menuBar);• JMenu fileMenu = new JMenu("File");• menuBar.add(fileMenu);• JMenuItem restartItem = new JMenuItem("Restart");• fileMenu.add(restartItem);• RestartListener myRestartListener = new

RestartListener();• restartItem.addActionListener(myRestartListener);• JMenuItem exitItem = new JMenuItem("Exit");• fileMenu.add(exitItem);• ExitListener myExitListener = new ExitListener();• exitItem.addActionListener(myExitListener);• }

Page 16: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

16

Here is the listener for the Restart menu option. It calls a method to renew the cups in the panel.

• private class RestartListener implements ActionListener• {• public void actionPerformed(ActionEvent event)• {• myPanel.renewCups();• }• }

Page 17: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

17

Here is the listener for the Exit menu option. It duplicates the functionality of the WindowCloser class.

Taking the Exit option ends the application.

• private class ExitListener implements ActionListener• {• public void actionPerformed(ActionEvent event)• {• System.exit(0);• }• }• }

Page 18: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

18

Here is the renewCups() method in the panel class. It duplicates part of the functionality of the constructor for that

class, replacing the cups and then repainting.

• public void renewCups()• {• myCupA = new SeedCup(4, 200, 200, 40, 40);• myCupB = new SeedCup(0, 250, 200, 40, 40);• whichCupIsActive = myCupA;• repaint();• }

Page 19: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

19

22.3 Multiple Frames

Page 20: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

20

ClickMany

• This program makes it possible to open up more than one frame with cups in it.

• The menu for the main frame allows you to open up the new frames or exit the application overall.

• The menu for a subframe allows you to restart or close that frame.

Page 21: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

21

Page 22: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

22

• There are two UML diagrams for this application.

• This first one shows the relationships of the classes associated with the main frame of the program.

Page 23: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

23

ClickMany

ClickManyFrame

-has11JFrame

JMenuBar

JMenu

JMenuItem JMenuItem

MakeSubFrameListener ExitListener

-has11

-has11

-has11

-has11

-has11 -has11

«interface»ActionListener

«interface»ActionListener

ClickSubFrame

1*

Page 24: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

24

• The second UML diagram for the application shows relationships of the classes associated with the subframes of the program.

• Notice how the application panel, cups, and seeds are unchanged from the previous versions of the application, but they now descend from the subframe rather than the main frame.

• The structure of the menu belonging to the subframe is analogous to that of the main frame, but the menu items have different names and their listeners implement the corresponding functionality.

• This application still has key handling functionality, but in order to keep the diagrams a little simpler, it is not shown in them.

Page 25: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

25

ClickSubFrame

ContentPane

+paintComponent()+setCup()

ClickManyPanel

+paintComponent()+repaint()

JPanel

-has1

1

-has added1

1

SeedCup

-has

1

2

MouseHandler

-has

1

1

WindowCloser

-has

1

1WindowAdapter 1

-has instance variable

1

Rectangle

-has1

1

MouseAdapter

JFrame

ArrayList

-has1

1

Seed

-has1

0..4

MouseMotionHandlerMouseMotionAdapter

-has1

1

JMenuBar

JMenu

JMenuItem JMenuItem

RestartListener CloseListener

-has

1

1

-has11

-has11 -has1 1

-has11

-has11

«interface»ActionListener

«interface»ActionListener

Page 26: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

26

• Portions of the code for the application follow.• Notice first of all the main frame is simplified

somewhat by removing the WindowCloser and going back to the default close operation.

• When the main frame is closed, the application ends.

• class ClickManyFrame extends JFrame• …• setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Page 27: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

27

• The main frame class has a listener for the menu option to make a subframe.

• This code is shown next.

• Notice how each new subframe is located at an offset from the previous one so they don’t completely overlap each other on the screen.

Page 28: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

28

• private class MakeSubFrameListener implements ActionListener

• {• public void actionPerformed(ActionEvent event)• {• ClickSubFrame myframe = new ClickSubFrame();• myframe.setLocation(subFrameLocation,• subFrameLocation);• if(subFrameLocation >= 500)• {• subFrameLocation = 50;• }• else• {• subFrameLocation += 50;• }• myframe.setVisible(true);• }• }

Page 29: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

29

• The subframe of this application is similar to the main frames of the previous example applications.

• The subframe has menu items for restarting and closing.

• The RestartListener implements the restarting of the application by renewing the cups in the panel.

Page 30: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

30

• private class RestartListener implements ActionListener• {• public void actionPerformed(ActionEvent event)• {• myPanel.renewCups();• }• }

Page 31: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

31

• The CloseListener for the subframe does something different from the WindowCloser or Exit menu options for the main frame.

• Closing a subframe does not end the application. It merely disposes of that particular subframe.

• private class CloseListener implements ActionListener

• {• public void actionPerformed(ActionEvent event)• {• dispose();• }• }• }

Page 32: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

32

The renewCups() method in the panel class duplicates all of the logic of the constructor,

plus a call to repaint().• public void renewCups()• {• myCupA = new SeedCup(4, 60, 70, 40, 40);• myCupB = new SeedCup(0, 140, 70, 40, 40);• whichCupIsActive = myCupA;• addMouseListener(new MouseHandler());• addMouseMotionListener(new

MouseMotionHandler());

• addKeyListener(new KeyHandler());

• setFocusable(true);

• repaint();• }

Page 33: 1 Object-Oriented Programming (Java), Unit 22 Kirk Scott

33

The End