CSC171 — HW 5 — URPaint
CSC171 — HW 5 — URPaint
Figure 1: What the finished application looks like.
Problem Description
The purpose of this assignment is to give you practice building a complete graphical program. Along the way, you will gain deeper experience with object oriented programming – in particular, with inheritance, polymorphism, interfaces, event driven programming, and graphics.
Your objective is to build a painting program. Your program should allow a user to choose a shape (circle or square) and place it anywhere on the screen. The user should be able to control the position of shapes by clicking and dragging. The user should be able to individually control the color of the shapes, as well as the color of the background. The controls (shape and color) should be presented via a panel at the top of the overall frame. You must also write a readme file for this assignment. Please read the complete writeup before you begin. This assignment is due by 1159pm March 30th.
GUI Layout Requirements
Your overall program should be a subclass of JFrame named URPaint. You should build separate subclasses of JPanel for the actual painting and the controls. Call the first the PaintPanel and the second the ControlPanel.
The overall URPaint class should use the default BorderLayout for the main frame, with the ControlPanel at PAGE START and the PaintPanel at CENTER. For the Control panel you can use a JPanel with a PAGE AXIS BoxLayout with two panels inside – the top panel can contain the buttons and the bottom can contain the JSliders and JLabels. Each of these subpanels should have a LINE AXIS BoxLayout. You can tweak the spacing and size by adding horizontal and vertical struts from the Box class. The sliders should have labels.
For the PaintPanel, you will be using absolute positioning, so you should set its layout to null.
Event Handling
Your program will need to handle different kinds of events: • MouseEvents – clicks, presses, and drags when placing, selecting, and adjusting shapes. • ActionEvents – for the circle, square, and clear buttons. • ChangeEvents – for the RGB sliders
On a mouse click, if the user clicks an existing shape, it should become the active shape, and the RGB sliders should adjust to reflect the shape’s color. If the user clicks an empty space, then a new shape should be created centered at the location.
On a mouse press, if the user presses on an existing shape, it becomes active until the mouse is released.
On a mouse drag, if there is an active shape, then the shapes position should change to the position of mouse event.
The circle and square buttons should simply update an instance variable in the overall URPaint class. The clear button should eliminate all the drawn shapes and reset the panel to empty.
Useful Abstractions
An abstract base class for Shapes – this allows uniform treatment when painting, moving, and interacting. This class should include abstract methods for paintComponent, intersections, and moving the shape by an offset. It should also have an instance variable for the color and corresponding getters and setters.
An advantage of the Shape class is that the PaintPanel can maintain an array of all drawable shapes very simply in a single array. A proper collection would be more appropriate, but we have not yet covered those topics, so you should use an array. For simplicity, you can assume that there will never be more than 1000 shapes at once.
To facilitate draggable circles and adjustable colors, you should keep track of an “active shape”. When the user clicks on an existing shape to activate it, that information has to persist until they deselect or the program terminates. The identity of the active object should be stored as a private reference variable in the overall JFrame instance. Making the ControlPanel and PaintPanel classes be inner classes of the overall URPaint class simplifies communication.
Grading
• Click to add circles centered on click location. (10pts) • Click to add squares centered on click location. (10pts) • Circles can be dragged smoothly. (10 points) • Squares can be dragged smoothly. (10 points) • Shape selector panel controls between placing circles and squares. (10 points). • Active shape is highlighted and released by appropriate events. (10 points) • RGB Sliders control background color (10 points). • RGB Sliders control individual circle colors (10 points). • Style and control. (10 points) • Readme. (10 points)
General Notes and Other Requirements
Filenames and Submission Format - You should do all of your work in a public class named URPaint. Your source file should therefore be named URPaint.java. All of your classes should go into this file – you should carefully consider the class relationships and declarations. You should also include a readme file, which should be a plain text file with a description of your program. You should zip your source file and readme together into an archive named Homework5.zip. Note that this is case-sensitive. As stated elsewhere, do not use alternative compression formats or submit any unrequested files or directories.
Documentation and Style - You should use consistent formatting (i.e., indentation, whitespace, and aligning of braces). You should choose clear and informative variable names, and follow camelCase conventions. A reasonable set of guidelines to follow are those provided by the Google Java Style Guide.
Additionally, your program must begin with a block comment listing your name, school email, the date you last modified the code, and the assignment name. Technically complex regions of your code should be documented with in-line or block comments as appropriate. Key areas to consider are complex expressions (numerical or logical) and high-level control flow.
No Packages, Directories, or Modules - You should not include any additional unnecessary files or directories. You should not embed your source file in any unnecessary directories. You should not include any package declarations in your program. You should not embed your program in any unnecessary modules.
Resubmissions and Deadlines - You are encouraged to double check your file modification times, contents, and names when submitting work. Please be careful to not introduce last minute typos by hastily saving and submitting work. You are encouraged to double check your work after submitting, and resubmit if you notice errors. Re-submissions before the deadline are not penalized; however, no revisions (or late work) will be accepted after the submission deadline.
Academic Integrity - You must be able to explain any aspect of your program to the instructor or TAs at any time. You must take sole responsibility for the quality and integrity of the work you submit in this course. This is an individual assignment and all work must be your own. Please note if you submit work that involves coding styles, techniques, or classes not discussed in class, then you should be extra well prepared to explain how they work to your TA or the instructor. If you are unable to explain your code to the instructor, you may receive significant grade penalties. Do not submit work that is not your own..
Additional Documentation Requirements
Please include the following information in your readme: • Basics – Your name, school email (aka, blackboard username), the date of submission, and the assignment number. • Self-review – please include your best estimate as to the correctness of your program on each sub-task, as well as any remaining bugs or defects. • Metadata – please include an estimate of the total time you spent on this assignment (in hours) as well as any particularly tricky bugs you had to solve along the way.
Examples of Potential Deductions
• -5 to -90 functionality • -5 to -10 for graphical errors • -5 to -15 for package, module, archive, and directory issues. (see general requirements) • -5 to -15 source file and public class naming issues (see general requirements) • -5 to -15 documentation issues (see general requirements) • -5 to -15 style issues (see general requirements) • -10 missing readme
Note that other deductions may be applied based on the quality of submitted work. In particular, if your program - as submitted - does not compile cleanly, you will receive heavy deductions up to potentially a zero on the assignment. Partial credit may or may not be applied at the discretion of the instructor.