



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Chapter 10—Event-driven Programs The Art and Science of An Introduction
C H A P T E R 1 0 I claim not to have controlled events, but confess events have controlled me. plainly that —Abraham Lincoln, letter to Albert Hodges, 1864
(adapted from slides by Eric Roberts)
import acm.program.; import acm.graphics.; import java.awt.event.; /* Draws a circle anywhere the user clicks the mouse / public class DrawSplatMap extends GraphicsProgram { public static final int RING_SIZE = 20; public void init() { this.addMouseListeners(); } public void mouseClicked(MouseEvent e) { GOval circle = new GOval(RING_SIZE,RING_SIZE); circle.setFilled(true); this.add(circle, e.getX(), e.getY()); } public static void main (String[] args) { new DrawSplatMap().start(); } }*
Drawing a line using this program requires three actions: pressing the mouse button at the starting point of the line, dragging the mouse to the desired end point, and then releasing the mouse. The mousePressed method responds to that event by creating a new zero-length line that begins and ends at the current mouse position. Dragging the mouse results in a series of mouseDragged calls that come in rapid succession each time the computer reads the mouse position. Each call simply resets the end point of the line. The effect of this strategy is that the user sees the line as it grows, providing the necessary visual feedback to position the line correctly. As you drag the mouse, the line will stretch, contract, and change direction as if the two points were connected by an elastic band. This technique is therefore called rubber-banding. public class DrawLines extends GraphicsProgram { / Initializes the program by enabling the mouse listeners / public addMouseListeners(); void init() { } / Called on mouse press to create a new line / public void mousePressed(MouseEvent e) { line = new GLine(e.getX(), e.getY(), e.getX(), e.getY()); }^ add(line); / Called on mouse drag to extend the endpoint / public void mouseDragged(MouseEvent e) { line.setEndPoint(e.getX(), e.getY()); } / Private instance variables / private GLine line; } In all likelihood, you have at some point used an application that allows you to draw lines with the mouse. In Java, that program takes less than a page of code. public class DrawLines extends GraphicsProgram { / Initializes the program by enabling the mouse listeners / public void init() { this.addMouseListeners(}; } / Called on mouse press to create a new line / public void mousePressed(MouseEvent e) { line = new GLine(e.getX(), e.getY(), e.getX(), e.getY()); }^ this.add(line); / Called on mouse drag to extend the endpoint / public void mouseDragged(MouseEvent e) { line.setEndPoint(e.getX(), e.getY()); } / Private instance variables / private GLine line; } main method and import statements not shown
DragObjects
page 1 of 2 skip code main method not shown import acm.graphics.; import acm.program.; import java.awt.; import java.awt.event.; /** This class displays a mouse-draggable rectangle and oval / public class DragObjects extends GraphicsProgram { / Initializes the program / public void init() { GRect rect = new GRect(100, 100, 150, 100); rect.setFilled(true); rect.setColor(Color.RED); add(rect); GOval oval = new GOval(300, 115, 100, 70); oval.setFilled(true); oval.setColor(Color.GREEN); add(oval); addMouseListeners(); } / Called on mouse press to record the coordinates of the click / public void mousePressed(MouseEvent e) { this.last = new GPoint(e.getPoint()); this.gobj = getElementAt(this.last); } / Called on mouse drag to reposition the object / public void mouseDragged(MouseEvent e) { if (this.gobj != null) { this.gobj.move(e.getX()-this.last.getX(), e.getY()-this.last.getY()); this.last = new GPoint(e.getPoint()); } } / Called on mouse click to move this object to the front / public void mouseClicked(MouseEvent e) { if (this.gobj != null) this.gobj.sendToFront(); } / Private instance variables / private GObject gobj; / The object being dragged / private GPoint last; / The last mouse position / }*
page 2 of 2 skip code
Using the Stoplight class you wrote for Lab 5, write a GraphicsProgram that creates a stoplight and three buttons labeled Red, Yellow, and Green, as shown in the sample run below. Clicking on a button should send a message to the stoplight to change its state accordingly. GStoplightGUI Red Yellow Green import acm.program.; // for GraphicsProgram import java.awt.event.; // for ActionListener import javax.swing.; // for JButtons public class TestStopLight extends GraphicsProgram { public static final int APPLICATION_WIDTH = 250; public static final int APPLICATION_HEIGHT = 475; public static final int RED = 1; public static final int YELLOW = 2; public static final int GREEN = 3; public static final int X = 20, Y = 20; // 3 JButton instance vars to be added along south control strip private JButton red,green,yellow; // instance variable...the StopLight private StopLight stopLight;*
main method not shown public void init() { this.stopLight = new StopLight(X,Y); this.stopLight.add(this); this.red this.add(this.red, = new JButton SOUTH);(" Red "); this.green = new JButton("Green "); this.add(this.green,SOUTH); this.yellow = new JButton("Yellow"); this.add(this.yellow,SOUTH); this.addActionListeners(); } // end run method public void actionPerformed(ActionEvent e) { if (e.getSource().equals(this.red)) { stopLight.setLight(RED); } else if (e.getSource().equals(this.yellow)) { stopLight.setLight(YELLOW); } else if (e.getSource().equals(this.green)) { }^ stopLight.setLight(GREEN); } //end actionPerformed method
import acm.program.; import java.awt.event.; import javax.swing.; /* This class displays a greeting whenever a name is entered / public class HelloGUI extends ConsoleProgram { public void init() { this.nameField = new JTextField(10); this.add(new JLabel("Name"), SOUTH); this.add(nameField, SOUTH); nameField.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == this.nameField) { this.println("Hello, " + this.nameField.getText()); this.nameField.setText(""); } } / Private instance variables / private JTextField nameField; }* main method not shown