import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import cs101.io.Console; public class DisplayClicks extends MouseAdapter { mouseClicked(MouseEvent e) { Console.out.println("Mouse clicked at (" + e.getX() + ", " + e.getY() + ")."); } }Bonus:
import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import cs101.io.Console; public class DisplayClicks extends MouseAdapter { private int lastX; private int lastY; private boolean firstClick = true; mouseClicked(MouseEvent e) { if (!firstClick) { Console.out.println("Last click at (" + this.lastX + ", " + this.lastY + ")."); firstClick = false; } Console.out.println("Mouse clicked at (" + e.getX() + ", " + e.getY() + ")."); lastX = e.getX(); lastY = e.getY(); } }Q: Now define a class that extends java.awt.Canvas and sends its mouse events to your MouseListener.
import java.awt.Canvas; public class ClickyCanvas extends Canvas { public ClickyCanvas() { this.addMouseListener(new DisplayClicks()); } }Q: Define a class that implements java.awt.event.WindowListener and extends the windowClosing() and extends the windowClosing() method by printing "Nah, nah, you can't kill me!" (Alternatly, you can do the potentially more useful thing and (1) call the object's dispose() method and (2) call System.exit(0).) What class do you think would be useful when implementing WindowListener?
import java.awt.event.WindowAdapter; import cs101.io.Console; public class IndestructibleWindowListener extends WindowAdapter { public windowClosing(WindowEvent e) { Console.println("Nah, nah, you can't kill me!"); } }Bonus:
import java.awt.event.WindowAdapter; import cs101.io.Console; public class DestructibleWindowListener extends WindowAdapter { public windowClosing(WindowEvent e) { e.getWindow().dispose(); System.exit(0); } }Q: What methods does a Graphics have that allow you to make things appear on it? (One such method is drawOval).
import java.awt.Graphics; import java.awt.Canvas; public class JapaneseFlag extends Canvas { public void paint(Graphics g) { g.fillOval(75, 75, 50, 50); } }Bonus
import java.awt.Graphics; import java.awt.Canvas; import java.awt.Color; public class JapaneseFlag extends Canvas { public paint(Graphics g) { g.setColor(Color.red); g.fillOval(75, 75, 50, 50); } }Extra Bonus
import java.awt.Graphics; import java.awt.Canvas; import java.awt.Color; public class JapaneseFlag extends Canvas implements MouseListener { private Color currentColor = Color.black; public paint(Graphics g) { g.setColor(currentColor); g.fillOval(75, 75, 50, 50); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) { this.currentColor = Color.red; this.repaint(); } public void mouseExited(MouseEvent e) { this.currentColor = Color.black; this.repaint(); } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} }
This course is a part of Lynn
Andrea Stein's Rethinking
CS101 project at the MIT
AI Lab and the Department
of Electrical Engineering and Computer Science at the
Massachusetts Institute of
Technology.
Questions or comments: