Advertisement
Guest User

FieldView

a guest
Feb 6th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. package View;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5.  
  6. import Model.Animal;
  7. import Model.Field;
  8. import Model.SimulationModel;
  9. import Model.Terrain;
  10.  
  11. /**
  12.  * A graphical view of the simulation grid.
  13.  * The view displays a colored rectangle for each location
  14.  * representing its contents. It uses a default background color.
  15.  * Colors for each type of species can be defined using the
  16.  * setColor method.
  17.  *
  18.  * @author Projectgroup 2, Wim, John, and Maurits
  19.  * @version 2011.07.31
  20.  */
  21. public class FieldView extends ViewPanel
  22. {
  23.     // Colors used for empty locations.
  24.     private static final Color EMPTY_COLOR = Color.white;
  25.    
  26.     /**
  27.      * Create a view of the given width and height.
  28.      * @param height The simulation's height.
  29.      * @param width  The simulation's width.
  30.      */
  31.     public FieldView(View view)
  32.     {
  33.         super(view);
  34.     }
  35.    
  36.     @Override
  37.     public void update(SimulationModel simulationModel)
  38.     {
  39.         this.repaint();
  40.         this.preparePaint();
  41.        
  42.         Field field = simulationModel.getField();
  43.         int fieldWidth = field.getWidth();
  44.         int fieldHeight = field.getHeight();
  45.        
  46.         Dimension d =  getSize();
  47.         double panelWidth = d.getWidth();
  48.         double panelHeight = d.getHeight();
  49.        
  50.         this.getGraphics().setColor(new Color(0xDDDDDD));
  51.         this.getGraphics().fillRect(0, 0, (int) panelWidth, (int) panelHeight);
  52.        
  53.         for(int x = 0; x < fieldWidth; x++) {
  54.             for(int y = 0; y < fieldHeight; y++) {
  55.                 Animal animal = field.getAnimalAt(x, y);
  56.                 Terrain terrain = field.getTerrainAt(x, y);
  57.                
  58.                 if(animal != null) {
  59.                     this.getGraphics().setColor(animal.getSpecies().getColor());
  60.                 }
  61.                 else if (terrain != null) {
  62.                     this.getGraphics().setColor(terrain.getColor());
  63.                 }
  64.                 else {
  65.                     this.getGraphics().setColor(EMPTY_COLOR);
  66.                 }
  67.                 this.getGraphics().fillRect(
  68.                         (int) (x * (panelWidth / fieldWidth)),
  69.                         (int) (y * (panelHeight / fieldHeight)),
  70.                         (int) (panelWidth / fieldWidth) - 1,
  71.                         (int) (panelHeight / fieldHeight) - 1
  72.                         );
  73.             }
  74.         }
  75.         //this.getGraphics().drawImage(offScreen,0,0,this);
  76.        
  77.         this.repaint();
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement