CS 134 Programming Exercise 7:

Similar documents
To gain experience using recursion and recursive data structures.

Programming Exercise. Scribbler

To gain experience using GUI components and listeners.

CS 134 Programming Exercise 3:

CS 134 Programming Exercise 9:

1. Write code to answer each of the following questions. For this question, it is not necessary for you to define constants or write comments.

CS 51 Laboratory # 12

To gain experience using arrays, manipulating image data, and using inheritance.

To gain experience using arrays, manipulating image data, and using inheritance.

Scope. Dragging Algorithm Revisited. Tuesday, October 28, October 28, 2008

CS 134 Programming Exercise 2:

CS 134 Midterm Fall 2012

Lecture 15 CSE11 Fall 2013 Arrays

Agenda. Container and Component

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

You Can Make a Difference! Due April 11/12 (Implementation plans due in class on 4/9)

Topic Notes: Java and Objectdraw Basics

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

CSE115 Lab 4 Fall 2016

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

CS 170 Java Programming 1. Week 10: Loops and Arrays

Part I: Learn Common Graphics Components

Programming Languages and Techniques (CIS120)

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

CS 170 Java Programming 1. Week 9: Learning about Loops

2IS45 Programming

Class 16: The Swing Event Model

Lecture 9. Lecture

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Java Programming Lecture 6

CoSc Lab # 6 (The Model) Due Date: Part I, Experiment classtime, Tuesday, Nov 6 th, 2018.

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

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

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Java Classes (Java: An Eventful Approach, Ch. 6),

CS 134 Test Program #2

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Classes. Integer Division. Thursday, October 23, March 13, 2008

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

CS 134 Programming Exercise 1:

More Swing. CS180 Recitation 12/(04,05)/08

Introduction. Introduction

Programming Assignment 7 (100. Points)

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

GUI Forms and Events, Part II

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

MIT AITI Swing Event Model Lecture 17

Programming Languages and Techniques (CIS120)

CISC 1600 Lecture 3.1 Introduction to Processing

Window Interfaces Using Swing Objects

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

CS 106 Winter Lab 05: User Interfaces

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Window Interfaces Using Swing Objects

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

CSC 101: Lab #5 Prelab Boolean Logic Practice Due Date: 5:00pm, day after lab session

CSE115 Lab 2 Fall 2016

Dr. Hikmat A. M. AbdelJaber

Fall 2013 Program/Homework Assignment #2 (100 points) -(Corrected Version)

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

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

Lab 3. A Multi-Message Reader

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points)

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

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 5, 2004

State Application Using MVC

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

1. Traffic Control CIT/CSE 230 PROGRAMMING PROJECT 4

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Computer Science 210: Data Structures. Intro to Java Graphics

Window Interfaces Using Swing. Chapter 12

Readings for This Lecture

CSC207 Week 4. Larry Zhang

AP CS Unit 11: Graphics and Events

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

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

} Each object in a Java program has an identifier (name) } This includes:

BINARY SEARCH TREES CS 200 RECITATION 7

Express Yourself. Writing Your Own Classes

Homework 6 part 2: Turtle Etch-A-Sketch (40pts)

CS Problem Solving and Object-Oriented Programming

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

CS 170 Java Programming 1. Week 12: Creating Your Own Types

Dragging. Dragging Rectangles. Tuesday, October 21, October 21, User interface effect that we want:

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Programming Assignment 3 ( 100 Points ) START EARLY!

Programming Language Concepts: Lecture 8

Lecture 5: Java Graphics

CS Exam 3 - Spring 2010

CS180 Recitation. More about Objects and Methods

CSE 113 A. Announcements - Lab

Programming Project 1

More about GUIs GEEN163

GRAPHICS PROGRAMMING. LAB #3 Starting a Simple Vector Animation

Transcription:

CS 134 Programming Exercise 7: Scribbler Objective: To gain more experience using recursion and recursive data structures. This week, you will be implementing a program we call Scribbler. You have seen a simple version of this program in class as a demo. In lab we would like you to extend the program from class to include the ability to move or color any of the scribbles that have been drawn rather than just the last one. With the first test program behind us, we return to the expectation that you will prepare a design for this program before you come to lab. You should come to lab with a design for steps 1 and 2 in the How to Proceed section. See the online version of this handout for instructions on obtaining a copy of the starter folder. In addition, in the online version, the picture of the program window shown above is replaced by a working version of the Scribbler. You may want to visit the web page after you read the handout so that you can experiment with the program to clarify any details that are not clear to you. The Scribbler program has three modes: Draw mode, Move mode, and Color mode. The program starts in Draw mode. Draw mode, Move mode, and Color mode are selected by pressing buttons, and the color used by Color mode is selected by choosing a color from a JComboBox filled with color names. The modes behave as follows: Draw mode: Drag to draw a new scribble on the canvas. Move mode: Reposition a scribble by pressing the mouse on it and dragging. 1

Color mode: Click on any scribble to change its color to that selected in the color menu. The program also has an Erase Last button that will erase the most recently drawn scribble. Pressing the button repeatedly will erase scribbles in the reverse order of which they were drawn. How to Proceed The starter for this lab is a working Scribbler, but it has only the Draw mode and a simplified Move mode that is capable of moving only the most recently drawn scribble. You will need to add code that will manage your scribbles to allow the various modes and the Erase last button to work correctly. There are a number of step-by-step approaches you could take to solve this problem, but it is important that you have a plan, and that you add functionality one step at a time. Here is one possible ordering of the tasks. We recommend that you develop and test your program incrementally make sure you have a working implementation at each step before moving on to the next. 1. Implement a simplified Color mode. This is similar to Move mode, except that you set the color of the scribble that contains the mouse point instead of moving it. To do this, you will have to add a setcolor method to the ScribbleInterface interface, and the NonEmptyScribble and EmptyScribble classes. Initially, you will only be able to set the color of the most recently drawn scribble. 2. Implement a simplified Erase mode. Here, you respond to the Erase last button s actionperformed event by deleting the most recently drawn scribble from the canvas. For now, you will only be able to erase the most recently drawn scribble. A second button press will do nothing. 3. Since you need to keep track of a number of scribbles, you will need to define classes representing a collection of scribbles. The interface for these classes will be called ScribbleCollectionInterface, and the classes implementing that interface will be named EmptyScribbleCollection and NonEmptyScribbleCollection. This interface and classes will be similar to the stacks used in Wood- Stack and ball lists in the ChainReaction. Just as we do not know how many line segments will make up a scribble when we start to draw one, we will not know how many scribbles will be drawn and stored in a scribble collection. In your Scribbler class you will need a variable with type ScribbleCollectionInterface that will be initialized with an object created from EmptyScribbleCollection. You will add new scribbles to it using the constructor from NonEmptyScribbleCollection as they are drawn. Consider carefully what methods your scribble collection needs to support the functionality of the four modes. We have provided the skeleton of a ScribbleCollectionInterface interface, and NonEmptyScribbleCollection and EmptyScribbleCollection classes. Draw mode needs to add a new scribble to the scribble collection after the dragging is done. The Erase last button needs to remove the most recently drawn scribble from the canvas and remove it from the scribble collection. This means there is now a new most recently drawn scribble, and a second press would remove that one, and so on. Be careful in the case when there are no scribbles left. Move mode and Color mode need to determine if a mouse click takes place on any of the scribbles on the canvas, not just the most recently drawn. This requires that your scribble collection classes be able to search through all of the scribbles until either a scribble is located that contains the click point, or it is determined that none of the existing scribbles contain the point. Once you have determined which scribble contains the click point, you can move or color that scribble as you did in the simplified versions of these modes. 2

Submitting Your Work The lab is due at 11 PM on Wednesday for those in the Monday lab, 11 PM Thursday for those in the Tuesday lab. When your work is complete you should deposit in the appropriate dropoff folder a folder creating by exporting your work from Eclipse. Make sure the folder name includes your name and the phrase Lab 7. Also make sure that your name is included in the comment at the top of each Java source file. Before turning in your work, be sure to double check both its logical organization and your style of presentation. Make your code as clear as possible and include appropriate comments describing major sections of code and declarations. Value Feature Design preparation (3 points total) 1.5 points Simplified color mode 1.5 points Erase mode Style (5 points total) 2 points Descriptive comments 1 point Good names 1 point Good use of constants 1 point Appropriate formatting Design (6 points total) 1 point Good use of boolean expressions, loops, conditionals 1 point Not doing more work than necessary 2 points Appropriate methods in NonEmptyScribbleCollection 2 points Appropriate recursive structure in NonEmptyScribbleCollection Correctness (6 points total) 1 point Drawing the GUI components correctly 1 point Switching among modes correctly 1 point Draw mode adds correctly to the scribble collection 1 point Move mode works correctly 1 point Color mode works correctly 1 point Erase button works correctly Sketch of classes provided for this program We are providing several classes and interfaces to help you get started. We include here printouts of the main program and the classes and interface for a single scribble. These are very similar to the Scribbler examples from lecture. Here is the code from the basic Scribbler class. You may need to add additional methods to it: // A very simple drawing program. public class Scribbler extends WindowController implements ActionListener { // user modes // using ints rather than boolean to allow for extension to // other modes private static final int DRAWING = 1; private static final int MOVING = 2; private static final int COLORING = 3; 3

// empty scribble as placeholder when nothing there. private static final EmptyScribble empty = new EmptyScribble(); // the current scribble private ScribbleInterface currentscribble; // the collection of scribbles private ScribbleCollectionInterface scribbles; // stores last point for drawing or dragging private Location lastpoint; // whether the most recent scribble has been selected for moving private boolean draggingscribble; // buttons that allow user to select modes private JButton setdraw, setmove, seterase, setcolor; // Choice JButton to select color private JComboBox choosecolor; // new color for scribble private Color newcolor; // label indicating current mode private JLabel modelabel; // the current mode -- drawing mode by default private int chosenaction = DRAWING; // create and hook up the user interface components public void begin() { setdraw = new JButton( Draw ); setmove = new JButton( Move ); setcolor = new JButton( Color ); JPanel buttonpanel = new JPanel(); buttonpanel.add(setdraw); buttonpanel.add(setmove); buttonpanel.add(setcolor); choosecolor = new JComboBox(); choosecolor.additem( red ); choosecolor.additem( blue ); choosecolor.additem( green ); choosecolor.additem( yellow ); seterase = new JButton( Erase last ); JPanel choicepanel = new JPanel(); choicepanel.add(seterase); 4

choicepanel.add(choosecolor); JPanel controlpanel = new JPanel(new GridLayout(3,1)); modelabel = new JLabel( Current mode: drawing ); controlpanel.add(modelabel); controlpanel.add(buttonpanel); controlpanel.add(choicepanel); getcontentpane().add(controlpanel, BorderLayout.SOUTH); // add listeners setdraw.addactionlistener(this); setmove.addactionlistener(this); seterase.addactionlistener(this); setcolor.addactionlistener(this); // make the current scribble empty currentscribble = empty; validate(); // if in drawing mode then start with empty scribble // if in moving mode then prepare to move public void onmousepress(location point) { if (chosenaction == DRAWING) { // start with an empty scribble for drawing currentscribble = empty; else if (chosenaction == MOVING) { // check if user clicked on current scribble draggingscribble = currentscribble.contains(point); // remember point of press for drawing or moving lastpoint = point; // if in drawing mode, add a new segment to scribble // if in moving mode then move it public void onmousedrag(location point) { if (chosenaction == DRAWING) { // add new line segment to current scribble Line newsegment = new Line(lastPoint, point, canvas); currentscribble = new NonEmptyScribble(newSegment, currentscribble); else if (chosenaction == MOVING) { if (draggingscribble) {// move current scribble currentscribble.move(point.getx() - lastpoint.getx(), point.gety() - lastpoint.gety()); 5

// update for next move or draw lastpoint = point; public void onmouserelease(location point) { // Add code when have collection of scribbles // Set mode according to JButton pressed. public void actionperformed(actionevent e) { if (e.getsource() == setdraw) { chosenaction = DRAWING; modelabel.settext( Current mode: drawing ); else if (e.getsource() == setmove) { chosenaction = MOVING; modelabel.settext( Current mode: moving ); Here is the starting code for ScribbleInterface: public interface ScribbleInterface { // returns whether point is contained in scribble public boolean contains(location point); // move scribble by dx in x-direction and dy in y-direction public void move(double dx, double dy); Here is the starting code for NonEmptyScribble: // A class to represent a non-empty scribble public class NonEmptyScribble implements ScribbleInterface { private Line first; // an edge line of the scribble private ScribbleInterface rest; // the rest of the scribble public NonEmptyScribble(Line segment, ScribbleInterface therest) { first = segment; rest = therest; // returns true iff the scribble contains the point public boolean contains(location point) { return (first.contains(point) rest.contains(point)); // the scribble is moved xoffset in the x direction 6

// and yoffset in the y direction public void move(double xoffset, double yoffset) { first.move(xoffset, yoffset); rest.move(xoffset, yoffset); Here is the starting code for EmptyScribble: /* Class representing an empty scribble */ public class EmptyScribble implements ScribbleInterface { public EmptyScribble() { // point is never in an empty scribble! public boolean contains(location point) { return false; // nothing to move, so do nothing! public void move(double dx, double dy) { The start up code for the collection interface and classes is omitted here. 7