/* FishCanvas.java */ /* DO NOT CHANGE THIS FILE. */ /* YOUR SUBMISSION MUST WORK CORRECTLY WITH _OUR_ COPY OF THIS FILE. */ import java.awt.*; /** * A FishCanvas object is the canvas upon which the animation is drawn. * * @author Jonathan Shewchuk */ public class FishCanvas extends Canvas { private Ocean currentOcean; // The ocean being animated on this canvas private int cellWidth; // The size of each cell on the screen /** * Construct an unassociated FishCanvas object. * @return the new FishCanvas. */ public FishCanvas() { currentOcean = null; setBackground(Color.white); } /** * setOcean() associates a specific ocean and cell size with a FishCanvas. * @param ocean is the ocean to animate. * @param cellSize tells how wide each cell should appear. */ public void setOcean(Ocean ocean, int cellSize) { currentOcean = ocean; cellWidth = cellSize; } /** * paint() draws an ocean on the FishCanvas. * @param graphics is the Graphics object associated with this FishCanvas. */ public void paint(Graphics graphics) { super.paint(graphics); if (currentOcean != null) { graphics.clearRect(0, 0, currentOcean.width() * cellWidth, currentOcean.height() * cellWidth); // Blank canvas currentOcean.restartCritters(); // Prepare to enumerate all the animals Critter critter = currentOcean.nextCritter(); // First fish or shark while (critter != null) { if (critter.species == Critter.SHARK) { graphics.setColor(Color.red); // Draw a red shark graphics.fillRect(critter.x * cellWidth, critter.y * cellWidth, cellWidth, cellWidth); } else if (critter.species == Critter.FISH) { graphics.setColor(Color.green); // Draw a green fish graphics.fillRect(critter.x * cellWidth, critter.y * cellWidth, cellWidth, cellWidth); } critter = currentOcean.nextCritter(); // Next fish or shark } } } }