1   package main;
2   
3   import interfaces.*;
4   import simworldobjects.*;
5   
6   import javax.swing.*;
7   import java.awt.image.*;
8   import java.awt.*;
9   import java.lang.*;
10  import java.util.*;
11  import java.awt.geom.*;
12  
13  /**
14  * Provides a grid display for any SimWorld
15  *
16  * @author Simon Zienkiewicz
17  */
18  public class GridDisplay extends JPanel
19  {
20      /** the width of the simulation world */    
21      private int width;
22      /** the height of the simulation world */    
23      private int height;
24      /** java graphics 2d object */    
25      private Graphics2D g2;
26      /** the horizontal and vertical spacing between grid pixels */    
27      private int gridSize=25;
28      /** a buffered image of the grid */    
29      private BufferedImage gridBuffer;   
30      /** a graphics 2d image */    
31      private Graphics2D bufferG2D;
32      /** a java RescaleOp object */    
33      private RescaleOp op;
34      /** the simulation world */    
35      private BasicSimWorld simWorld;
36      /** the colour of the grid pixels */    
37      private Color gridColor;
38      /** the ground colour of the world */    
39      private Color worldColor;
40      /** if the grid is visible */    
41      private boolean wantGrid;
42      /** the pixel-size of each grid dot */    
43      private int dotSize;
44         
45      /** Sets up the grid display
46       * @param size the horizontal and vertical spacing between grid pixels
47       * @param width the width of the simulation world
48       * @param height the height of the simulation world
49       * @param gridColor the colour of the grid pixels
50       * @param worldColor the ground colour of the world
51       * @param wantGrid if the grid is visible
52       */
53      public GridDisplay(int size, int width, int height, Color gridColor, Color worldColor, boolean wantGrid)
54      {
55          this.gridSize = size;
56          this.width = width;
57          this.height = height;
58          this.simWorld = simWorld;
59          this.gridColor = gridColor;
60          this.worldColor = worldColor;
61          this.wantGrid = wantGrid;
62          
63          
64          gridBuffer = new BufferedImage(width,height,BufferedImage.TYPE_4BYTE_ABGR_PRE);
65          bufferG2D = gridBuffer.createGraphics();
66          op=null;
67  
68          //draw the background/grid
69          this.drawImage();        
70      }
71      /** Updates the size of the grid pixels
72       * @param x the desired size for the grid pixels
73       */    
74      public void updateGridDotSize(int x){
75          this.dotSize = x;
76          this.drawImage();
77      }
78      /** Updates the horizontal and vertical spacing between grid pixels
79       * @param s the new horizontal and vertical spacing between grid pixels
80       */    
81      public void updateGridSize(int s){
82          this.gridSize = s;
83          this.drawImage();
84      }
85      
86      /** Updates the ground colour of the world
87       * @param c the new world ground colour
88       */    
89      public void updateWorldColor(Color c){
90          this.worldColor=c;
91          this.drawImage();
92      }
93      
94      /** Updates the grid colour
95       * @param c the new grid colour
96       */    
97      public void updateGridColor(Color c){
98          this.gridColor = c;
99          this.drawImage();
100     }
101     /** Gets the current state of the grid; displayed or hidden.
102      * @return true if grid is activated, false otherwise
103      */    
104     public boolean getWantGrid(){
105         return this.wantGrid;
106     }
107     /** Sets the current state of the grid; displayed or hidden.
108      * @param s the new status, true to activate, false to hide
109      */    
110     public void setWantGrid(boolean s){
111         this.wantGrid = s;
112         this.drawImage();
113     }
114         
115     /** Draws the grid into a java graphics object which is then saved to a buffered
116      * image for fast displaying and redrawing.
117      */    
118     private void drawImage(){
119         //draws the background
120         bufferG2D.setColor(this.worldColor);
121         bufferG2D.fillRect(0,0,width,height);
122         
123         if(wantGrid){
124             //draws the grid
125             bufferG2D.setColor(this.gridColor);
126             for(int a=0;a*gridSize<=width;a++){
127                 for(int b=0; b*gridSize<=height;b++){
128                     bufferG2D.fillOval((int)(a*gridSize-this.dotSize/2),(int)(b*gridSize-this.dotSize/2), this.dotSize,this.dotSize);
129                 }
130             }
131         }
132    }
133 
134     /** Main repaint method
135      * @param g java graphics object
136      */
137     public void paintComponent(Graphics g)
138     {
139         // paint background
140         g2 = (Graphics2D)g;
141         g2.drawImage(gridBuffer,op,0,0);
142     }
143 }
144     
145