C代写:COMP2120 Tree-based drawing program

代写一个绘图软件,需要考虑叠加情况。练习OOP中的继承,以及数据结构中的树。

Requirement

In this assignment you’ll write a Swing app that manipulates a hierarchy of rectangles. You’ll be able to do the following operations:

Draw a rectangle by clicking, dragging, then releasing the mouse. If the rectangle you drew doesn’t overlap with any other rectangles, it is added to the drawing. (It will be completely contained within another rectangle).

Delete a rectangle, by right-clicking on it. This operation also results in deleting all rectangles contained within the deleted rectangle.
Each rectangle should be the same color as its siblings, but it doesn’t have to be the same colors as its cousins (i.e. the root node’s grandchildren may be different colors if they’re not siblings).

You’ll need to model your drawing as a tree as follows:

  • Before any rectangle is drawn, there is a root of the tree that represents the entire drawing area.
  • A rectangle x is a child of another rectangle y if x is fully contained within y.
  • Removing a rectangle (a node of the tree) will delete the rectangle along with all its descendant nodes.

Each node should contain:

  • a Rectangle (in the Java standard library), and a color (to draw it)
  • a reference to an ArrayList of its children
  • a reference to its parent (this is useful for removing a node)

Some methods you’ll want/need to implement in the TreeNode class:

  • TreeNode getFullyContainingRectangle(Rectangle r). This method should return the smallest TreeNode that completely contains r (note that the rectangle could be fully contained within nodes along the path from root), or null if r partially intersects any rectangles. You may want to use the Rectangle class’s contains() and intersects() methods. You’ll want to use recursion in this method.
  • TreeNode getContainer(Point p): return the treeNode that contains this point. This should be the node furthest from the root (which of course should contain all points). Again, you’ll want to employ recursion.
  • void draw(Graphics g). Draw the rectangle and all of its children. This method should use (you guessed it) recursion. This method will use one of the traversal strategies we discuss in class.
  • void remove(). Remove this node from its parent. If the node is the root, do nothing (we do not want to remove the root node).
  • void addChild(Rectangle) add a new child node for the given rectangle to this node.
  • A Constructor. Think about what parameters will be most convenient to take.

We’ll be using Swing to do the drawing for this assignment. You’ll need to create 2 swing classes: a JFrame for the main window, and a class that inherits from JPanel that will handle drawing and mouse event handling. Your JPanel should store a reference to the root node of your tree (a rectangle that fills the entire window).

Below is a video of the program in action.