C代写:COMP2120 Tree-based drawing program

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

Requirement

In this assignment youll write a Swing app that manipulates a hierarchy of rectangles. Youll be able to do the following operations:

Draw a rectangle by clicking, dragging, then releasing the mouse. If the rectangle you drew doesnt 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 doesnt have to be the same colors as its cousins (i.e. the root nodes grandchildren may be different colors if theyre not siblings).

Youll 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 youll 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 classs contains() and intersects() methods. Youll 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, youll 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.

Well be using Swing to do the drawing for this assignment. Youll 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.

]]>

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注