Unformatted text preview: Programming Assignment 4 Due: 11:59pm Thursday, October 21 Overview For this assignment, you will create an applet that displays resizing co lored balls. The canvas will be divided into four ant regions and the color of each ball will be determined by which quadra the center of the ball l in. The colors for each lies of the quadrants will be the four printer colors: Cyan (top left), Mage nta (top right), Yellow (bottom left) and Key/Black (bottom right). If you mouse press on either of the horizontal or vert ical lines dividing the four quadrants, you can drag the line to a new position. If this repositioning changes which quadra nt a ball is in, the ball will change to the appropriate color. You should also be able to resize the applet and in doing so, ch ange the position of the horizontal and vertical lines to keep them in the same relative/proportional positions in the resized applet canvas area. You should have two classes to implement this: ResizableBallControl ler and ResizableBall. The controller class will handle all of the mouse events and repositioning of the horizontal an d vertical lines. The ball class will be responsible for the color and resizing animation of the balls. Strategy and Specifications Start early! This is a complex assignment that will take some time to implement. Divide the program up into different stages to make it a little easier: Stage 1: Creating Quadrants Begin with the controller class (ResizableBallController) by creating tw o Line objects to divide the canvas into four quadrants of equal size (a horizontal line and a vertical line). The end points of the lines should be based on the size (width and height) of the canvas. Stage 2: Manipulating Quadrants The first manipulation you should implement is dragging the Lines. Yo u can use the onMousePress() method to determine whether one or both of the Lines have been grabbed or no t. You can set boolean flags in this method to indicate which line(s) have been selected. Note that if you grab the in tersection of the two Lines, you should be able to drag both lines simultaneously. Refer to the objectdraw documentati on (linked from the Useful Links page) for other mouse event methods that may be useful. You will need to update th e position of the Lines as they are dragged. You should include logic to make sure that the Lines are not dragged off t he visible canvas/applet area. So do not let either line go beyond 5 pixels from the edge of the canvas in any direction ( a 5 pixel margin). The second manipulation you should implement is resizing the screen . If you resize the window, the Lines should move to preserve their current proportions on the screen. To redraw the ca nvas, override the paint() method: public void paint( java.awt.Graphics g ) You should first make a call to the superclass’s version of the method by adding the line: super.paint( g ); You can then add in your code for repositioning the Lines based on the proportions of where they were previously. Make sure that you test this in appletviewer and not in a browser. When using a browser, you cannot resize the canvas itself. Stage 3: Creating the Balls You should create a new class ResizableBall, which should be an Activ eObject, to create each of the balls. The controller class should not have any references to any of the balls created. Inste ad the controller should simply create a new ResizableBall every time the mouse is clicked in the canvas/applet. Th e constructor should look something like this: public ResizableBall(double xLoc, double yLoc, double size, Drawi ingCanvas ca nvas, Line hLine, Line vLine) The first two parameters are the x and y coordinates of the center of the ball (where the mouse was clicked). The third parameter is the starting size (diameter) of the ball (50 pixels is a goo d starting size). The last two parameters are the horizontal and vertical Lines used to divide the canvas into different q uadrants. For now, the constructor should just create and display the ball. Remember: the Oval object shapes use the upper‐left corner of an invisible bounding box as the x and y location to draw their shapes. So translate accordingly. Stage 4: Resizing the Balls You should add a run() method to your ResizableBall class to deal wit h the resizing animation of the balls. The diameter of the ball should grow by 2 pixels each step to make it a smooth tran sition. You should pause for 50 milliseconds in each iteration so the balls won’t grow and shrink too quickly. If the ball gro ws to twice its starting size, it should start to shrink. Once the ball reaches half its starting size, it should start to gr ow. Because the ResizableBall is an ActiveObject and therefore a Thread, it can run on its own, independent of all other objects. The run() method contains all of the code that will run when the thread is started. You start the thread by calling the start() method when the ball is created at the end of the construc tor. (See Ch 9 and the lecture notes on Active Objects. We went over all of this in class.) Stage 5: Adding Color When you click in a quadrant, the ball that is created (centered at the point of the mouse click) will be a particular color based on the quadrant: upper‐left – Cyan; upper‐right – Magenta; low er‐left – Yellow; lower‐right – Black (the printer colors). You should set the color of each ball based on which quadran t the center of the ball is located in at any particular time. Use the two Lines passed in to the constructor to det ermine which quadrant the ball is in. When the lines move they redefine the quadrant areas and the balls need to po ssibly change color. The lines also move when resizing the applet. It may be more efficient to only perform this quad rant/color check when a line actually moves. Style See the pa2 write‐up for the style guidelines. README You need to provide a README file with this assignment. Make sure t hat the filename is "README". Note that this is in n all caps and has no extension. In your README, you should A) Provide a high level description of what your program does and ho w you can interact with it. Make this explanation such that your grandmother or uncle or someone you know who has no programming experience can understand what this program does and how to use it. B) Briefly describe a cool feature that could be added to this assignme ent. C) Answer the following questions: 1. What is the Unix command to delete all the class files from th e current directory? 2. What is the command to cut a full line of text to the clipboard in vim? How do you paste it? (both from the keyboard) 3. How do you search for a string in vim? 4. How do you turn on line numbers in vim? 5. Name 2 ways to easily spot a constructor signature from a no rmal method signature. 6. What is overloading? Set Up Log in to your cs11f account, open a terminal session, make a new dir rectory (pa4), and copy in the objectdraw.jar file from the public directory. More detailed instructions can be found in previous homework write‐ups. Discussion Sections Go to them! Demos of the assignment will be given in Discussion Sections. Discussion Board There is a PA4 forum in Moodle for this programming assignment. Turnin Run the turnin script with the name of your programming assignment directory: turnin pa4 START EARLY!!! ...
View Full Document
- Fall '09
- Data Structures, controller class, resized applet canvas, vertical lines, new class ResizableBall, mouse event methods