Graphics and Inheritance
-
Analyze diagrams, documentation, and code to create functioning classes.
-
Create an interesting class that will draw pictures using a gui.
-
Start using keywords that are specific to inheritance.
Warning
|
Be careful, this assignment may take extra time! |
Write a program that reads a list of graphics primitives from a text file and draws them in a window. This assignment creates a set of Java classes in a very primitive hierarchy to model the various graphics primitives: text, squares, rectangles, triangles, and circles, and ovals. Each primitive has a different set of attributes, for example text has font name, style, and size, and a rectangle has color, width, and height, etc. When your program is complete, you will be able to see the contents of the text file rendered inside of a window. In the next assignment you will substitute a more complex hierarchy of classes that takes advantage of inheritance to reduce duplication and force consistent behavior among graphics primitives. You will also another primitives, polygon.
-
Create a project called P2.
-
Download and import P2-starter.jar.
After the import is complete, your P2 directory should look like this:
P2/ ├── resources │ ├── circles.txt │ ├── ovals.txt │ ├── rectangles.txt │ ├── simplePrimitives.txt │ ├── squares.txt │ ├── text.txt │ └── triangles.txt └── src ├── DrawInterface.java ├── DrawProgram.java ├── Primitive.java └── UserInterface.java
Each test file has primitives for one type except for the simplePrimitives
file
which has 37 primitives for all different types.
Tip
|
Do not be concerned that the Primitive class is empty. You will be adding
code to the Primitive class in next assignment.
|
Refer to the simplePrimitives
file to see the format of the text files.
-
The color attributes in the file are numbers in the format
RRGGBB
, specified in hexadecimal, where each channel has eight bits.-
For example
FF0000
is red,00FF00
is green, and0000FF
is blue, all maximum intensity.
-
-
The font name can either be "Times Roman" or "Arial".
-
All coordinates are integer values representing pixels on the drawing surface.
Java AWT(the underlying graphics library used in this project) uses a screen coordinate system where (0,0) corresponds to the pixel in the top left corner of the window. X increases towards the right and Y increases towards the bottom.
For specifications on the precise drawing location of the methods provided in UserIterface, consult the Java AWT Graphics class Javadoc.
Create the following classes:
-
Square
-
Rectangle
-
Triangle
-
Circle
-
Oval
-
Text
Each shape should inherit from the Primitive
class. For example:
public class Square extends Primitive {
Each shape requires certain fields and methods. This includes location, size, color, etc. You will need to figure out what each class should include through analyzing the following sources:
-
The code in the
DrawProgram
andUserInterface
classes.-
The code in the
DrawProgram
class is not complete, this will be the next part of your assignment. However, you should able to deduce what each shape needs from the code that is already provided.
-
-
The
UML
diagram below -
The javadoc
Note
|
Not all parameters are explicit in the UML diagram, so make sure that you look at the javadoc, as well. |
DrawProgram
classThe main method calls load, passing args[0]
as the name of the text file containing graphics primitives,
then it calls draw to draw the primitives. Try running the program with the files corresponding
to the Text
, Square
, or Rectangle
classes.
Once you have successfully tested these classes, complete the code for the following methods:
public ArrayList<Primitive> load(String filename);
public void draw(ArrayList<Primitive> primitives);
The image below shows the output from the simplePrimitives
file:
Your program must meet the following specifications:
-
Work on your own, as always.
-
You must package all source files into
P2.jar
for submission. -
Assignments should be implemented using
Java
1.8. -
Make sure your code runs on machines in the COMCS120 lab.
-
Submit your program to the
Checkin
. -
Read the syllabus for the late policy.
-
We will be checking programs for plagiarism, so please don’t copy from anyone else.
-
100 points for perfect submission.
-
0 points for no submission, will not compile, submitted class file, etc.
-
Preliminary Tests:
-
compileTest: checks that all source files compile. (0 points)
-
testText: verifies the
Text
class withDrawProgram
. (15 points) -
testSquare: verifies the
Square
class withDrawProgram
. (15 points) -
testTriangle: verifies the
Triangle
class withDrawProgram
. (20 points)
-
-
Final Tests:
-
testRectangle: verifies the
Rectangle
class withDrawProgram
. (10 points) -
testCircle: verifies the
Circle
class withDrawProgram
. (10 points) -
testOval: verifies the
Oval
class withDrawProgram
. (10 points) -
testSimple: verifies the drawing of
simplePrimitives
file. (20 points)
-
Important
|
Submit P2.jar to Checkin |