Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC305 Summer Lecture 06

CSC305 Summer Lecture 06

Individual Software Design and Development
Listeners
(202508)

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

August 07, 2025
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info o ffi ce: 14 -227

    CSC 305 Individual Software Design and Development | Summer Version Lecture 06. Listeners
  2. Observable import java.util.LinkedList; public class Observable { private LinkedList<Observer> observers

    = new LinkedList<Observer>(); public void addObserver(Observer observer) { observers.add(observer); } public void removeObserver(Observer observer) { observers.remove(observer); } public void notifying() { for (Observer ob : observers) { ob.update(this); } } } 8
  3. Student as Observer class Student implements Observer { public String

    answerQuestion (String question) { String answer; // solve the question return answer; } @Override public void update(Observable s) { String q = ((Teacher)s).getQuestion(); Sting answer = answerQuestion (q); System.out.println (answer); } } 9
  4. Teacher as Observable class Teacher extends Observable { private String

    question; public String getQuestion() { return question; } public void createQuestion () { // your code here notifying(); } } 10
  5. Main public class Main { public static void main(String[]a) {

    Teacher teacher = new Teacher(); Student s1 = new Student(); Student s2 = new Student(); teacher.addObserver(s1); teacher.addObserver(s2); // Teaching teacher.createQuestion(); // More Teaching teacher.createQuestion(); } } 11
  6. 14

  7. 17

  8. 19

  9. 20

  10. 22

  11. MouseListener • public void mousePressed (MouseEvent e) {} • public

    void mouseClicked(MouseEvent e) { } • public void mouseRele a sed(MouseEvent e) { } • public void mouseEntered(MouseEvent e) { } • public void mouseExited(MouseEvent e) { } • public void mouseDr a gged(MouseEvent me) { } • public void mouseMoved(MouseEvent e) { } 23
  12. MouseMotionListener • public void mouseDr a gged(MouseEvent me) { }

    • public void mouseMoved(MouseEvent e) { } 24
  13. 26

  14. Homework Reuse (Complete as needed) your Code from Assignment 1

    Add a GUI + Listeners Class Diagram (Use Astah Tool)
  15. TicTaeToe 38 O O X Dr a w the Squ

    a res ( a nd the O a nd X), i.e., Use f illRect, dr a wOv a l, dr a wString, etc. methods
  16. Lab. What about a new version for TicTaeToe 1. Cre

    a te a Jfr a me a nd a Jp a nel 3. Dr a w the 9 boxes to represent the TicT a cToe 5. Add MouseListeners to detect clicks on the P a nel 7. Review if the click is inside of a box 9. If so, dr a w a nd X or a 0 on th a t position (one time X, the next O, the next X, etc) Do not worry a bout the g a me (Tic-T a c-Toe) logic, yet. The go a l of this l a b is just a GUI with listeners 39
  17. CSC 305 Individual Software Design and Development Javier Gonzalez-Sanchez, Ph.D.

    [email protected] Summer 2025 Copyright. These slides can only be used as study material for the class CSC305 at Cal Poly. They cannot be distributed or used for another purpose.