Java assignment- I have starter code and teacher posted youtube vid explaining it. It's more just filling in the starter code - were on arrays and lists this week to give you an idea of where were...

Java assignment- I have starter code and teacher posted youtube vid explaining it. It's more just filling in the starter code - were on arrays and lists this week to give you an idea of where were at

1 Project Introduction The company you work for has grand plans for a drawing application.  This is just the first phase, serving as a proof-of-concept demonstration of feasibility.


Conceptually, there are two major components that work together to create drawings:  shapes and instructions.  Individual shapes are stored in their own files (in a folder called “shapes” under the project folder), and are, at their core, simple polygons consisting of a series of points.  When the application starts, the shapes are loaded into a shape library, and are then available by name (e.g., “circle”).  Think of these like the list of shapes you see when you choose Insert Shape in a Microsoft Office application.


Selected shapes are used to create drawings as dictated by a series of instructions in simple text files.  Instructions tell the application where to draw the shape, how to scale it, what color to use, etc.


2 About Shapes All shapes are polygons, created from a series of points.  All shapes are initially drawn to fill an area that is 100 pixels wide by 100 pixels tall (end users can later scale these via instructions).  No color or other information is included in the shape file.  To allow the loading of a shape in a single step, and to prevent users from modifying the files arbitrarily, these are not stored in simple text format1.  Think of these as the shapes you see when you choose Insert Shapes from a Microsoft Office application.


3 About Instructions Files 3.1 Introduction Instructions are contained in simple text files.  One file contains all the instructions for one drawing, which can be comprised of many shapes.


On the first line is the canvas drawing instruction.  Each subsequent line contains a shape drawing instruction.  Each instruction can contain several commands.   3.2 Example Here is a quick example with a single shape-drawing instruction containing several commands:


width=500, height=400, red=198, green=218, blue=247 shape=square, scale=200, x=50, y=25, filled=true, red=218, green=34, blue=23


Interpretation of instructions/commands, listed by line number of the file:


1. Create a drawing canvas that is 500 pixels wide by 400 pixels tall, with a solid background color as specified by the RGB values (a light blue color, in this example). 2. Draw the shape called “square”, at 200% of its original size.  Start the drawing 50 pixels to the right and 25 pixels below the canvas origin.  Draw a filled shape whose color is specified by the RGB values (a deep red color, in this example).


1 While not part of the client-side software we plan to ship, the Utility class contains some useful methods that allow the company to generate all shape files.  This is not part of the normal workflow; it is called only when needed to update existing shapes or generate new ones.  Another non-shipping utility is provided to help developers create sample text files (InstructGui); a more refined tool is planned for actual end users.


Java 2  Barry


Page 3 of 14


3.3 Instruction and Command Format Each command takes this form:  command=value


Notes: • One instruction can include multiple commands, separated by commas.   • Neither commands nor values are case sensitive.   • No delimiters are used around values, regardless of data type.   • Commands may appear in any order.  • If not included in instructions, values for non-required commands will be set to defaults. • In general, out-of-range values are ignored, with values set to defaults instead. • White space within an instruction is ignored.   • Blank lines are not allowed in instruction files.


See the next pages for a list of all available canvas and drawing commands.


(project handout continues…)




Java 2  Barry


Page 4 of 14


3.4 Available Canvas Commands Here is a list of available Canvas drawing commands and their meanings.  Note that some commands override others, e.g., gradient fills override the solid background color for the canvas.  Shaded commands are part of extra credit work.


Command Description Default Example2 Example Interpretation width The desired width of the canvas 100 width=400 The drawing canvas is 400 pixels wide height The desired width of the canvas 100 height=200 The drawing canvas is 200 pixels tall red Gives the red component of the RGB color specification (range 0–255) for a solid color background 255 red=193 The red value for the solid background is 193 green Gives the green component of the RGB color specification (range 0–255) for a solid color background 255 green=34 The green value for the solid background is 34 blue Gives the blue component of the RGB color specification (range 0–255) for a solid color background 255 blue=254 The blue value for the solid background is 254 gradDir3 For gradient fills, specifies the direction of the fill, either 0 (horizontal), 1 (vertical), 2 (diagonal from top left), or 3 (diagonal from top right) 0 gradDir=1 The gradient direction will be vertical gradStartRed For gradient fills, the red component of the RGB specification for the color at the starting area of the drawing canvas.  Overrides solid background color 255 gradStartRed =182 The red value for the gradient fill at the top of the canvas is 182 gradStartGreen For gradient fills, the green component of the RGB specification for the color at the starting area of the drawing canvas.  Overrides solid background color 255 gradStartGreen =23 The green value for the gradient fill at the top of the canvas is 23 gradStartBlue For gradient fills, the blue component of the RGB specification for the color at the starting area of the drawing canvas.  Overrides solid background color 255 gradStartBlue =150 The blue value for the gradient fill at the top of the canvas is 150 gradEndRed Same as topRed but specifies the color component for the end area of the drawing canvas. 255 gradEndRed =99 The red value for the gradient fill at the bottom is 99 gradEndGreen Same as topGreen but specifies the color component for the end area of the drawing canvas. 255 gradEndGreen =3 The green value for the gradient fill at the bottom is 3 gradEndBlue Same as topBlue but specifies the color component for the end area of the drawing canvas. 255 gradEndBlue =93 The blue value for the gradient fill at the bottom is 93




2 Wrapping, when shown, is for space considerations; these should be on the same line in the actual file. 3 Implement grad* commands for extra credit (see below).


Java 2  Barry


Page 5 of 14


3.5 Available Shape-Drawing Commands Here is a list of available Shape commands and their meanings.  Note that some commands do not make sense to use together, e.g., random placement and repeat offsets.  Shaded commands are for extra credit.


Command Description Default Example Example Interpretation shape A required command matching a shape that exists in the shape library  shape=circle Draw the circle shape scale The scale at which the shape should be drawn, represented as an integer.  Must be greater than 0 100 scale=25 Draw the shape 25% of its original size x The x coordinate at which the shape is to be drawn.  If the minimum integer4 is specified (negative 2147483648), a random x value will be generated, in the range 0 to the canvas width.  If random placement is used, this overrides any repOffX value specified 0 x=200 Draw the shape 200 pixels to the right of the canvas’s origin point y The y coordinate at which the shape is to be drawn.  If the minimum integer is specified (negative 2147483648), a random y value will be generated, in the range 0 to the canvas height.  If random placement is used, this overrides any repOffY value specified 0 y=150 Draw the shape 150 pixels below the canvas’s origin point rep The number of repeats to draw.  Makes sense only for random placement, offsets, or repeat rotations 1 rep=5 Draw five shapes filled Indicates whether the shape should be drawn as a simple or filled polygon.  Valid values are true and false true filled=false When drawing the shape, just draw the outline red Gives the red component of the RGB color specification, range 0–255 0 red=193 The red value of the RGB color is 193 green Gives the green component of the RGB color specification, range 0–255 0 green=34 The green value of the RGB color is 34 blue Gives the blue component of the RGB color specification, range 0–255 0 blue=255 The blue value of the RGB color is 255 repOffX5 For repeated shapes, gives the offset x value for each subsequent shape drawn  0 repOffX=15 Draw each repeated shape 15 pixels to the right of the last one repOffY For repeated shapes, gives the offset y value for each subsequent shape drawn  0 repOffY=25 Draw each repeated shape 25 pixels below the last one rotate6 Gives the angle (in degrees) that the shape should be rotated clockwise 0 rotate=30 Rotate the shape 30 degrees clockwise repRot For repeated shapes, gives the rotation to be applied to each subsequent shape drawn 0 repRot=10 Draw each repeated shape rotated 10 additional degrees


4 Write the literal in the instruction file.  Use Integer.MIN_VALUE and Integer.MAX_VALUE in code. 5 Implement repOffX and repOffY for extra credit (see below). 6 Implement rotate and repRot for extra credit (see below).


Java 2  Barry


Page 6 of 14


4 Design Documentation Before you code, create appropriate design documentation and obtain feedback.  Update designs per feedback, then use them during the rest of the development process, then submit them as part of your project.  This should include a UML Class Diagram and a UML Object Diagram.


5 Code Implementation 5.1 Additional Building Blocks You will Need • Implementation and multiple uses of a generic (not class specific) ArrayList • In ArrayList, implementation of an Iterator and for/each support (use both, somewhere) • Serialization • Exception handling (try/catch) • File/folder processing • DrawingPanel class (from textbook author) and Java classes including Graphics and Color. 5.2 What Is Provided (and some things you must add/augment) You will be given a starter project.  Do not modify this code unless indicated with an asterisk (*). • Code for the Utility* class.  This includes code that builds shape files.  In this class, add at least two more shapes.  This class requires a working implementation of a class you must finish (clue!). • Code for the CanvasInstruction class.  This includes class basics and the code that will read a canvas instruction from a text file and return it as an object.  A test class and test files are also provided. • Code for the DrawInstruction class.  This includes class basics and the code that will read a drawing instruction from a text file and return it as an object.  A test class and test files are also provided. • Code for the DrawingPanel class, supplied by the textbook author.   • Code for the Point* class.  You may only change the class definition line, if needed. • An instruction-file-creating GUI.  Run the main method in InstructGUI to use it.   • Sample instruction files.  Do not modify these.  Create at least two additional instruction files. 5.3 Requirements 5.3.1 ArrayList Implement a generic (not data-type specific) version of ArrayList; do not use Java’s version. • You would learn far more if you code this yourself; but frankly you may not have time, here. • If you choose to use the book's code, you are completely responsible for what is there; you must be able to explain every line of code.  Write sample code (for yourself) that tries every method, so you know what each one does.  Review code to make sure you know how each method does its work. • Use your ArrayList class (not Java’s) everywhere that one object manages a list of other objects. • To the ArrayList class, add a method called compressNulls.  It should pack non-null references into the lower indexes of the array, adjusting the size accordingly.  This should use an efficient algorithm; do not bubble/shift to accomplish the task (we want O(n), not O(n^2), if you understand that notation).  Do not create a new array in the process; that is unnecessary and inefficient. • Set the default size in the ArrayList class to 50 items.  In the same class, adjust the code to grow the list by 50% when needed.  But when creating the initial drawing instruction list, use the fact that most instruction files contain 10 or fewer drawing instructions.


Java 2  Barry


Page 7 of 14


• Implement an iterator within your ArrayList class.  Also make it possible for clients to use for/each loops with your ArrayLists.  Use for/each loops in code, wherever possible. • Include the ArrayList class on pertinent design documentation. • When creating generics, it is likely you will generate some compiler warnings that need to be fixed.  Use the class-demonstrated method of running javac.exe with Xlint, to find these.  Do not simply suppress such warnings; there is only one place in this project where this is expected and acceptable.


5.3.2 Reading Files • The Utility class provides code that serializes shape files.  The code you submit should not run this code; it is unnecessary except as a preparatory step while working on the project.  In your submitted project code, you will need to read in and use this serialized data, however.   • The instruction file should be passed to the appropriate constructor as an argument.  The shape file and folder information does not need to be passed as an argument; it is not specified by client code, as it is always the same. • Assume files are well formed; you do not need code that catches file truncations, missing data, etc.


5.3.3 Classes and Methods • Most classes need only a single, full constructor.  If you have more than one constructor, do not duplicate code between constructors, but call the more complex one from the simpler one, passing along defaults. • Classes should include “get” methods that will allow clients to access state information as well as individual items (never the whole list7) from lists those classes manage.   • You don’t need mutators for most attributes included as parameters inside constructors; but think about methods that will make sense given the Client requirements (those stated, or you know may arise), esp. in cases where lists are manipulated.   • Implement preconditions that make sense, e.g., for empty strings, null pointers where they should not be allowed, and numeric values that make no sense in the context of the problem. Implement these such that you have fine control over what failed and what thoughtful error message you can provide to Client code. • Do not carelessly proliferate class “throws” additions; handle those locally whenever it makes sense. • toString methods should include all information for contained objects, e.g., shapes should show their points.  This may help in debugging, too. • A Main class should display your drawings as well as rich toString output for every class.  Note:  do not remove the sample drawings, even if you do not do the extra credit; just add yours.


5.3.4 Shapes and Drawing Add at least two of your own shapes.  Use those, along with the provided samples, to create a couple of interesting drawings (at least two) consisting of a variety of shapes and colors.  The drawing instructions should include a variety of commands, as many as you can reasonably apply.  Your Main class should invoke these drawings.


Use no Graphics2D commands for any of this work, regular or extra credit.


7 A perfect alternative is to surface an iterator, e.g., Shape can provide a method that return an iterator for the list of Point objects it maintains.  With a trivial amount of additional work, you can also make for/each possible.


Java 2  Barry


Page 8 of 14


5.4 Style Follow the Course Style Guide.  You will lose points on every assignment if you fail to do so.


6 Testing Write unit tests for compressNulls ArrayList method.  Otherwise, take a break from testing on this project; much of the interesting stuff cannot be tested via the methods we have so far employed.  It is a good thought exercise to ponder how you would test drawn output; think about that and we will discuss it in class.


7 Hints • The supplied starter code will not compile.  Compile errors are big clues, as are constructor parameters and method names you will find in that code.  Look carefully at this code! • If the Utility shape generation fails, it is likely you have not implemented serialization correctly in the classes you’ve written; in that case, review the troubleshooting tips discussed in class. • Design carefully.  Incorporate design feedback before coding your project. • A complete object diagram will be a great help when navigating drilldowns. • Think carefully about the order of implementation.  Start with basic building blocks and work up. • Use InstructGui to create additional instruction files.  You must have successfully created shape files for shapes you wish to use, as the shapes folder is scanned when InstructGui starts.  Alternatively, to create your own files by hand (or edit existing ones), use a plain text editor, e.g., Notepad on Windows or TextEdit on Mac, set to Plain Text mode.  Do not use editors that write RTF or adorned formats.


8 Recommended Milestones 1. Shape File Generation.  Get Utility compiling, which will require Shape to be completed and Serialization to be implemented in a few classes.  Run the static method createShapeFiles to get shape files created.  You can do this in BlueJ by right clicking the class and choosing the method. 2. ShapeLibrary.  Get this class working.  This entails getting the shapes folder scanned, files filtered (you should only attempt to deserialize .shp files), files deserialized into shapes, and those shapes added to the shape library’s list.   3. Crude Drawing.  Get your first canvas created and first shape drawn.  It does not have to be in the right position, nor scaled, just drawing somewhere. 4. Required Shape Drawing.  Get basic shape commands working (positioning, scaling, color, fill, etc.). 5. Your Art.  Add your own shapes and create your own drawings via new instruction files. 6. Extra Credit.  Implement the advanced Canvas and Shape commands implemented including gradients, repeats, and Utility updated with code to generate smoother polygons for circles/hearts.


9 Submitting Your Work Follow the course’s Submission Guide when submitting your project.


Java 2  Barry


Page 9 of 14


10 Extra Credit 10.1 Circle/Heart Instead of using the provided code that hard-codes circle and heart points, write code that accepts an increment parameter (angle or other way to divide a circle), and then generates the points using a loop.  If you’ve forgotten basic trigonometry concepts, review unit circle trigonometry and the use of radians, perhaps via online videos like this one.  Also review the trigonometric functions provided in Java’s Math library.  There is a later project that requires thinking along these lines; you will be glad you did this work.   10.2 Gradient Fill In addition to the basic canvas instructions, implement the grad* commands for gradient fills.  In your sample drawing(s), illustrate their use in an interesting way.  This does not need to be done pixel by pixel; there is a cleverer way, if you think about it.  Note that these should work on rectangular canvases. 10.3 Repeat Offsets In addition to the basic drawing instructions, implement the repOffX and repOffY instructions.  In your sample drawing(s), illustrate their use in an interesting way.   10.4 Rotation In addition to the basic drawing instructions, implement the rotate and repRot instructions.  These will also require a bit of trigonometry to assist with rotation on a 2D plane.  Do not use Graphics2D for this.  If you search, you’ll find online resources and code (example), and videos.  In your sample drawing(s), illustrate their use in an interesting way.  Make sure these work in conjunction with other commands.


(continues…)




Java 2  Barry


Page 10 of 14


11 Grading Matrix and Points Values Area Value Evaluation Design docs 10% Did you submit initial design documents, and were they in reasonable shape to begin coding?  Did you submit final design documents, and were they a good representation of the final version of the project? ArrayList (generic) implementation/use; proper handling of compiler warnings, compressNulls method 10% Was your own version of a generic ArrayList implemented, and is it working properly?  Are there unchecked warnings present when scanned by Xlint?  Were those warnings handled properly, and only suppressed in the expected/proper cases?  Was compress nulls written, and does it work with the desired efficiency? Iterator implementation and use 5% Was an iterator implemented, along with for/each support?  Was for/each support used throughout the project, where possible? Class implementation 20% Were classes implemented successfully and properly? Instruction implementation 25% Does the Drawing class successfully implement all basic (non-extracredit) canvas and drawing instructions? Deserialization 10% Were the shape files deserialized correctly at shape library construction time? New shapes added 5% Did you create two new shapes, and demonstrate their use? Sample drawing instructions 5% Did you create two new instruction files, to demonstrate the use of all program capabilities you have coded? Style/internal documentation 10% Did you use JavaDoc notation, and use it properly?  Were other elements of style (including the Style Guide) followed?   Extra:  Circle/heart math 1% Did you revise the existing circle and heart creation method to use math to generate points, creating a more attractive result? Extra:  Gradient fill 1% Did you implement the gradient fill commands for the canvas?   Extra:  Repeat offsets 1% Did you implement repeat offsets?   Extra:  rotate and repRot 1% Did you implement the rotate and repRot instructions?   Total 104%


Code that does not compile or run will not be graded




Java 2  Barry


Page 11 of 14


12 Appendix – Expected Output 12.1 Output from Instruct-Simple.txt All students should have code producing this output


12.2 Output from Instruct-Rand.txt All students should have code producing similar output (randomization will cause some variation).  If you have far fewer stars, you should suspect you have a bug your randomization algorithm.


Java 2  Barry


Page 12 of 14


12.3 Output from Instruct-RepeatOffset.txt Students completing the Repeat Offsets portion of the Extra Credit should see these results.


12.4 Output from Instruct-Rotate.txt Students completing the Rotation portion of the Extra Credit should see these results.


Java 2  Barry


Page 13 of 14


12.5 Output from Gradient Instruction Files Students completing the Gradient portion of the Extra Credit should see these results for InstructGradient.txt.


Students completing the Gradient portion of the Extra Credit should also see these results.




Instruct-Gradient-Horiz.txt     Instruct-Gradient-Vert.txt        Instruct-Gradient-DiagTL.txt                                           Instruct-Gradient-DiagTR.txt


Java 2  Barry


Page 14 of 14


13 Abbreviated Version You are doing the abbreviated version of this assignment.  There are changes below that should make the project a bit easier and quicker.   13.1 Provided Code • Shape—starter code for this class is provided.  This class should not take much work to complete. • ShapeLibrary—a starter file is provided but requires the completion of a key method, the one that loads shapes from shape folder and stores them in its collection.  It will require some other small changes, as well. • Drawing—a bare-bones starter file is provided.  This is where most of your work will be done. 13.2 Provided Diagrams Starter Class and Object diagrams are provided.  You will need to complete these and organize them to fully document the project.


14 Super-Abbreviated Version You are doing the super-abbreviated version of this assignment.  There are changes below that should make the project much easier and quicker.   14.1 Provided Code • Shape—most of the code for this class is provided.  Only a little work remains. • ShapeLibrary—a starter file is provided, as is a method that deserializes a Shape from the specified file.  You must complete the method that scans the shape folder, leverages the method that reads files, and stores the shapes.  There is a small amount of additional work required, as well. • Drawing—a bare-bones starter file is provided.  This is where most of your work will be done. • Utility and Dependencies—all code is in place to support the creation of the initial shape files.  Use the provided static Utility method to create those files.   14.2 Provided Diagrams Completed UML Class, Object, and Sequence diagrams are provided.  You do not need to submit preliminary or final designs for this project.


May 18, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here