1

JPanel内で図形をバウンスさせる割り当てを実行しようとしています。形状が側面に当たると、反対方向に跳ね返ります。バウンス部分はすでに通常の形状で機能していますが、NestingShapeを作成する必要があります。

NestingShapeは、その内部で跳ね返る0個以上のShapeを含む長方形ですが、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
    }
}

それが十分なコンテキストを提供するかどうかはわかりませんが、私が問題を抱えているのは、メソッドaddremoveメソッドの実装です。「NestingShapeと新しく追加されたShapeの間に双方向のリンクが確立されている」と書かれている場合、どうすればよいかわかりません。ArrayListやListofShapesなどを使用しますか?addメソッドの実装方法と、このコード内からメソッドが呼び出される場所についての手がかりはありますか?

ありがとう。

4

2 に答える 2

2

私がこれを正しく理解した場合、実際には、ネストシェイプがより多くの子を持つことができるように、このネストシェイプ内にある種のリスト(たとえば、ArrayList)または順序付きの子のセットが必要です。次に、リストのメソッドを使用して、図形が既に含まれているかどうかを追加するときに確認し、図形を削除して挿入できます。

彼らがあなたが双方向の接続を確立すると言うとき、彼らはあなたが挿入された形に今それが新しい親であることが与えられた入れ子になった形であることをどうにかして伝えるべきであることを意味します。シェイプクラスのインターフェイスを指定していません(これはインターフェイスであるため、標準のjava.awt.Shapeのようには見えません)。シェイプクラスは、移動先の境界を知るために何らかの形で親長方形を参照します。そのため、子シェイプをネストシェイプに挿入するときに、その親をネストシェイプに設定する必要があります。また、ネストされた図形から図形を削除するときに、その親子関係を削除する必要があります。

より詳細な回答を提供するには、形状クラスに関する詳細情報が必要ですが、これがあなたが探していたものだと思います;)

于 2011-05-16T06:31:03.780 に答える
0

True-Shape-Nesting-algorithmを使用する必要があります

トゥルーシェイプネスティングは、より詳細なネスティングアルゴリズムを使用して、指定された領域にさらに多くのオブジェクトを収める機能です。

于 2021-03-05T17:07:18.653 に答える