Computer Science 15-100, Fall 2008
Class Notes:  Getting Started with Events


  1. timerFired Events
    1. Beeping (using JComponentWithEvents)
    2. Animation (with an instance variable)
    3. Smoother Animation (with setTimerDelay in start method)
  2. mousePressed Events
  3. keyPressed Events
  4. Practice
  5. Coming soon...

Getting Started with Events

  1. timerFired Events
     
    1. Beeping (using JComponentWithEvents)
      // BeepingDemo.java
      // Demonstrates the timerFired event in JComponentWithEvents.
      // To compile and run this Java file, you must:
      // 1. Download current JComponentsWithEvents.class file from here:
      //       http://kosbie.net/cmu/fall-08/15-100/handouts/events/
      //    and place it in the same directory as your Java program.
      // 2. Include a main method that calls the "launch" method
      //    defined in JComponentWithEvents (see below).
      import javax.swing.*;
      import java.awt.*;
      class BeepingDemo extends JComponentWithEvents {
      
        // This method is called at regular intervals by the timer.
        public void timerFired()  {
          beep();
        }
        public void paint(Graphics2D page) {
          page.fillRect(50, 50, 50, 50);
        }
        // Standard main method:
        public static void main(String[] args) { launch(500, 400); }
      }
    2. Animation (with an instance variable)
      // AnimationDemo.java
      // Demonstrates the timerFired event in JComponentWithEvents,
      // and using an instance variable to retain data between method calls
      // (to the event handlers and the paint method).
      import javax.swing.*;
      import java.awt.*;
      class AnimationDemo extends JComponentWithEvents {
        // this instance variable persists between method calls!
        private int rectX = 50;
        public void timerFired()  {
          rectX += 5;
        }
        public void paint(Graphics2D page) {
          page.fillRect(rectX, 50, 50, 50);
        }
        // Standard main method:
        public static void main(String[] args) { launch(500, 400); }
      }
    3. Smoother Animation (with setTimerDelay in start method)
        // Add this method to your Animation program
        // This method is called exactly once at the start of the program.
        public void start() {
          int delayInMilliseconds = 1;
          setTimerDelay(delayInMilliseconds);
        }
  2. mousePressed Events
    // MousePressedDemo.java
    // Demonstrates the mousePressed event in JComponentWithEvents.
    import javax.swing.*;
    import java.awt.*;
    class MousePressedDemo extends JComponentWithEvents {
      // these instance variables persists between method calls!
      private int rectX = 50;
      private int rectY = 50;
      // This method is called each time the mouse is pressed.
      // The parameters contain the (x,y) location of the mouse press.
      public void mousePressed(int x, int y) {
        rectX = x;
        rectY = y;
      }
      public void paint(Graphics2D page) {
        page.fillRect(rectX, rectY, 50, 50);
      }
      // Standard main method:
      public static void main(String[] args) { launch(500, 400); }
    }
  3. keyPressed Events
    // KeyPressedDemo.java
    // Demonstrates the mousePressed event in JComponentWithEvents.
    import javax.swing.*;
    import java.awt.*;
    class KeyPressedDemo extends JComponentWithEvents {
      // these instance variables persists between method calls!
      private int rectX = 50;
      private int rectY = 50;
      // This method is called each time a key is pressed.
      // The parameter contains the key that was pressed.
      // Note that you can use the constants UP, DOWN, LEFT, and RIGHT
      // to represent the arrow keys.
      public void keyPressed(char key) {
        if (key == RIGHT)
          rectX += 10;
        else if (key == 'l')
          // We'll take 'l' to mean "left" edge of the window
          rectX = 0;
      }
      public void paint(Graphics2D page) {
        page.fillRect(rectX, rectY, 50, 50);
      }
      // Standard main method:
      public static void main(String[] args) { launch(500, 400); }
    }
  4. Practice
    See EventsPractice.html
     
  5. Coming soon...
    EventsDemo2.html

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem