Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMPSCI 230

Assignment THREE

2022


Introduction - The Bouncing Program


This is the final instalment of the Bounce project and perhaps the most challenging. The assignment involves working with design patterns and Java's Swing API. You are required to complete and extend the application in a way that makes appropriate use of     design principles and patterns. A3 is a model/view application that presents two views of a list of bouncing shapes in the program. Such applications are commonplace and introduce the need for views to be mutually consistent and synchronised with a data         structure whose state changes at run-time.

You are advised to start working on the assignment using this document as a main guide and switch to/from CR whenever required. Directly working on the CR questions without reading this document will be problematic.

Assessment Criteria

Complete CodeRunner questions and submit the entire program via the assignment dropbox (https://adb.auckland.ac.nz/) at   any time from the first submission date up until the final due date. You will receive an electronic receipt. Submit a single zip file containing all source files remember to include your name, UPI and a comment at the beginning of each file.

You may make more than one submission, but note that every submission that you make replaces your previous submission.          Submit ALL your files in every submission. Only your very latest submission will be marked. Please double check that you have  included all the files required to run your program in the zip file before you submit it. Your program must compile and run to gain any marks.

Description

Task 1: The NestedShape class [12 marks]

Download the template zip file from Canvas. Define a new class named NestedShape which represents nested shapes. A              NestedShape contains zero or more inner shapes that bounce around within this shape. The children of a NestedShape instance can be either simple shapes, like RectangleShape and OvalShape objects, or other NestedShape instances. Hence, a                NestedShape object can have an arbitrary containment depth.

The Shape and NestedShape structure is an application of the Composite design pattern. The composite pattern describes a  group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to       "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat  individual objects and compositions uniformly, i.e. users should be able to add one or more rectangle(s)/circle(s) or one or more nested shape(s) into a list of shapes. Each nested shape contains zero or more shapes.

The NestedShape should use an array list of shapes to store the shapes within itself. Each shape inside the NestedShape has its own coordinate system. For example if a rectangle with a location of (10, 20) is contained inside a NestedShape, it will be           located 10 pixels below and 20 pixels to the right of the top-left corner of that NestedShape.

You may want to use graphics translation in order to draw the children of a nested shape. (i.e. adjusting the coordinate system by  specifying a new origin (the nested shape’s top left corner) that corresponds to a point in the original coordinate system). This can be achieved using the translate() method. Once translated, all drawing operations are performed relative to the new origin.       Note that any translation should be reversed after painting all inner shapes.

For example, a nested shape has been created at (60, 40), width=100, height=50 and an inner rectangle is at (10, 20), width=50, height=25. Then, your program should do the following:

● Set the pen's color to black

● Draw the nested shape rectangular boundary at x = 60, y = 40, width=100, height=50

● Translate the coordinate system by (60, 40)

o  Set the required pen's color

o  Fill the inner rectangle (10, 20) 50 x 25 (i.e. fill the inner rectangle at the absolute position (70, 60) )

● Restore the original origin by translating the coordinate system by (-60, -40)

Note:

● The root of the program is a nested shape without any children. (Note: you should remove the shapes array list in the AnimationViewer class.) The properties of the nested shape are:

o  the top left corner is (0,0)

o  the width is marginWidth and the height is marginHeight

o  the margin width is DEFAULT_MARGIN_WIDTH and the margin height is DEFAULT_MARGIN_HEIGHT

o  the fill color is Color .black

o  the text is an empty string

o  the path type is PathType .BOUNCE.

● When the user clicks anywhere on the bouncing area with the "NESTED" shape type selected in the ShapeType combo box. The mouseClicked() method should create a nested shape and add the shape to the root. The nested shape contains one    inner rectangle shape by default. The properties of the nested shape are:

o  the top left corner is at the mouse point

o  the width/height is the current width/height in the AnimationViewer class,

o  the fill color is the current fill color in the AnimationViewer class

o  the text is the current text in the AnimationViewer class

o  the path type is the current path type in the AnimationViewer class

o  the margin width/height is the width/height of it's parent (i.e. the root)

o  The properties of the inner rectangle shape are:

▪ the top left corner is (0,0)

▪ the width/height is the half of the width/height of it's parent

▪ the margin width/height width/height of it's parent

▪ the fill color is the fill color of it's parent

▪ the text is the text of it's parent

▪ the path type is PathType .BOUNCE

● Users would be able to select nested shapes and change the fill colour/text of the selected nested shapes. The method should also modify the fill colour/text of all inner shapes.

● Users would be able to select nested shapes and change the width/height of the selected nested shapes. The method should also modify the margin width/ margin height of all inner shapes.

Complete the CodeRunner questions

● Question 1: Complete the ShapeType class

● Question 2: Modify the Shape class

● Question 3 - 5: Complete the basic structure of the NestedShape class

● Question 6: Complete the Painter Interface

● Question 7: Complete the draw() and move() method of the NestedShape class

● Question 8 - 9: Complete the AnimationViewer class

You should make changes in the template files and complete the GraphicsPainter class. Your program should support the following:

Users would be able to create new nested shapes by clicking anywhere within the panel area of the program.

The properties of the newly nested shape are based on the current values saved in the appropriate UI fields.

The inner shape of the nested shape is a rectangle, the width/height is half of it's parent. The path is bouncing.

●     Users would be able to select nested shapes and change the width/height/fill colour/text of the selected nested shapes. The method should also modify the margin width/margin height/fill colour/text of all inner shapes.

Task 2: The Tree Model [16 marks]

This task requires you to lay the foundation for a JTree. The JTree displays a hierarchical view of the composition structure of bouncing shapes . The AnimationViewer class implements the TreeModel interface. A JTree object does not actually contain  your data; it simply provides a view of the data. Like any non-trivial Swing component, the tree gets data by querying its data       model. TreeModel specifies methods for getting a particular node of the tree, getting the number of children of a particular node, determining whether a node is a leaf, notifying the model of a change in the tree, and adding and removing tree model listeners.    Each node in the program contains a Shape object. A leaf node is just a simple shape but a composite node is a nested shape.

In order to provide the above functionalities, the structure of the AnimationViewer class has been changed. We do not use an   ArrayList to store a list of shapes now. The AnimationViewer defines a root of type NestedShape for holding the data to be presented by a JTree.

Note:

● When the user selects the root and clicks the "Add shape" button, a new shape will be added to the root and a new node will be added to the root as well. The properties of the newly created shape are:

o  the top left corner is (0,0)

o  the width/height is the current width/height in the AnimationViewer class

o  the fill color is the current fill color in the AnimationViewer class

o  the text is the current text in the AnimationViewer class

o  the shape/path type is the current shape/path type in the AnimationViewer class

o  the margin width/height is the width/height of it's parent (i.e. the root)

● When the user selects a nested node and clicks the "Add shape" button, a new shape will be added to the selected nested shape and a new node will be added to the selected node as well. The properties of the newly created shape are:

o  the top left corner is (0,0)

o  the width/height is the half of current width/height in the AnimationViewer class

o  the fill color is the current fill color in the AnimationViewer class

o  the text is the current text in the AnimationViewer class

o  the shape/path type is the current shape/path type in the AnimationViewer class

o  the margin width/height is the width/height of it's parent

Complete the following CodeRunner questions:

Q10: Implement the basic tree model functionality

Q11 - 13: Implement methods for adding a new node

Q14 - 15: Implement methods for removing an existing node

Q16: Update the mouseClicked method

Q17 Implement an inner class for adding a node

Q18: Implement an inner class for removing an existing node.

You should also make changes in the template files and complete the following:

Complete the A3 class

Modify the AnimationViewer class such that the AnimationViewer implements the TreeModel interface Modify the toString() method in the Shape class such that the method returns the shape type, the color and the

text only.

Your program should support the following:

● Create a new node and shape by clicking a point in the bouncing area.

● Create a new shape by clicking the "Add shape" button. The new node will be added to the selected node. The selected node must be an instance of NestedShape. Your program should display an error message if the node is not a nested  shape using a message dialog box.

● Remove the selected node by clicking the "Remove shape" button. The selected node must NOT be the root of the JTree. Your program should display an error message if the root is selected using a message dialog box.

● Expand the selected nested node in the tree by implement the tree selection event handler.