Viewport { public void setPosition(int newX, int newY) { int maxX = world.getWidth() - getWidthTiles(); if (newX >= 0 && newX <= maxX) { x = newX; } else if (newX < 0) { x = 0; } else if (newX > maxX) { x = maxX; } int maxY = world.getHeight() - getHeightTiles(); if (newY >= 0 && newY <= maxY) { y = newY; } else if (newY < 0) { y = 0; } else if (newY > maxY) { y = maxY; } } Viewport(double width, double height, World world); boolean isOnScreen(Drawable item); int getX(); int getY(); Point getPosition(); void setPosition(int newX, int newY); void move(int offsetX, int offsetY); int getWidthTiles(); int getMaxWidthTiles(); int getWidthPixels(); int getHeightTiles(); int getMaxHeightTiles(); int getHeightPixels(); void setWidthTiles(int newWidth); void setMaxWidthTiles(int newMaxWidth); void setHeightTiles(int newHeight); void setMaxHeightTiles(int newMaxHeight); void setSizeTiles(int width, int height); void setWidthPixels(double newWidth); void setHeightPixels(double newHeight); void setMaxWidthPixels(double newMaxWidth); void setMaxHeightPixels(double newMaxHeight);  }
@Test public void testSetPosition() { Viewport viewport = new Viewport( 2 * TileRegister.TILE_SIZE, 2 * TileRegister.TILE_SIZE, world ); viewport.setPosition(5, 5); assertEquals( "Viewport position should be at (5,5)", new Point(5, 5), viewport.getPosition() ); viewport.setPosition(0, 30); assertEquals( "Viewport position should be at (0,18)", new Point(0, 18), viewport.getPosition() ); viewport.setPosition(0, 19); assertEquals( "Viewport position should be at (0,18)", new Point(0, 18), viewport.getPosition() ); viewport.setPosition(30, 0); assertEquals( "Viewport position should be at (18,0)", new Point(18, 0), viewport.getPosition() ); viewport.setPosition(19, 0); assertEquals( "Viewport position should be at (18,0)", new Point(18, 0), viewport.getPosition() ); viewport.setPosition(30, 30); assertEquals( "Viewport position should be at (18,18)", new Point(18, 18), viewport.getPosition() ); viewport.setPosition(19, 19); assertEquals( "Viewport position should be at (18,18)", new Point(18, 18), viewport.getPosition() ); viewport.setPosition(2, -5); assertEquals( "Viewport position should be at (2,0)", new Point(2, 0), viewport.getPosition() ); viewport.setPosition(-5, 10); assertEquals( "Viewport position should be at (0,10)", new Point(0, 10), viewport.getPosition() ); viewport.setPosition(-5, -10); assertEquals("Viewport position should be at (0,0)", new Point(0, 0), viewport.getPosition() ); }