18a-section-2-solutions

18a-section-2-solutions - Handout#18A CS 106A July 9 2009...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: Handout #18A CS 106A July 9, 2009 Solution to Section #2 1. Rubber-banding /* * File: RubberBanding.java * ------------------------ * This program allows users to create lines on the graphics * canvas by clicking and dragging with the mouse. The line * is redrawn from the original point to the new endpoint, which * makes it look as if it is connected with a rubber band. */ import acm.graphics.*; import acm.program.*; import java.awt.event.*; /* This class allows users to drag lines on the canvas */ public class RubberBanding extends GraphicsProgram { private GLine line; public void run() { addMouseListeners(); } /* Called on mouse press to create a new line */ public void mousePressed(MouseEvent e) { double x = e.getX(); double y = e.getY(); line = new GLine(x, y, x, y); add(line); } /* Called on mouse drag to reset the endpoint */ public void mouseDragged(MouseEvent e) { double x = e.getX(); double y = e.getY(); line.setEndPoint(x, y); } }...
View Full Document

This note was uploaded on 10/14/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Page1 / 2

18a-section-2-solutions - Handout#18A CS 106A July 9 2009...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online