在 Java 中如何在形状中嵌套形状?

发布于 2024-11-07 17:54:44 字数 4743 浏览 0 评论 0原文

我正在尝试在 JPanel 中完成一项涉及弹跳形状的作业。每当形状撞击一侧时,它们就会向另一个方向反弹。我已经让弹跳部分适用于正常形状,但现在我需要制作一个 NestingShape。

NestingShape 是一个矩形,包含零个或多个在其内部反弹的 Shape,而 NestingShape 在 JPanel 中围绕 反弹。 NestingShape 实例的子级可以是简单的 Shapes(例如 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
    }
}

我不确定这是否提供了足够的上下文,但我遇到问题的部分是实现 addremove 方法。当它说“在 NestingShape 和新添加的形状之间建立了双向链接”时,我不知道该怎么做。我会使用 ArrayList 或形状列表还是其他东西?关于如何实现 add 方法以及从该代码中调用该方法的位置,是否有任何线索?

谢谢。

I am trying to do an assignment involving bouncing shapes within a JPanel. Whenever shapes hit a side, they will bounce off in the other direction. I have already got the bouncing part working for normal shapes, but now I need to make a NestingShape.

The NestingShape is a rectangle that contains zero or more Shapes that bounce around inside it, while the NestingShape bounces around the in the JPanel. The children of a NestingShape instance can be either simple Shapes, like RectangleShape and OvalShape objects, or other NestingShape instances.

The specification for the NestingShape is below:

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
    }
}

I'm not sure if that provides enough context, but the part I am having trouble with is implementing the add and remove methods. When it says "a two-way link is established between the NestingShape and the newly added Shape", I don't know how I'd go about doing that. Would I use an ArrayList or List of Shapes or something else? Are there any clues on how I'd go about implementing the add method and where the method would be called from within this code?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北渚 2024-11-14 17:54:45

如果我理解正确,那么实际上您确实需要在此嵌套形状内有某种列表(例如 ArrayList)或有序的子项集,以便嵌套形状可以有更多的子项。然后,您可以使用列表的方法在添加形状时检查是否已包含该形状,以及删除和插入形状。

当他们说您建立双向连接时,他们的意思是您应该如何告诉插入的形状,现在它的新父级是给定的嵌套形状。您还没有给我们 shape 类的接口(它看起来不像标准的 java.awt.Shape,因为它是一个接口)。您的形状类确实以某种方式引用父矩形来知道它应该移动的边界是什么,因此您应该在将子形状插入嵌套形状时将该父矩形设置为嵌套形状。从嵌套形状中删除形状后,您还应该删除该父子关系。

我需要有关形状类的更多信息来提供更详细的答案,但我相信这就是您正在寻找的;)

If I get this right, you do in fact need to have some sort of list (ArrayList for example) or ordered-set of children inside this nesting shape, so that the nesting shape could have more children. You can then use the list's method to check when adding a shape if it's already contained, and remove and insert shapes.

When they say you establish a two way connection they mean that you should some how tell the inserted shape that now it's new parent is the given nested shape. You have not given us the interface of the shape class (and it does not look like the standard java.awt.Shape since that is an interface). Your shape class does reference somehow a parent rectangle to know what are the boundries in which it should move, so you should set that parent to be the nesting-shape upon inserting the child shape into the nesting-shape. You should also remove that parent-child relation upon removing the shape from the nesting shape.

I'll need more info on the shape class to provide a more detailed answer, but I believe this is what you were looking for ;)

情何以堪。 2024-11-14 17:54:45

必须需要使用和
真实形状嵌套算法

真实形状嵌套是一种使用更详细的嵌套算法将更多对象放入指定区域的功能。

Must need to use and
True-Shape-Nesting-algorithm

True shape nesting is a feature that uses a more detailed nesting algorithm to fit even more objects into a specified area.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文