18-section-2

18-section-2 - string, this technique is called...

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

View Full Document Right Arrow Icon
Handout #18 CS 106A July 7, 2009 Section Handout #2 Random Numbers and Simple Graphics 1. Drawing lines Write a GraphicsProgram that allows the user to draw lines on the canvas. Pressing the mouse button sets the starting point for the line. Dragging the mouse moves the other endpoint around as the drag proceeds. Releasing the mouse fixes the line in its current position and gets ready to start a new line. For example, suppose that you press the mouse button somewhere on the screen and then drag it rightward an inch, holding the button down. What you’d like to see is the following picture: DrawLines If you then move the mouse downward without releasing the button, the displayed line will track the mouse, so that you might see the following picture: DrawLines Because the original point and the mouse position appear to be joined by some elastic
Background image of page 1

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

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

Unformatted text preview: string, this technique is called rubber-banding. Although this program may seem quite powerful (and indeed it allows you to draw any figure on the screen that can be drawn with lines), it is also simple to implement. The entire program requires fewer than 20 lines of code. 2. Random circles Write a GraphicsProgram that draws a set of ten circles with different sizes, positions, and colors. Each circle should have a randomly chosen color, a randomly chosen radius between 5 and 50 pixels, and a randomly chosen position on the canvas, subject to the condition that the entire circle must fit inside the canvas without extending past the edge. The following sample run shows one possible outcome: 2 Checkerboard On some runs of this program you might not see ten circles. Why?...
View Full Document

Page1 / 2

18-section-2 - string, this technique is called...

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