java interfaces
play

Java Interfaces } An interface is more abstract than a class } Almost - PowerPoint PPT Presentation

Java Interfaces } An interface is more abstract than a class } Almost always completely abstract in specification } Not an actual class to be inherited KeyListener {interface} No constructor (not a class at all) Class #36: + void keyPressed(


  1. Java Interfaces } An interface is more abstract than a class } Almost always completely abstract in specification } Not an actual class to be inherited KeyListener {interface} No constructor (not a class at all) Class #36: + void keyPressed( KeyEvent e ) + void keyReleased( KeyEvent e ) Interfaces and Events All methods are + void keyTyped( KeyEvent e ) abstract and must all be implemented by the Software Design I (CS 120): D. Mathias programmer who wants to use them. 2 Software Design I (CS 120) 1 2 Coding Java Interfaces Coding Java Interfaces } Interface implementation is very simple } Interface implementation is very simple } A list of method signatures without any code (not even braces } Another example for an interface we’ve used many times in which to place code): (without knowing that’s what we were doing) access (public/private, etc.) access (public/private, etc.) 1. 1. return type return type 2. 2. method name method name 3. 3. parameter type(s) parameter type(s) 4. 4. public interface KeyListener KeyListener {interface} ActionListener {interface} { public interface ActionListener public void keyPressed( KeyEvent e ); { + void keyPressed( KeyEvent e ) public void keyReleased( KeyEvent e ); public void actionPerformed(ActionEvent e); public void keyTyped( KeyEvent e ); } + void keyReleased( KeyEvent e ) + void actionPerformed(ActionEvent e) } + void keyTyped( KeyEvent e ) 3 4 Software Design I (CS 120) Software Design I (CS 120) 3 4 1

  2. Classes to Handle Events Dealing with Java Events Events in Java are things like button clicks, mouse moves, keyboard inputs } } The basic Java event model Classes can respond to such things by handling these events } Creator involves 3 parts: Class implements ActionListener , Some class of object to 1. so it must contain actionPerformed () create events (button, method, to handle events keyboard, mouse, etc.) public class TwoButtons implements ActionListener { Listener Some class of object to private JButton left, right; 2. listen for the events public void actionPerformed( ActionEvent e ) { // what to do when actions occur Methods that handle the 3. } } different events. The class uses some JButton The class will wait (“listen”) for events Handler objects that are sources of the events to occur, and when they do, the (when they are clicked) method will run, responding to them 5 6 Software Design I (CS 120) Software Design I (CS 120) 5 6 The Basic Process JButton: Creates ActionEvents To respond to any event, the } } JButton class is one basic Java class for creating events process is basically the same: public class X implements ActionListener { Pick class X to implement 1. the type of listener you want javax.swing.JButton Once the events are // class constructor public X() Add the required methods created, an added 2. + JButton( String ) { for handling events to X ActionListener + int getX( ) addActionListener( this ); Find object of class Y that 3. + int getY( ) can respond to them can create events of that } + int getWidth( ) type, and add the listener public void actionPerformed( ActionEvent e ) + int getHeight( ) (i.e., X ) to it (if X == Y , then { + String getLabel( ) you can use this ) // code to handle event here ActionListener Note : for this last step, you + addActionListener( ActionListener ) } } need to look up what sort of + void repaint() is the interface that a addWhateverListener() + void setBounds( int, int , int, int) methods are part of the class Y class can implement + void setSize( int, int ) This example assumes X can + void setLocation( int, int ) } to do this } create ActionEvent objects + void setLabel( String ) all by itself 7 8 Software Design I (CS 120) Software Design I (CS 120) 7 8 2

  3. Another Source of ActionEvents: Timer Using a Timer for Animation } Another Java class that handles events is Timer } Once we have added a Timer object to a class, and then started it running, it will generate events over and over } Takes input int for its delay (milliseconds) } Also takes an ActionListener to respond to events } With the repetitive timer running, we can now make the class that contains it repeat the same method over and over } Generates new event each delay ms, over and over and over… } For example, suppose we add a method that moves an object on the screen in some direction by 5 pixels javax.swing.Timer } Now, when the timer event is handled, we can call this method to move the object + Timer(int, ActionListener) } Since this will happen again and again, the object will “glide” + boolean isRunning() across the screen in some direction + void start() + void stop() ... 9 10 Software Design I (CS 120) Software Design I (CS 120) 9 10 java.awt.event.ActionEvent ActionListener Methods Using ActionEvents «query» + Object getSource() } Imported from the java.awt.event.* library public interface ActionListener { } Usually, all we do is run getSource() in order to tell what object has public void actionPerformed( ActionEvent e ); actually produced the input event } public void actionPerformed( ActionEvent e ) } The ActionListener interface has one basic method, { which takes an ActionEvent as input if ( e.getSource() == oneThing ) { doSomething(); The event will come from some producer object (and can be 1. } produced in a number of different ways) else if ( e.getSource() == anotherThing ) { doSomethingElse(); The method runs if and only if the object that actually } 2. implements ActionListener and contains the above } method is listening to the producer Note : if there is only one possible source being listened to, we This means that we have run addActionListener() to make } can just run whatever code we want to happen every time sure that this will happen at the right time 11 12 Software Design I (CS 120) Software Design I (CS 120) 11 12 3

Recommend


More recommend