20 - Last Day: A Simple Board CS133: Developing Programming...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Last Day: A Simple Board CS133: Developing Programming Principles Lecture 20 Mouse Events, Board (2) • No text, no line. • Only yellow pegs Today: Mouse clicks CS 133 Course Notes Lecture 20, Slide 2 1 2 Event-Driven Programming • Programming so far: programmer decides what happens next (“flow of control”) • GUIs: user decides what happens next – load a file, click a peg (which one?), draw a rectangle, quit the game... Listening for Mouse Events In order to receive mouse events we must declare the BoardPanel class as implementing the MouseListener interface: public class BoardPanel extends JPanel implements MouseListener { … • Idea: GUI generates events. The programmer writes code to react to the events. CS 133 Course Notes Lecture 20, Slide 3 CS 133 Course Notes Lecture 20, Slide 4 3 4 Interfaces In Java, an interface defines a set of methods that a class must implement. Just like a class, an interface is a type. (In the remainder of the course, we’ll see several interfaces, but we won’t study them in detail.) CS 133 Course Notes Lecture 20, Slide 5 Listening for Mouse Events import javax.swing.*; import java.awt.*; import java.awt.event.*; public class BoardPanel extends JPanel implements MouseListener { // etc. } CS 133 Course Notes Lecture 20, Slide 6 5 6 MouseListener methods public public public public public void void void void void mouseClicked(MouseEvent e) mouseEntered(MouseEvent e) mouseExited(MouseEvent e) mousePressed(MouseEvent e) mouseReleased(MouseEvent e) mouseClicked public void mouseClicked(MouseEvent e) { int x = (int)e.getPoint().getX(); int y = (int)e.getPoint().getY(); boardClicked(x, y); repaint(); } CS 133 Course Notes Lecture 20, Slide 7 CS 133 Course Notes Lecture 20, Slide 8 7 8 Class MouseEvent When a mouse action occurs in a GUI component, a MouseEvent is generated by Java and delivered to the MouseListener for that component. public Point getPoint() Returns the x, y position of the event relative to the source component. CS 133 Course Notes Lecture 20, Slide 9 repaint When repaint is called it makes a request to Java that eventually results in a call to paintComponent. public void repaint() CS 133 Course Notes Lecture 20, Slide 10 9 10 Remaining MouseListener Methods public public public public void void void void mouseEntered(MouseEvent e) { } mouseExited(MouseEvent e) { } mousePressed(MouseEvent e) { } mouseReleased(MouseEvent e) { } boardClicked private void boardClicked(int x, int y) { int row, col; if (x < X_OFFSET || y < Y_OFFSET) { return; } row = (y - Y_OFFSET)/SIZE; col = (x - X_OFFSET)/SIZE; flipPeg(row, col); } CS 133 Course Notes Lecture 20, Slide 12 CS 133 Course Notes Lecture 20, Slide 11 11 12 (x, y) to (row, col) (X_OFFSET, Y_OFFSET) Flipping Pegs On and Off public void flipPeg(int row, int col) { if (row >= 0 && row < rows && cols >= 0 && col < cols) { grid[row][col] ^= true; } } (x, y) SIZE CS 133 Course Notes Lecture 20, Slide 13 CS 133 Course Notes Lecture 20, Slide 14 13 14 Listening for Mouse Events Finally, in the constructor we must tell Java that the BoardPanel class will be responsible for handling its own mouse events. addMouseListener(this); } CS 133 Course Notes Lecture 20, Slide 15 CS 133 Revised Constructor public BoardPanel(int rows, int cols) { int xSize = 2*X_OFFSET + cols*SIZE; int ySize = 2*Y_OFFSET + rows*SIZE; this.rows = rows; this.cols = cols; emptyGrid(); setPreferredSize( new Dimension(xSize, ySize)); addMouseListener(this); Course Notes Lecture 20, Slide 16 15 16 Listening for Mouse Events 1. Class must implements MouseListener Additional Features of Board • • Window characteristics (title, resizing) Implementing displayMessage Implementing getClick 2. Define all MouseListener methods • 3. addMouseListener(this) CS 133 Course Notes Lecture 20, Slide 17 CS 133 Course Notes Lecture 20, Slide 18 17 18 Window Characteristics Setting the title of a JFrame: this.setTitle("Board game"); Preventing a resize of the JFrame: this.setResizable(false); Revised Constructor public Board(int rows, int cols) { Container content = getContentPane(); board = new BoardPanel(rows, cols); content.add(board); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); this.setTitle("Board game"); this.setResizable(false); } CS 133 Course Notes Lecture 20, Slide 19 CS 133 Course Notes Lecture 20, Slide 20 19 20 Displaying Text void drawString(String s, int x, int y) Displaying Text public class TextExample extends JPanel { public TextExample() { setPreferredSize(new Dimension(100, 100)); } public void paintComponent(Graphics canvas) { canvas.setColor(Color.BLACK); canvas.drawString("Hello world!", 20, 50); } } Draws the text given by the specified String, using a graphics context's current font and color. CS 133 Course Notes Lecture 20, Slide 21 CS 133 Course Notes Lecture 20, Slide 22 21 22 Displaying Text public static void main(String args) { JFrame frame = new JFrame(); JPanel eg = new TextExample(); Container content = frame.getContentPane(); content.add(eg); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } CS 133 Course Notes Lecture 20, Slide 23 Displaying Text CS 133 Course Notes Lecture 20, Slide 24 23 24 Implementing getClick getClick is tricky to implement. The calling thread of execution must explicitly wait for the mouse event. (NOT CS133!) Checkers CS 133 Course Notes Lecture 20, Slide 25 CS 133 Course Notes Lecture 20, Slide 26 25 26 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

Ask a homework question - tutors are online