2011-05-16 2 views
1

JPanel에서 도형 바운스와 관련된 과제를 수행하려고합니다. 도형이 측면을 공격 할 때마다 다른 방향으로 튀어 나옵니다. 나는 정상적인 모양을 위해 작동하는 튀는 부분을 이미 가지고 있지만, 이제 NestingShape를 만들어야합니다.Java에서 도형 내에서 도형을 어떻게 중첩시킬 수 있습니까?

NestingShape는 JPanel에서 NestingShape가 바운스하는 동안 0 개 이상의 Shapes가 바운딩되는 사각형입니다. NestingShape 인스턴스의 자식은 RectangleShape 및 OvalShape 객체와 같은 간단한 모양이거나 다른 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의 사양은 다음과 같습니다. "NestingShape와 새로 추가 된 Shape 사이에 양방향 링크가 설정되었습니다."라고 말하면 어떻게해야하는지 모릅니다. ArrayList 또는 List of Shapes 또는 다른 것을 사용합니까? add 메서드를 구현하는 방법과이 코드에서 메서드가 호출되는 위치에 대한 단서가 있습니까?

감사합니다.

답변

2

이 권리를 얻는다면 중첩 모양에 더 많은 자식이 포함될 수 있도록 실제로이 중첩 모양 안에 일련의 목록 (예 : ArrayList) 또는 정렬 된 자식 집합이 있어야합니다. 그런 다음 목록의 메서드를 사용하여 이미 포함되어있는 모양을 추가 할 때를 확인하고 모양을 제거하고 삽입 할 수 있습니다.

양방향 연결을 설정한다고 말하면 삽입 된 모양에 새로운 부모가 주어진 중첩 된 모양임을 알려주는 방법을 사용해야한다는 것을 의미합니다. 셰이프 클래스의 인터페이스를 제공하지 않았습니다 (인터페이스이므로 표준 java.awt.Shape처럼 보이지 않습니다). 셰이프 클래스는 부모 사각형을 어떻게 든 참조하여 이동해야하는 경계선을 알고 있으므로 중첩 모양으로 자식 셰이프를 삽입 할 때 해당 부모를 중첩 셰이프로 설정해야합니다. 또한 중첩 모양에서 모양을 제거 할 때 해당 부모 - 자식 관계를 제거해야합니다.

+0

가 대단히 감사합니다)

나는 더 자세한 답변을 제공하는 형태의 클래스에 대한 추가 정보가 필요합니다,하지만 난 이것이 당신이 찾고 있던 것을 믿습니다. 그것은 내가 필요로했던 바로 그 것이었다. 나는 내 자신의 나머지를 알아낼 수있다 =) – Jigglypuff

관련 문제