Week 15 Monday, Dec 3 Final: Thursday, Dec 12, 1:00-3:00 version 0.2: add JMenuBar, JMenu, JMenuItem. add a global event listener: implements ActionListener supplies actionPerformed(ActionEvent e) method call addActionListener(this) for all JMenuItem things Note what MenuItems now *do* version 0.2 uses the ImageViewer class as its ActionListener. Old-style event manager: (Applet style) windows widgets typically have an OnMouseClick, OnMouseDown, OnMouseMove, etc You have your own instance extend that class, and provide its own method. Any mouseclicks now get translated! Look up how addActionListener really works Discuss Exercise 11.9: having openItem, quitItem, saveItem respectively call openFile(), saveFile(), quit(). Who can have an addActionListener? What events to the object trigger it? AbstractButton class. Some Events and Their Associated Event Listeners Act that Results in the Event Listener Type User clicks a button, presses Enter while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener Any property in a component changes such as the text on a label PropertyChangeListener Big problem: event dispatch Better: have each JMenuItem listen for activation, & call appropriate method menuItem is an ActionButton addActionListener is a method of class AbstractButton menuItem extends AbstractButton JLabel, MenuBar do NOT ================= Inner classes Digression: what about making Rooms inner classes of Game? SOLVES the inventory-access problem! But generally makes a muddle of things. Demo of: class OpenActionListener class QuitActionListener Where do these *go*?? Weirdness: just use once! "Anonymous" inner classes: define & create in one place, without a class name version 0-3: implements anonymous classes!! Also, quit() now exits instead of printing. Specifically for menu actionListeners ------------------- How do we get from (complicated) file formats to a simple model of an image in which all we have to do is access the pixel at location (x,y)?? version 0-4: Groundwork for displaying images. Keyboard shortcuts for menus ImagePanel extends JComponent: panel for the image where does repaint() come from? from JComponent paintComponent: our revision of a JComponent method OFImage: our own format for the image itself extends BufferedImage in java.awt.image Question 1: if it's our own class, how do files get opened? How do images get displayed? Question 2: is it really just a renamed BufferedImage? ImageFileManager: ========================================== version 1.0: adds menu of darker/lighter/threshold layoutManager NORTH/CENTER/SOUTH version 2.0: adds abstract Filter class version 3.0: uses layout manager and buttons What do YOU have to do to get buttons to work? adds WEST to layout manager ========================================================================= Wednesday: A little bit more GUI Example of making Filter be an interface. Problem: the field "name" Solution: put getName() in the interface. But this means that we no longer create names when we create the menu layouts BlueJ "Layout" project ========================================================= Friday: Final will be OPEN BOOK (but not notes) FoxRabbit: Field.java 2-D arrays class FieldRow { public Animal[] row; FieldRow(int width) {row = new Animal[width];} } private FieldRow[] field; ... field = new FieldRow[depth]; for (int i=0; i