CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

Similar documents
CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

1 Getting started with Processing

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

Lab 10: Inheritance (I)

Class #1. introduction, functions, variables, conditionals

CISC 1600, Lab 2.2: Interactivity in Processing

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

CISC 1600, Lab 3.2: Interactivity in Processing

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

AP CS Unit 12: Drawing and Mouse Events

CS 201 Advanced Object-Oriented Programming Lab 5 - Sudoku, Part 1 Due: March 3/4, 11:30 PM

CISC 1600, Lab 3.1: Processing

Basic Computer Programming (Processing)

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

We will start our journey into Processing with creating static images using commands available in Processing:

Exploring Processing

Event Driven Programming

CS Problem Solving and Object-Oriented Programming

Ancient Cell Phone Tracing an Object and Drawing with Layers

CISC 1600, Lab 2.1: Processing

Graphics and Painting

Solution Notes. COMP 151: Terms Test

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Mathematics 350 Section 6.3 Introduction to Fractals

2IS45 Programming

CS 134 Programming Exercise 3:

CPSC Fall L01 Final Exam

Topic Notes: Java and Objectdraw Basics

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

1 Getting started with Processing

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Paint Tutorial (Project #14a)

Basking in the. by Micah Martin 8th Light, Inc. Copyright 2009 Micah Martin

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

CISC 1600 Lecture 3.1 Introduction to Processing

CS 106A, Lecture 14 Events and Instance Variables

Working with images and scenes

ICOM 4015 Advanced Programming Laboratory. Chapter 3 Introduction to Graphical Applications in Java using Swing

2. (True/False) All methods in an interface must be declared public.

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

HOW TO. In this section, you will find. miscellaneous handouts that explain. HOW TO do various things.

CS Programming Exercise:

The AWT Event Model 9

CS 201 Advanced Object-Oriented Programming Lab 4 - Asteroids, Part 2 Due: February 24/25, 11:30 PM

[ the academy_of_code] Senior Beginners

To specify the dimensions of the drawing canvas use the size statement: ! size( 300, 400 );

Watch the following for more announcements

CS 051 Homework Laboratory #2

Module 05 User Interfaces. CS 106 Winter 2018

CS 106A, Lecture 14 Events and Instance Variables

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

First Visual Basic Lab Paycheck-V1.0

CIS 110 Introduction to Computer Programming Summer 2014 Final. Name:

CS 106 Winter Lab 05: User Interfaces

Generating Vectors Overview

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Which of the following syntax used to attach an input stream to console?

On the Web sun.com/aboutsun/comm_invest STAROFFICE 8 DRAW

Kimberly Nguyen Professor Oliehoek Introduction to Programming 8 September 2013

Old 257 Exam #2s for Practice

Instructions for Crossword Assignment CS130

AP Computer Science Unit 13. Still More Graphics and Animation.

How to...create a Video VBOX Gauge in Inkscape. So you want to create your own gauge? How about a transparent background for those text elements?

Chapter 7 Applets. Answers

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

Perry Bible Fellowship [pbfcomics.com/94/]

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Unit 7: Event driven programming

Creating a Title Block & Border Using Chief Architect. Architectural Design & Residential Construction Penncrest High School

PIC 20A GUI with swing

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Programming: You will have 6 files all need to be located in the dir. named PA4:

Lesson 6 Adding Graphics

Learning to use the drawing tools

Word 2007: Flowcharts Learning guide

Week 5 Creating a Calendar. About Tables. Making a Calendar From a Table Template. Week 5 Word 2010

2D Drawing Primitives

CS Multimedia and Communications. Lab 06: Webpage Tables and Image Links (Website Design part 3 of 3)

Lecture 5: Java Graphics

The Menger Sponge in Google SketchUp

Programmierpraktikum

Introduction to Processing

HYPERSTUDIO TOOLS. THE GRAPHIC TOOL Use this tool to select graphics to edit. SPRAY PAINT CAN Scatter lots of tiny dots with this tool.

Adobe illustrator Introduction

EEN118 LAB TWO. 1. A Five-Pointed Star.

ADOBE TRAINING CS6 PHOTOSHOP BASICS: EDITING PHOTOS & WORKING WITH TEXT - 1

JASCO CANVAS PROGRAM OPERATION MANUAL

CSE115 Lab 4 Fall 2016

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

EXAMINATIONS 2017 TRIMESTER 2

Assignment 3 Functions, Graphics, and Decomposition

Luggage Tag. 1. Open Creative DRAWings. 2. Left click on on the new design menu.

Programming graphics

Corel Draw 11. What is Vector Graphics?

Let s Make a Front Panel using FrontCAD

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

CS 134 Programming Exercise 7:

CS 134 Programming Exercise 2:

Transcription:

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM Introduction to the Assignment In this assignment, you will get practice with recursion. There are three parts to the lab. The first two parts use recursive methods to draw interesting pictures. The last part use a recursive structure. Part 1: Recursive Tunnel In this part, you will write a program that draws the following image: The starting font is Verdana, bold, 60 points. The starting rectangle size is 600 pixels wide and 400 pixels tall. On each level of recursion, the width and height of the rectangle and the font size should all be 20% smaller. The recursion should stop when the height of the rectangle is 20 pixels or less. Step 1: Create the window with the black border. 1

Step 2: Draw the outermost layer Define a method with the following signature: private void paintonce(int left, int top, int width, int height, int fontsize, Graphics g) This method should draw the white outline for the rectangle and the word RECURSION centered at the bottom of the rectangle. To draw this image, you will need to define the paintcomponent method and use the draw- Rect and drawstring methods, like we did in the IdCard lab at the beginning of the semester. To center the string, you will need to determine its size at the given font size. You will need to call the setfont and getfontmetrics methods in the Graphics class, and then use the getstringbounds method in the FontMetrics class. Call your paintonce method from paintcomponent. At this point, your window should look like this: Step 3: Recursion Define a method with the following signature: private void paintrecursively(int left, int top, int width, int height, int fontsize, Graphics g) Remember the basic principles of recursion: Define a base case. When should the recursion stop? How do you define a smaller subproblem? Change paintcomponent to call paintrecursively instead of paintonce. Magic! 2

Part 2: Sierpinski s Gasket In this part, you will write a program that does another recursive drawing. Specifically, you will draw a triangle with smaller and smaller triangles inside of it, making a shape known as Sierpinski's Gasket (http://en.wikipedia.org/wiki/sierpinski_triangle). Here are some snapshots of Sierpinski s Gasket at various levels of development: This is the final shape that we want to draw. We start by drawing a single large triangle. Next, we find the midpoint of each line of the original triangle. Using these points, we draw the 3 upright triangles inside the big triangle. Note that the upside down triangle is not drawn explicitly. 3

Next, repeat the process with each of the triangles drawn in the previous stage. Continue until the length of an edge of the triangle reaches some minimum value. Doing the Assignment Step 1: Create a window with a white area to draw on Step 2: Draw a single large triangle Now, write a method called drawtriangles. Eventually, this will become your recursive method, but for now just place code in it to draw a single triangle. You should pass it 3 Point2D objects that represent the corners of the triangle as well as the Graphics object that you will draw on. Point2D is an abstract class in the Java libraries that encapsulates an x, y pair. We will use the Point2D.Double class that extends this abstract class: public Point2D.Double (double x, double y) The triangles look best if they are equilateral (all edges have the same length), but that is not really necessary, so just pick three starting points so that the bottom line is horizontal. You will need to make method calls that are defined in the Point2D abstract class as well as methods defined in the Graphics class to draw the triangles. Please refer to the online Java API to find methods to use. At some point, you will likely discover that you need to convert from a double to an int. Remember that the way to do that is with casting: int myint = (int) mydouble; This causes Java to truncate the double to turn it into an int. 4

Call your drawtriangles method from paintcomponent and you should see a single large triangle. Hint: Your code will be easier to understand if you pick your variable names carefully. You will also find it helpful to write private methods to do some of the work to avoid repeating code within your drawtriangles methods. Step 3: Draw a triangle that recurses in one direction only Rather than just plunge into drawing the entire gasket, take it slowly and draw just one of the interior triangles when you recurse. For example, if you just recurse in the lower left direction, you will end up with something looking like the figure at the right. Remember the basic principles of recursion: Define a base case. When should the recursion stop? How do you define a smaller subproblem? You need to call drawtriangles recursively, so you will need to determine where the corners of the interior triangles go. Notice that when we draw a triangle in the bottom left, its bottom left corner is the same and the other two corners are midpoints of the left and bottom edges of the larger triangle. The midpoint can be found by averaging the x value of the endpoints and the y value of the endpoints. Step 4: Draw the complete gasket Once you are successfully drawing the figure shown on this page, add more recursive calls to repeatedly draw triangles in each of the top and bottom right of the outer triangle. Part 3: Grab a Tiger by the Tail In this part, you will write several simple classes that allow the user to draw a tiger consisting of a head and a tail consisting of a number of tail parts. Then, if the user depresses the mouse on the end of the tail and drags, the tail will move around like a snake, pulling the head. Please look at the movie online at http://www.mtholyoke.edu/~blerner/cs201/labs/lab10/tiger.mov for clarification. On the first mouse press, when the user drags the mouse, the tiger s head is drawn followed by a series of small circles. On subsequent mouse presses, if the user clicks in the last circle, the tail moves such that the last circle follows the mouse and all previous circles follow the circle that succeeds them. The head moves to be centered on the location of the first circle in the tail. Doing the Assignment Step 1: Create the head You will need a simple user interface for this application. You will need to create a class that extends JComponent and has a MouseListener and MouseMotionListener attached to it. MouseListener s methods are called when the user presses or releases the mouse button: public void mousepressed (MouseEvent event) public void mousereleased (MouseEvent event) 5

MouseMotionListener s methods are called when the user drags the mouse (that is, moves the mouse while the mouse button is depressed). public void mousedragged (MouseEvent event) These interfaces have additional methods that you will need to declare, but you can leave their bodies empty since you do not want to do anything in those cases: public void mouseclicked (MouseEvent event) public void mouseentered (MouseEvent event) public void mouseexited (MouseEvent event) public void mousemoved (MouseEvent event) The tiger is a recursive structure consisting of a head and circles that form a tail. As with Scribbler, you will need an interface, called TigerPart, and two implementing classes, called TigerHead and TigerTailPart. When the user first presses the mouse, create a TigerHead centered where the user presses down the mouse button. You can read the tiger head image from the file by calling: ImageIcon tigerheadimage = new ImageIcon ("tigersmall.gif"); To display it, use the painticon method defined in ImageIcon. (Refer to the online Java API for details.) Step 2: Create a tail As the user drags the mouse, you should create TigerTailPart objects. Each TigerTailPart has an associated shape, which is an Ellipse2D. The tail part should be created with its x and y values being where the mouse is when mousedragged is called. This program differs from the Scribbler example in that your program only needs to draw a single chain. After the user releases the mouse button, the program should enter moving mode and interpret the mouse presses and drags as attempts to move the existing tiger, rather than drawing additional tigers. Step 3: Moving the end of the tail The first step in moving the tiger is determining when the user has clicked on the end of the tail. Now that you can determine which link is the end of the tail, you can write a method to move the end of the tail. Give this method 2 parameters to indicate the distance it should move in the x & y directions. Initially, just show that you can drag the end of the tail around, leaving the other parts of the tiger stationary. Step 4: Moving the tiger Modify the method that you wrote in step 3 so that it recursively moves the rest of the tiger. Think about what parameters this method will need in order to move tiger parts to the right places. Magic! You should be able to drag your tiger around the screen! 6

Grading 5 Step 1: Create the window with black background 5 Step 2: Draw the outermost layer 10 Step 3: Recursion 5 Step 1: Create a window with a white area to draw on 5 Step 2: Draw a single large triangle 10 Step 3: Draw a triangle that recurses in one direction only 5 Step 4: Draw the complete gasket 5 Step 1: Create the head 5 Step 2: Clicking a tail 5 Step 3: Moving the end of the tail 5 Step 4: Moving the tiger 15 Comments 20 Style Turning in Your Work Submit your source code as three separate jar files on Ella. 7