Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package View;
- import java.awt.Color;
- import java.awt.Dimension;
- import Model.Animal;
- import Model.Field;
- import Model.SimulationModel;
- import Model.Terrain;
- /**
- * A graphical view of the simulation grid.
- * The view displays a colored rectangle for each location
- * representing its contents. It uses a default background color.
- * Colors for each type of species can be defined using the
- * setColor method.
- *
- * @author Projectgroup 2, Wim, John, and Maurits
- * @version 2011.07.31
- */
- public class FieldView extends ViewPanel
- {
- // Colors used for empty locations.
- private static final Color EMPTY_COLOR = Color.white;
- /**
- * Create a view of the given width and height.
- * @param height The simulation's height.
- * @param width The simulation's width.
- */
- public FieldView(View view)
- {
- super(view);
- }
- @Override
- public void update(SimulationModel simulationModel)
- {
- this.repaint();
- this.preparePaint();
- Field field = simulationModel.getField();
- int fieldWidth = field.getWidth();
- int fieldHeight = field.getHeight();
- Dimension d = getSize();
- double panelWidth = d.getWidth();
- double panelHeight = d.getHeight();
- this.getGraphics().setColor(new Color(0xDDDDDD));
- this.getGraphics().fillRect(0, 0, (int) panelWidth, (int) panelHeight);
- for(int x = 0; x < fieldWidth; x++) {
- for(int y = 0; y < fieldHeight; y++) {
- Animal animal = field.getAnimalAt(x, y);
- Terrain terrain = field.getTerrainAt(x, y);
- if(animal != null) {
- this.getGraphics().setColor(animal.getSpecies().getColor());
- }
- else if (terrain != null) {
- this.getGraphics().setColor(terrain.getColor());
- }
- else {
- this.getGraphics().setColor(EMPTY_COLOR);
- }
- this.getGraphics().fillRect(
- (int) (x * (panelWidth / fieldWidth)),
- (int) (y * (panelHeight / fieldHeight)),
- (int) (panelWidth / fieldWidth) - 1,
- (int) (panelHeight / fieldHeight) - 1
- );
- }
- }
- //this.getGraphics().drawImage(offScreen,0,0,this);
- this.repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement