Question
I am having trouble with an android studio proejct. I have one class that lists three different groups : public static final Shape simpleGroup =
I am having trouble with an android studio proejct. I have one class that lists three different groups :
public static final Shape simpleGroup = new Group( new Location(200, 100, new Circle(50)), new Location(400, 300, new Rectangle(100, 50)) ); public static final Shape middleGroup = new Location(50, 100, new Group( new Outline(new Rectangle(50, 30)), new Group( new Circle(20), new Rectangle(50, 30) ) ) ); public static final Shape complexGroup = new Location(50, 100, new Group( new Circle(20), new Rectangle(100, 200), new Location(150, 50, new Stroke(Color.RED, new Fill( new Group( new Rectangle(50, 30), new Outline(new Rectangle(300, 60)), new Stroke(Color.CYAN, new Polygon( new Point(50, 50), new Point(60, 100), new Point(100, 110), new Point(120, 60) ) ), new Location(100, 200, new Stroke(Color.MAGENTA, new Outline(new Circle(50)) ) ) ) ) ) ) ) );
-----------------I have a general "group" class:-------------------
public class Group implements Shape { protected final List extends Shape> shapes; public Group(final Shape... shapes) { this.shapes = Arrays.asList(shapes); } public List extends Shape> getShapes() { return Collections.unmodifiableList(shapes); } @Override publicResult accept(final Visitor v) { return v.onGroup(this); } }
There are two different objectives that I am having trouble figuring out. The first involves creating a "bounding box" or the smallest rectangle that would encompass the group of objects. This is the"bounding box" class that I need to change.
@Override public Location onGroup(final Group g) { return null;
}
I was given a test class that asserts certain things:
@Test public void testGroupSimple() { Location b = simpleGroup.accept(v); Rectangle r = (Rectangle) b.getShape(); assertEquals(150, b.getX()); assertEquals(50, b.getY()); assertEquals(350, r.getWidth()); assertEquals(300, r.getHeight()); } @Test public void testGroupComplex() { final Location b = complexGroup.accept(v); final Rectangle r = (Rectangle) b.getShape(); assertEquals(30, b.getX()); assertEquals(80, b.getY()); assertEquals(470, r.getWidth()); assertEquals(320, r.getHeight());
}
The other problem was with the "draw" class that atually draws the group of shapes.
public class Draw implements Visitor{ private final Canvas canvas; private final Paint paint; public Draw(final Canvas canvas, final Paint paint) { this.canvas = canvas; this.paint = paint; paint.setStyle(Style.STROKE); }
@Override public Void onGroup(final Group g) { return null; }
Again, I have a test class which is as follows:
@Test public void testSimpleGroup() { Fixtures.simpleGroup.accept(draw); inOrder.verify(canvas).translate(200, 100); inOrder.verify(canvas).drawCircle(0, 0, 50, paint); inOrder.verify(canvas).translate(-200, -100); inOrder.verify(canvas).translate(400, 300); inOrder.verify(canvas).drawRect(0, 0, 100, 50, paint); inOrder.verify(canvas).translate(-400, -300); } @Test public void testComplexGroup() { Fixtures.complexGroup.accept(draw); inOrder.verify(canvas).translate(50, 100); inOrder.verify(canvas).drawCircle(0, 0, 20, paint); inOrder.verify(canvas).drawRect(0, 0, 100, 200, paint); inOrder.verify(canvas).translate(150, 50); inOrder.verify(paint).setColor(Color.RED); inOrder.verify(paint).setStyle(Style.FILL_AND_STROKE); inOrder.verify(canvas).drawRect(0, 0, 50, 30, paint); inOrder.verify(paint).setStyle(Style.STROKE); inOrder.verify(canvas).drawRect(0, 0, 300, 60, paint); inOrder.verify(paint).setStyle(any(Style.class)); inOrder.verify(paint).setColor(Color.CYAN); inOrder.verify(canvas).drawLines((float[]) any(), eq(paint)); inOrder.verify(paint).setColor(anyInt()); inOrder.verify(canvas).translate(100, 200); inOrder.verify(paint).setColor(Color.MAGENTA); inOrder.verify(paint).setStyle(Style.STROKE); inOrder.verify(canvas).drawCircle(0, 0, 50, paint); inOrder.verify(paint).setStyle(any(Style.class)); inOrder.verify(paint).setColor(anyInt()); inOrder.verify(canvas).translate(-100, -200); inOrder.verify(paint).setStyle(any(Style.class)); inOrder.verify(paint).setColor(anyInt()); inOrder.verify(canvas).translate(-150, -50); inOrder.verify(canvas).translate(-50, -100); } }
I am having toruble figuring out a general way to code for this. Any help would be great.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started