我正在尝试执行涉及在JPanel中弹跳形状的作业。每当形状撞到一侧时,它们就会向另一个方向反弹。我已经将弹跳部件用于正常形状,但现在我需要制作一个NestingShape。
NestingShape是一个矩形,包含零个或多个在其中反弹的形状,而NestingShape在JPanel中反弹。 NestingShape实例的子节点可以是简单形状,如RectangleShape和OvalShape对象,也可以是其他NestingShape实例。
NestingShape的规范如下:
public class NestingShape extends Shape {
/**
* Creates a NestingShape object with default values for state.
*/
public NestingShape() {
super();
}
/**
* Creates a NestingShape object with specified location values, default values for other
* state items.
*/
public NestingShape(int x, int y) {
super(x,y);
}
/**
* Creates a NestingShape with specified values for location, velocity and direction.
* Non-specified state items take on default values.
*/
public NestingShape(int x, int y, int deltaX, int deltaY) {
super(x,y,deltaX,deltaY);
}
/**
* Creates a NestingShape with specified values for location, velocity, direction, width, and
* height.
*/
public NestingShape(int x, int y, int deltaX, int deltaY, int width, int height) {
super(x,y,deltaX,deltaY,width,height);
}
/**
* Moves a NestingShape object (including its children) with the bounds specified by arguments
* width and height.
*/
public void move(int width, int height) {
//Not yet implemented
}
/**
* Paints a NestingShape object by drawing a rectangle around the edge of its bounding box.
* The NestingShape object's children are then painted.
*/
public void paint(Painter painter) {
painter.drawRect(fX,fY,fWidth,fHeight);
painter.translate(fX,fY);
// Paint children here. Not implemented yet
painter.translate(0,0);
}
/**
* Attempts to add a Shape to a NestingShape object. If successful, a two-way link is
* established between the NestingShape and the newly added Shape. Note that this method
* has package visibility - for reasons that will become apparent in Bounce III.
* @param shape the shape to be added.
* @throws IllegalArgumentException if an attempt is made to add a Shape to a NestingShape
* instance where the Shape argument is already a child within a NestingShape instance. An
* IllegalArgumentException is also thrown when an attempt is made to add a Shape that will
* not fit within the bounds of the proposed NestingShape object.
*/
void add(Shape shape) throws IllegalArgumentException {
// Not implemented yet
}
/**
* Removes a particular Shape from a NestingShape instance. Once removed, the two-way link
* between the NestingShape and its former child is destroyed. This method has no effect if
* the Shape specified to remove is not a child of the NestingShape. Note that this method
* has package visibility - for reasons that will become apparent in Bounce III.
* @param shape the shape to be removed.
*/
void remove(Shape shape) {
// Not implemented yet
}
/**
* Returns the Shape at a specified position within a NestingShape. If the position specified
* is less than zero or greater than the number of children stored in the NestingShape less
* one this method throws an IndexOutOfBoundsException.
* @param index the specified index position.
*/
public Shape shapeAt(int index) throws IndexOutOfBoundsException {
// Not implemented yet
}
/**
* Returns the number of children contained within a NestingShape object. Note this method is
* not recursive - it simply returns the number of children at the top level within the callee
* NestingShape object.
*/
public int shapeCount() {
// Not implemented yet
}
/**
* Returns the index of a specified child within a NestingShape object. If the Shape specified
* is not actually a child of the NestingShape this method returns -1; otherwise the value
* returned is in the range 0 .. shapeCount() - 1.
* @param the shape whose index position within the NestingShape is requested.
*/
public int indexOf(Shape shape) {
// Not implemented yet
}
/**
* Returns true if the shape argument is a child of the NestingShape object on which this method
* is called, false otherwise.
*/
public boolean contains(Shape shape) {
// Not implemented yet
}
}
我不确定这是否提供了足够的上下文,但我遇到问题的部分是实现add
和remove
方法。当它说“在NestingShape和新添加的Shape之间建立了双向链接”时,我不知道我该怎么做。我会使用ArrayList或形状列表或其他东西吗?有没有关于我如何实现add
方法的线索以及从这段代码中调用方法的位置?
感谢。
答案 0 :(得分:2)
如果我做对了,你实际上需要在这个嵌套形状中有一些列表(例如ArrayList)或者有序的子集,这样嵌套形状可以有更多的子元素。然后,您可以使用列表的方法检查添加形状的时间(如果已包含),并删除并插入形状。
当他们说你建立双向连接时,他们意味着你应该告诉插入的形状现在它的新父亲是给定的嵌套形状。你没有给我们shape类的接口(它看起来不像标准的java.awt.Shape,因为它是一个接口)。你的shape类确实以某种方式引用了一个父矩形来知道它应该移动到哪个边界,所以你应该在将子形状插入嵌套形状时将该父元素设置为嵌套形状。您还应该在从嵌套形状中删除形状时删除该父子关系。
我需要有关形状类的更多信息以提供更详细的答案,但我相信这正是您所寻找的;)
答案 1 :(得分:0)
必须需要使用和 真实形状嵌套算法
真实形状嵌套是一种使用更详细的嵌套算法将更多对象放入指定区域的功能。