paint project
TRANSCRIPT
-
8/12/2019 Paint Project
1/7
Final Homework
CSS 162: Programming Methodology
Due Finals Week by Sunday at 11:30pm
Winter 2014, Instructor Rob Nash
Summary
In this assignment, well tie together multiple software techniques weve learned throughout the
quarter. Well reuse our Shape classes and our data structures to build a fully functioning paint program
similar in spirit to Microsoft Paint. If youre familiar with older versions MS Paint (before Windows
Vista), then youll recognize this project.
The Paint Program
-
8/12/2019 Paint Project
2/7
What to Submit
{Shape.java, ShapeSubclass1.java, ShapeSubClass2.java, Application.java (your main Driver here),
ButtonPanel.java, DrawPanel.java}
Recursion?
Outcomes
{Class Design, File IO, ArrayList(of Shapes), Stack(Undo), Recursive Sorting,
Inheritance, Composition, Privacy Leaks, Interfaces, Exception Handling,
Generics}
You will demonstrate multiple software techniques all wrapped up in one project that can be used to
create pictures composed of shapes. Your software will read and write whole ArrayLists of Shapes (both
of which must be Serializable) to disk to load and save pictures. Youll sort your pictures by areaso that
the largest shapes are rendered before the smaller shapes, rather than the smaller shapes beingrendered first and then occluded by the rendering of the larger shapes later. Youll store each shape
that is drawn in an ArrayList or Queue (that youve already built) and as the user clicks on the
DrawPanel, youll add additional shapes to the list that representsyour Picture. While well see a
working demo of this code in class, its also worthwhile to return to the PolyDemo JFrame code that I
providedthis software maintains a list of Shapes in an ArrayList and draws them to the screen, which
is exactly what your software must do as well. Your shapes can be drawn to the screen and then
undone by offering undo and redo functionality. What if the user tries to undo when nothing has been
done? Or tries to load a file that isnt a serialized set of shapes? Your program will throw an
ApplicationException to describe the error that occurred.
Inheritance Hierarchies
Circle, Triangle extends Shape Application extends JFrame ButtonPanel, DrawPanel extends JPanel ApplicationException extends RuntimeException
o Attempting to undo an empty set of shapes, or redo an empty undo stack.
Composited Classes
An Application has a ButtonPanel and a DrawPanel An Application has a set of shapes An Application has a current shape or brush that it is drawing or adding to the list of shapes
o When the user left-clicks, clone (using a copy constructor) the current shape beingdrawn with and add it to the end of your list of shapes
-
8/12/2019 Paint Project
3/7
A ButtonPanel has a set of JButtonso Buttons to change the shape were drawingo Buttons to change the color of the shape were drawingo Buttons to undo and redo drawn shapeso Buttons to save and load a file
Use the JFileChooser or JOptionPane here to simplify this tasko Other widgets
A slider to control line width, for example. Three sliders to control custom RGB. A checkbox to determine whether to fill the shape being drawn
g.drawRect() versus g.fillRect()
Requirements
(1) Use your Shape hierarchy to implement drawing shapes on a JPanel. Define a Shape instancevariable called currentBrush and change thisto point to a square, circle, etc. depending on
which button is clicked in your button panel.
a. Shapes should beCloneable, so that you may copy the current users selected brush orshape and add that copy to the picture.
b. Shapes should beSerializable, so we may easily read and write these objects to diskc. Shapes should be Comparable, so we may sort shapes from smallest to largest using
compareTo().
i. Add your sort code where you store your set of shapes, which is most likely in theApplication container class.
(2) Inherit from aJPaneland make a ButtonPanelthat will hold all of the buttons for your paintprogram.
a. There should be buttons to choose the current shape being drawn.i. Your software must support at least three different shapes.
b. There should be buttons to choose the current color.i. You may want to see JColorChooserfor a free, premade color dialog.
c. There should be a button for undo and a different button for redo.i. Use your linked list stackto implement an undo/redo feature. Shapes that are
undrawn should be thrown onto a stack so that the user may undo and redo
drawing operations.
-
8/12/2019 Paint Project
4/7
d. There should be a button to rearrange the shapes in sorted order so that the largestshape is drawn first (element 0 in your ArrayList) and the smallest shape is drawn last
(element size-1).
i. Use any sort except the Bubble Sort to accomplish this.ii. Call the sort using the owner reference, discussed more below.
e. Add a button to saveyour picture to a file. Do this by using an ObjectOutputStream andwrite the ArrayList of Shapes to disk. This can be done in 3-5 lines of code.
f. Add a button to loada picture from a file. Do this by using an ObjectInputStream andread in a single objectyour ArrayList of Shapes.
i. See JFileChooser for a premade file selection dialog you can use, if you like.(3) Build a DrawPanelthat extends JPanel. A picture in our softwareis simply an ArrayList
that stores Shape objects; this will be implemented in in DrawPanel. So for each DrawPanel,
define an ArrayList to hold the shapes drawn onto that panel.
a. You can use PolyDemo as a starting point for the DrawPanel, since it already has a set ofshapes and can draw them to the screen; this is most of what your DrawPanel should do,
so reuse this class.
(4) Use your ArrayList class to store Shapesa. All lists and stacks must be of your own implementation in this project; you may notuse
Javas ArrayList, Stack, or other pre-built Java data structures.
(5) Use your Stack to provide an undo/redo feature for your shape.a. The only thing well undo and redo is shape drawing operations, and not color
changes, or any other operation.
b. When the user undoes an operation, take that Shape off the end of the ArrayList thatrepresents your picture and push it on your undo Stack.
c. When the user redoes an operation, pop off any Shape from the undo stack and add it tothe end of your ArrayList that represents your picture.
(6)
Getting Started
Find the code we built in previous labs and homeworks that relate to this assignment. Youll
need your shape and shape subclasses, your linked list (to be used for undo), and an example JFrame to
hold everything you will be designing (see the requirements above). Start by building a JFrame that
holds only a single object; a DrawPanel (note that weve done this in lab). Be sure DrawPanel
implements MouseListener like weve done in class, so you can capture Mouse Events and respond to
them (mouseClicked(), mouseReleased(), etc.). When youve got a JFrame with a working DrawPanel,
then add a new Panel to your JFrame (which will be the ButtonPanel). In order to see multiple items on
your JFrame, youll need to set a layout manager in the JFrame constructor, before you add the
DrawPanel and ButtonPanel. Id consider using a FlowLayout or a BorderLayout as your JFrame layout
manager.
-
8/12/2019 Paint Project
5/7
-
8/12/2019 Paint Project
6/7
o Now you can call methods defined in the Application class here owner.getBrush().clone() //example usage
Stack undoStack;o Use this to track changes to your picture and undo them
ArrayList picture;o Use this to store the set of Shapes that compose your picture
ButtonPanel Class Implements ActionListener
This class will hold a set of JButtonsthat will enable the user to select different shapes (or brushes),
choose the color the user is drawing with, undo a shape being drawn, redo a shape being drawn, save
shapes to a file, load shapes from a file and finally, sort shapes in ascending order with respect to area.
Once youve built the JFrame class above, inside its constructor you should instantiate a ButtonPaneland attach it to the root JFrame. Consider passing a this reference to the ButtonPanel constructor,
just as we did in the DrawPanel constructor. This is so when you change the current shape, color, etc.,
we can inform the JFrame, which manages a reference to the current shape being drawn (the artists
brush).
Data Members
Application owner;o In your Application class (likely in the constructor), provide a thisreference to your
ButtonPanel constructor and save it in the owner reference above.
o Now you can call methods defined in the Application class here owner.setCurrentBrush(new Square()) //an example owner.undo(); //more usage examples owner.redo();
Notes
Dont wait until the last minute to get help from your instructor this project is thelongest well see in 162.
Build your software in pieceso The containing JFrame, which holds a ButtonPanel and DrawPanel.o Test each piece as you progress, rather than waiting until the end
Ie, put a main in each class and test them
-
8/12/2019 Paint Project
7/7