Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Object-Oriented Programming (Java), Unit 22 Kirk Scott."— Presentation transcript:

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

2 2 Keystrokes, Menus, and Multiple Frames 22.1 Keystrokes 22.2 Menus 22.3 Multiple Frames

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

4 4

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

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

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

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

9 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 { }

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

11 11 ClickMenu Screenshot:

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

13 13

14 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());

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

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

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

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

19 19 22.3 Multiple Frames

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

21 21

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

23 23

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

25 25

26 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);

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

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

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

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

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

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

33 33 The End


Download ppt "1 Object-Oriented Programming (Java), Unit 22 Kirk Scott."

Similar presentations


Ads by Google