Advertisement
Guest User

Untitled

a guest
Jun 8th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. import java.awt.GridLayout;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.SwingConstants;
  6.  
  7.  
  8. public class Main {
  9.  
  10.     private static final int N = 50;    //The size of the forest. Assumes forest is a square
  11.     private static Time time;
  12.     private static Forest forest;
  13.     private static JFrame frame;
  14.     static JLabel[][] labels = new JLabel[N][N];
  15.    
  16.     public static void main(String[] args) {
  17.         init();
  18.         play();
  19.     }
  20.    
  21.     private static void init(){
  22.         initGUI();
  23.         forest = new Forest(N);
  24.         time = new Time(4800);
  25.     }
  26.    
  27.     private static void play(){
  28.         while(time.getRemainingTime() > 0 || forest.getTreeCount() != 0){
  29.             updateDisplay();
  30.             Forest.nextMonth();
  31.             time.nextMonth();
  32.         }
  33.     }
  34.    
  35.     private static void initGUI(){
  36.         frame = new JFrame("Forest");
  37.         GridLayout layout = new GridLayout(N, N);
  38.        
  39.         frame.setLayout(layout);
  40.        
  41.         for(int i = 0; i < N; i++){
  42.             for(int j = 0; j < N; j++){
  43.                 labels[i][j] = new JLabel("");
  44.                 labels[i][j].setHorizontalAlignment(SwingConstants.CENTER);
  45.                 labels[i][j].setOpaque(true);
  46.                 frame.add(labels[i][j]);
  47.             }
  48.         }
  49.        
  50.         frame.pack();
  51.         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  52.         frame.setSize(600, 600);
  53.         frame.setResizable(false);
  54.         frame.setLocation(300, 300);
  55.         frame.setVisible(true);    
  56.     }
  57.    
  58.     private static void updateDisplay(){
  59.         for(int i = 0; i < N; i++){
  60.             for(int j = 0; j < N; j++){
  61.                 if(forest.getTileAt(i, j) == 'S'){
  62.                     labels[i][j].setBackground(new java.awt.Color(175, 255, 190));      //Saplings, Light Green
  63.                 } else if(forest.getTileAt(i, j) == 'T'){
  64.                     labels[i][j].setBackground(new java.awt.Color(93, 161, 108));       //Trees, Dark Green
  65.                 } else if(forest.getTileAt(i, j) == 'E'){
  66.                     labels[i][j].setBackground(new java.awt.Color(156, 90, 20));        //Elder trees, Dark Brown
  67.                 } else if(forest.getTileAt(i, j) == 'B'){
  68.                     labels[i][j].setBackground(new java.awt.Color(0, 0, 0));            //Bears, Black
  69.                 } else if(forest.getTileAt(i, j) == 'L'){
  70.                     labels[i][j].setBackground(new java.awt.Color(0, 100, 210));        //Lumberjacks, Blue
  71.                 } else{
  72.                     labels[i][j].setBackground(new java.awt.Color(255, 255, 200));      //Empty space, Pasty colour
  73.                 }
  74.                 //labels[i][j].setText(forest.getTileAt(i, j) + "");
  75.             }
  76.         }
  77.         try {
  78.             Thread.sleep(1);
  79.         } catch (InterruptedException e) {
  80.             e.printStackTrace();
  81.         }
  82.     }
  83.    
  84.    
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement