Java 嵌套在另一个形状中的反弹形状(此代码有什么问题)
我有一个应用程序,它在一个JPanel中反弹Java 嵌套在另一个形状中的反弹形状(此代码有什么问题),java,oop,arraylist,Java,Oop,Arraylist,我有一个应用程序,它在一个JPanel中反弹形状。当形状碰到一侧时,它们会朝另一个方向反弹。我试图添加一个名为NestingShape的新形状,它包含零个或多个shape在其内部反弹,而NestingShape在JPanel中反弹。嵌套形状实例的子对象可以是简单的形状或其他嵌套形状实例 现在,我在使用NestingShape子类中的move(width,height)方法在NestingShape中移动NestingShape的子对象时遇到问题。我在Shape超类中开发一个方法时也遇到了问题,该
形状
。当形状碰到一侧时,它们会朝另一个方向反弹。我试图添加一个名为NestingShape
的新形状,它包含零个或多个shape
在其内部反弹,而NestingShape
在JPanel中反弹。嵌套形状
实例的子对象可以是简单的形状
或其他嵌套形状
实例
现在,我在使用NestingShape子类中的move(width,height)
方法在NestingShape
中移动NestingShape
的子对象时遇到问题。我在Shape
超类中开发一个方法时也遇到了问题,该方法可以在任何给定的形状中找到父对象。我将复制并粘贴我迄今为止为下面的Shape
超类和NestingShape
子类提出的代码,以及我目前用于测试代码的测试用例:
Shape
超类:
注意:parent()和path()方法是与此任务最相关的方法,parent()方法是我在实现时遇到问题的方法。有很多小细节,比如fFill
和count
,这些都与我开发的不同形状相关,可以忽略
package bounce;
import java.awt.Color;
import java.util.List;
/**
* Abstract superclass to represent the general concept of a Shape. This class
* defines state common to all special kinds of Shape instances and implements
* a common movement algorithm. Shape subclasses must override method paint()
* to handle shape-specific painting.
*
* @author wadfsd
*
*/
public abstract class Shape {
// === Constants for default values. ===
protected static final int DEFAULT_X_POS = 0;
protected static final int DEFAULT_Y_POS = 0;
protected static final int DEFAULT_DELTA_X = 5;
protected static final int DEFAULT_DELTA_Y = 5;
protected static final int DEFAULT_HEIGHT = 35;
protected static final int DEFAULT_WIDTH = 25;
protected static final Color DEFAULT_COLOR = Color.black;
protected static final String DEFAULT_STRING = "";
// ===
// === Instance variables, accessible by subclasses.
protected int fX;
protected int fY;
protected int fDeltaX;
protected int fDeltaY;
protected int fWidth;
protected int fHeight;
protected boolean fFill;
protected Color fColor;
protected int count;
protected int fState;
protected int before;
protected String fString;
// ===
/**
* Creates a Shape object with default values for instance variables.
*/
public Shape() {
this(DEFAULT_X_POS, DEFAULT_Y_POS, DEFAULT_DELTA_X, DEFAULT_DELTA_Y, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_COLOR, DEFAULT_STRING);
}
/**
* Creates a Shape object with a specified x and y position.
*/
public Shape(int x, int y) {
this(x, y, DEFAULT_DELTA_X, DEFAULT_DELTA_Y, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_COLOR, DEFAULT_STRING);
}
public Shape(int x, int y, String str) {
this(x, y, DEFAULT_DELTA_X, DEFAULT_DELTA_Y, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_COLOR, str);
}
/**
* Creates a Shape object with specified x, y, and color values.
*/
public Shape(int x, int y, Color c) {
this(x, y, DEFAULT_DELTA_X, DEFAULT_DELTA_Y, DEFAULT_WIDTH, DEFAULT_HEIGHT, c, DEFAULT_STRING);
}
public Shape(int x, int y, Color c, String str) {
this(x, y, DEFAULT_DELTA_X, DEFAULT_DELTA_Y, DEFAULT_WIDTH, DEFAULT_HEIGHT, c, str);
}
/**
* Creates a Shape instance with specified x, y, deltaX and deltaY values.
* The Shape object is created with a default width, height and color.
*/
public Shape(int x, int y, int deltaX, int deltaY) {
this(x, y, deltaX, deltaY, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_COLOR, DEFAULT_STRING);
}
public Shape(int x, int y, int deltaX, int deltaY, String str) {
this(x, y, deltaX, deltaY, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_COLOR, str);
}
/**
* Creates a Shape instance with specified x, y, deltaX, deltaY and color values.
* The Shape object is created with a default width and height.
*/
public Shape(int x, int y, int deltaX, int deltaY, Color c) {
this(x, y, deltaX, deltaY, DEFAULT_WIDTH, DEFAULT_HEIGHT, c, DEFAULT_STRING);
}
public Shape(int x, int y, int deltaX, int deltaY, Color c, String str) {
this(x, y, deltaX, deltaY, DEFAULT_WIDTH, DEFAULT_HEIGHT, c, str);
}
/**
* Creates a Shape instance with specified x, y, deltaX, deltaY, width and
* height values. The Shape object is created with a default color.
*/
public Shape(int x, int y, int deltaX, int deltaY, int width, int height) {
this(x, y, deltaX, deltaY, width, height, DEFAULT_COLOR, DEFAULT_STRING);
}
public Shape(int x, int y, int deltaX, int deltaY, int width, int height, String str) {
this(x, y, deltaX, deltaY, width, height, DEFAULT_COLOR, str);
}
public Shape(int x, int y, int deltaX, int deltaY, int width, int height, Color c) {
this(x, y, deltaX, deltaY, width, height, c, DEFAULT_STRING);
}
/**
* Creates a Shape instance with specified x, y, deltaX, deltaY, width,
* height and color values.
*/
public Shape(int x, int y, int deltaX, int deltaY, int width, int height, Color c, String str) {
fX = x;
fY = y;
fDeltaX = deltaX;
fDeltaY = deltaY;
fWidth = width;
fHeight = height;
fFill = false;
fColor = c;
count = 0;
fState = 1;
before = 0;
fString = str;
}
/**
* Moves this Shape object within the specified bounds. On hitting a
* boundary the Shape instance bounces off and back into the two-
* dimensional world and logs whether a vertical or horizontal wall
* was hit for the DynamicRectangleShape.
* @param width width of two-dimensional world.
* @param height height of two-dimensional world.
*/
public void move(int width, int height) {
int nextX = fX + fDeltaX;
int nextY = fY + fDeltaY;
if (nextY <= 0) {
nextY = 0;
fDeltaY = -fDeltaY;
fFill = false;
count++;
} else if (nextY + fHeight >= height) {
nextY = height - fHeight;
fDeltaY = -fDeltaY;
fFill = false;
count++;
}
// When Shape hits a corner the vertical wall fFill value overrides the horizontal
if (nextX <= 0) {
nextX = 0;
fDeltaX = -fDeltaX;
fFill = true;
count++;
} else if (nextX + fWidth >= width) {
nextX = width - fWidth;
fDeltaX = -fDeltaX;
fFill = true;
count++;
}
fX = nextX;
fY = nextY;
}
public void text(Painter painter, String str) {
painter.drawCentredText(str, fX, fY, fWidth, fHeight);
}
/**
* Returns the NestingShape that contains the Shape that method parent
* is called on. If the callee object is not a child within a
* NestingShape instance this method returns null.
*/
public NestingShape parent() {
// Related to NestingShape
}
/**
* Returns an ordered list of Shape objects. The first item within the
* list is the root NestingShape of the containment hierarchy. The last
* item within the list is the callee object (hence this method always
* returns a list with at least one item). Any intermediate items are
* NestingShapes that connect the root NestingShape to the callee Shape.
* E.g. given:
*
* NestingShape root = new NestingShape();
* NestingShape intermediate = new NestingShape();
* Shape oval = new OvalShape();
* root.add(intermediate);
* intermediate.add(oval);
*
* a call to oval.path() yields: [root,intermediate,oval]
*/
public List<Shape> path() {
// Related to NestingShape
}
/**
* Method to be implemented by concrete subclasses to handle subclass
* specific painting.
* @param painter the Painter object used for drawing.
*/
public abstract void paint(Painter painter);
/**
* Returns this Shape object's x position.
*/
public int x() {
return fX;
}
/**
* Returns this Shape object's y position.
*/
public int y() {
return fY;
}
/**
* Returns this Shape object's speed and direction.
*/
public int deltaX() {
return fDeltaX;
}
/**
* Returns this Shape object's speed and direction.
*/
public int deltaY() {
return fDeltaY;
}
/**
* Returns this Shape's width.
*/
public int width() {
return fWidth;
}
/**
* Returns this Shape's height.
*/
public int height() {
return fHeight;
}
/**
* Returns a String whose value is the fully qualified name of this class
* of object. E.g., when called on a RectangleShape instance, this method
* will return "bounce.RectangleShape".
*/
public String toString() {
return getClass().getName();
}
}
TestNestingShape
测试用例:
package bounce;
import java.util.List;
import junit.framework.TestCase;
/**
* Class to test class NestingShape according to its specification.
*/
public class TestNestingShape extends TestCase {
private NestingShape topLevelNest;
private NestingShape midLevelNest;
private NestingShape bottomLevelNest;
private Shape simpleShape;
public TestNestingShape(String name) {
super(name);
}
/**
* Creates a Shape composition hierarchy with the following structure:
* NestingShape (topLevelNest)
* |
* --- NestingShape (midLevelNest)
* |
* --- NestingShape (bottomLevelNest)
* |
* --- RectangleShape (simpleShape)
*/
protected void setUp() throws Exception {
topLevelNest = new NestingShape(0, 0, 2, 2, 100, 100);
midLevelNest = new NestingShape(0, 0, 2, 2, 50, 50);
bottomLevelNest = new NestingShape(5, 5, 2, 2, 10, 10);
simpleShape = new RectangleShape(1, 1, 1, 1, 5, 5);
midLevelNest.add(bottomLevelNest);
midLevelNest.add(simpleShape);
topLevelNest.add(midLevelNest);
}
/**
* Checks that methods move() and paint() correctly move and paint a
* NestingShape's contents.
*/
public void testBasicMovementAndPainting() {
Painter painter = new MockPainter();
topLevelNest.move(500, 500);
topLevelNest.paint(painter);
assertEquals("(rectangle 2,2,100,100)(rectangle 2,2,50,50)(rectangle 7,7,10,10)(rectangle 2,2,5,5)", painter.toString());
}
/**
* Checks that method add successfuly adds a valid Shape, supplied as
* argument, to a NestingShape instance.
*/
public void testAdd() {
// Check that topLevelNest and midLevelNest mutually reference each other.
assertSame(topLevelNest, midLevelNest.parent());
assertTrue(topLevelNest.contains(midLevelNest));
// Check that midLevelNest and bottomLevelNest mutually reference each other.
assertSame(midLevelNest, bottomLevelNest.parent());
assertTrue(midLevelNest.contains(bottomLevelNest));
}
/**
* Check that method add throws an IlegalArgumentException when an attempt
* is made to add a Shape to a NestingShape instance where the Shape
* argument is already part of some NestingShape instance.
*/
public void testAddWithArgumentThatIsAChildOfSomeOtherNestingShape() {
try {
topLevelNest.add(bottomLevelNest);
fail();
} catch(IllegalArgumentException e) {
// Expected action. Ensure the state of topLevelNest and
// bottomLevelNest has not been changed.
assertFalse(topLevelNest.contains(bottomLevelNest));
assertSame(midLevelNest, bottomLevelNest.parent());
}
}
/**
* Check that method add throws an IllegalArgumentException when an attempt
* is made to add a shape that will not fit within the bounds of the
* proposed NestingShape object.
*/
public void testAddWithOutOfBoundsArgument() {
Shape rectangle = new RectangleShape(80, 80, 2, 2, 50, 50);
try {
topLevelNest.add(rectangle);
fail();
} catch(IllegalArgumentException e) {
// Expected action. Ensure the state of topLevelNest and
// rectangle has not been changed.
assertFalse(topLevelNest.contains(rectangle));
assertNull(rectangle.parent());
}
}
/**
* Check that method remove breaks the two-way link between the Shape
* object that has been removed and the NestingShape it was once part of.
*/
public void testRemove() {
topLevelNest.remove(midLevelNest);
assertFalse(topLevelNest.contains(midLevelNest));
assertNull(midLevelNest.parent());
}
/**
* Check that method shapeAt returns the Shape object that is held at a
* specified position within a NestingShape instance.
*/
public void testShapeAt() {
assertSame(midLevelNest, topLevelNest.shapeAt(0));
}
/**
* Check that method shapeAt throws a IndexOutOfBoundsException when called
* with an invalid index argument.
*/
public void testShapeAtWithInvalidIndex() {
try {
topLevelNest.shapeAt(1);
fail();
} catch(IndexOutOfBoundsException e) {
// Expected action.
}
}
/**
* Check that method shapeCount returns zero when called on a NestingShape
* object without children.
*/
public void testShapeCountOnEmptyParent() {
assertEquals(0, bottomLevelNest.shapeCount());
}
/**
* Check that method shapeCount returns the number of children held within
* a NestingShape instance - where the number of children > 0.
*/
public void testShapeCountOnNonEmptyParent() {
assertEquals(2, midLevelNest.shapeCount());
}
/**
* Check that method indexOf returns the index position within a
* NestingShape instance of a Shape held within the NestingShape.
*/
public void testIndexOfWith() {
assertEquals(0, topLevelNest.indexOf(midLevelNest));
assertEquals(1, midLevelNest.indexOf(simpleShape));
}
/**
* Check that method indexOf returns -1 when called with an argument that
* is not part of the NestingShape callee object.
*/
public void testIndexOfWithNonExistingChild() {
assertEquals(-1, topLevelNest.indexOf(bottomLevelNest));
}
/**
* Check that Shape's path method correctly returns the path from the root
* NestingShape object through to the Shape object that path is called on.
*/
public void testPath() {
List<Shape> path = simpleShape.path();
assertEquals(3, path.size());
assertSame(topLevelNest, path.get(0));
assertSame(midLevelNest, path.get(1));
assertSame(simpleShape, path.get(2));
}
/**
* Check that Shape's path method correctly returns a singleton list
* containing only the callee object when this Shape object has no parent.
*/
public void testPathOnShapeWithoutParent() {
List<Shape> path = topLevelNest.path();
assertEquals(1, path.size());
assertSame(topLevelNest, path.get(0));
}
}
包反弹;
导入java.util.List;
导入junit.framework.TestCase;
/**
*类根据其规范测试类嵌套形状。
*/
公共类TestNestingShape扩展了TestCase{
私人巢穴形状顶层巢穴;
私有巢形中层巢;
私人筑巢形状底部水平巢;
私人形状的简单造型;
公共测试嵌套形状(字符串名称){
超级(姓名);
}
/**
*创建具有以下结构的形状组合层次结构:
*嵌套形状(顶层嵌套)
* |
*---嵌套形状(中层嵌套)
* |
*---嵌套形状(底部水平嵌套)
* |
*---矩形形状(简单形状)
*/
受保护的void setUp()引发异常{
topLevelNest=新的嵌套形状(0,0,2,2100,100);
中层嵌套=新嵌套形状(0,0,2,2,50,50);
bottomLevelNest=新的嵌套形状(5,5,2,2,10,10);
simpleShape=新矩形形状(1,1,1,1,5,5);
middlevelnest.add(bottomLevelNest);
添加(simpleShape);
添加(中间层嵌套);
}
/**
*检查方法move()和paint()是否正确地移动和绘制
*嵌套形状的内容。
*/
公共无效测试基本移动和绘制(){
Painter Painter=新的MockPainter();
顶层嵌套移动(500500);
面漆(油漆工);
assertEquals(“(矩形2,21001000)(矩形2,2,50,50)(矩形7,7,10,10)(矩形2,2,5,5)”,painter.toString();
}
/**
*检查方法add是否成功添加了有效的形状,如所示
*参数设置为嵌套形状实例。
*/
公共无效测试dd(){
//检查topLevelNest和MiddlevelNest是否相互引用。
assertSame(topLevelNest、midLevelNest.parent());
assertTrue(topLevelNest.contains(midLevelNest));
//检查midLevelNest和bottomLevelNest是否相互引用。
assertSame(midLevelNest、bottomLevelNest.parent());
assertTrue(midLevelNest.contains(bottomLevelNest));
}
/**
*检查add方法在尝试时是否引发IlegalArgumentException
*用于将形状添加到嵌套形状实例中,其中
*参数已经是某些NestingShape实例的一部分。
*/
公共无效,以属于其他嵌套形状()之子的论点进行测试{
试一试{
topLevelNest.add(bottomLevelNest);
失败();
}捕获(IllegalArgumentException e){
//预期操作。确保topLevelNest和
//bottomLevelNest尚未更改。
assertFalse(topLevelNest.contains(bottomLevelNest));
assertSame(midLevelNest、bottomLevelNest.parent());
}
}
/**
*检查add方法在尝试时是否引发IllegalArgumentException
*用于添加一个不适合对象边界的形状
*建议的嵌套形状对象。
*/
无边界的公共无效测试(){
形状矩形=新矩形形状(80,80,2,2,50,50);
试一试{
添加(矩形);
失败();
}捕获(IllegalArgumentException e){
//预期操作。确保topLevelNest和
//矩形尚未更改。
assertFalse(topLevelNest.contains(矩形));
assertNull(rectangle.parent());
}
}
/**
*检查“移除”方法是否会断开形状之间的双向链接
*已移除的对象及其曾经是其一部分的嵌套形状。
*/
公共void testRemove(){
顶层嵌套。移除(中层嵌套);
assertFalse(topLevelNest.contains(midLevelNest));
assertNull(midLevelNest.parent());
}
/**
*检查shapeAt方法是否返回保存在
*NestingShape实例中的指定位置。
*/
公共void testShapeAt(){
assertSame(MiddlevelNest、topLevelNest.shapeAt(0));
}
/**
*检查方法shapeAt在调用时是否抛出IndexOutOfBoundsException
*具有无效的索引参数。
*/
public void testShapeAtWithInvalidIndex(){
试一试{
顶部水平槽形槽(1);
失败();
}catch(IndexOutOfBoundsException e){
package bounce;
import java.util.List;
import junit.framework.TestCase;
/**
* Class to test class NestingShape according to its specification.
*/
public class TestNestingShape extends TestCase {
private NestingShape topLevelNest;
private NestingShape midLevelNest;
private NestingShape bottomLevelNest;
private Shape simpleShape;
public TestNestingShape(String name) {
super(name);
}
/**
* Creates a Shape composition hierarchy with the following structure:
* NestingShape (topLevelNest)
* |
* --- NestingShape (midLevelNest)
* |
* --- NestingShape (bottomLevelNest)
* |
* --- RectangleShape (simpleShape)
*/
protected void setUp() throws Exception {
topLevelNest = new NestingShape(0, 0, 2, 2, 100, 100);
midLevelNest = new NestingShape(0, 0, 2, 2, 50, 50);
bottomLevelNest = new NestingShape(5, 5, 2, 2, 10, 10);
simpleShape = new RectangleShape(1, 1, 1, 1, 5, 5);
midLevelNest.add(bottomLevelNest);
midLevelNest.add(simpleShape);
topLevelNest.add(midLevelNest);
}
/**
* Checks that methods move() and paint() correctly move and paint a
* NestingShape's contents.
*/
public void testBasicMovementAndPainting() {
Painter painter = new MockPainter();
topLevelNest.move(500, 500);
topLevelNest.paint(painter);
assertEquals("(rectangle 2,2,100,100)(rectangle 2,2,50,50)(rectangle 7,7,10,10)(rectangle 2,2,5,5)", painter.toString());
}
/**
* Checks that method add successfuly adds a valid Shape, supplied as
* argument, to a NestingShape instance.
*/
public void testAdd() {
// Check that topLevelNest and midLevelNest mutually reference each other.
assertSame(topLevelNest, midLevelNest.parent());
assertTrue(topLevelNest.contains(midLevelNest));
// Check that midLevelNest and bottomLevelNest mutually reference each other.
assertSame(midLevelNest, bottomLevelNest.parent());
assertTrue(midLevelNest.contains(bottomLevelNest));
}
/**
* Check that method add throws an IlegalArgumentException when an attempt
* is made to add a Shape to a NestingShape instance where the Shape
* argument is already part of some NestingShape instance.
*/
public void testAddWithArgumentThatIsAChildOfSomeOtherNestingShape() {
try {
topLevelNest.add(bottomLevelNest);
fail();
} catch(IllegalArgumentException e) {
// Expected action. Ensure the state of topLevelNest and
// bottomLevelNest has not been changed.
assertFalse(topLevelNest.contains(bottomLevelNest));
assertSame(midLevelNest, bottomLevelNest.parent());
}
}
/**
* Check that method add throws an IllegalArgumentException when an attempt
* is made to add a shape that will not fit within the bounds of the
* proposed NestingShape object.
*/
public void testAddWithOutOfBoundsArgument() {
Shape rectangle = new RectangleShape(80, 80, 2, 2, 50, 50);
try {
topLevelNest.add(rectangle);
fail();
} catch(IllegalArgumentException e) {
// Expected action. Ensure the state of topLevelNest and
// rectangle has not been changed.
assertFalse(topLevelNest.contains(rectangle));
assertNull(rectangle.parent());
}
}
/**
* Check that method remove breaks the two-way link between the Shape
* object that has been removed and the NestingShape it was once part of.
*/
public void testRemove() {
topLevelNest.remove(midLevelNest);
assertFalse(topLevelNest.contains(midLevelNest));
assertNull(midLevelNest.parent());
}
/**
* Check that method shapeAt returns the Shape object that is held at a
* specified position within a NestingShape instance.
*/
public void testShapeAt() {
assertSame(midLevelNest, topLevelNest.shapeAt(0));
}
/**
* Check that method shapeAt throws a IndexOutOfBoundsException when called
* with an invalid index argument.
*/
public void testShapeAtWithInvalidIndex() {
try {
topLevelNest.shapeAt(1);
fail();
} catch(IndexOutOfBoundsException e) {
// Expected action.
}
}
/**
* Check that method shapeCount returns zero when called on a NestingShape
* object without children.
*/
public void testShapeCountOnEmptyParent() {
assertEquals(0, bottomLevelNest.shapeCount());
}
/**
* Check that method shapeCount returns the number of children held within
* a NestingShape instance - where the number of children > 0.
*/
public void testShapeCountOnNonEmptyParent() {
assertEquals(2, midLevelNest.shapeCount());
}
/**
* Check that method indexOf returns the index position within a
* NestingShape instance of a Shape held within the NestingShape.
*/
public void testIndexOfWith() {
assertEquals(0, topLevelNest.indexOf(midLevelNest));
assertEquals(1, midLevelNest.indexOf(simpleShape));
}
/**
* Check that method indexOf returns -1 when called with an argument that
* is not part of the NestingShape callee object.
*/
public void testIndexOfWithNonExistingChild() {
assertEquals(-1, topLevelNest.indexOf(bottomLevelNest));
}
/**
* Check that Shape's path method correctly returns the path from the root
* NestingShape object through to the Shape object that path is called on.
*/
public void testPath() {
List<Shape> path = simpleShape.path();
assertEquals(3, path.size());
assertSame(topLevelNest, path.get(0));
assertSame(midLevelNest, path.get(1));
assertSame(simpleShape, path.get(2));
}
/**
* Check that Shape's path method correctly returns a singleton list
* containing only the callee object when this Shape object has no parent.
*/
public void testPathOnShapeWithoutParent() {
List<Shape> path = topLevelNest.path();
assertEquals(1, path.size());
assertSame(topLevelNest, path.get(0));
}
}
private Shape parent = null;
public Shape(Shape parent) {
this.parent = parent;
}
public void setParent(Shape parent) {
this.parent = parent;
}
public Shape parent() {
return parent;
}
public interface ShapeContainer {
public List<Shape> getChildren();
// .. more?
}
public class NestingShape extends Shape implements ShapeContainer