2011-05-16 38 views
1

我試圖做涉及一個JPanel內反彈形狀的分配。無論何時形狀碰到一側,它們都會沿另一個方向反彈。我已經有了正常形狀的彈跳部分,但現在我需要製作一個NestingShape。如何在Java中的形狀中嵌套形狀?

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

我不知道,如果提供足夠的上下文,但我有正在實施addremove方法麻煩的部分。當它說「NestingShape和新添加的Shape之間建立了雙向鏈接」時,我不知道該如何去做。我會使用ArrayList還是使用其他形狀的列表?有沒有對我怎麼會去有關實現add方法並在該方法會從這段代碼中調用任何線索?

謝謝。

回答

2

如果我得到這個權利,你其實需要有某種形式的列表(ArrayList的舉例)或有序集兒童的這種嵌套形狀內部,使得嵌套形狀可以有更多的孩子。然後,您可以使用列表的方法來檢查何時添加一個形狀,如果它已包含,並刪除和插入形狀。

當他們說,你建立他們的意思是你應該一些如何告訴插入的形狀,現在它的新的父母是給定的嵌套形式雙向連接。你沒有給我們形狀類的接口(它看起來不像標準的java.awt.Shape,因爲它是一個接口)。你的形狀類做參考莫名其妙父母矩形知道什麼是在它應該移動boundries,所以你應該設置父在插入子捏成嵌套形狀是嵌套狀。從嵌套形狀中刪除形狀時,也應刪除該父子關係。

我需要的形狀類的更多信息,以提供更詳細的答案,但我相信這是你要找的人;)

+0

非常感謝你。那正是我需要的。我可以自己找出其餘的=) – Jigglypuff 2011-05-16 08:01:09