Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.GridLayout;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.SwingConstants;
- public class Main {
- private static final int N = 50; //The size of the forest. Assumes forest is a square
- private static Time time;
- private static Forest forest;
- private static JFrame frame;
- static JLabel[][] labels = new JLabel[N][N];
- public static void main(String[] args) {
- init();
- play();
- }
- private static void init(){
- initGUI();
- forest = new Forest(N);
- time = new Time(4800);
- }
- private static void play(){
- while(time.getRemainingTime() > 0 || forest.getTreeCount() != 0){
- updateDisplay();
- Forest.nextMonth();
- time.nextMonth();
- }
- }
- private static void initGUI(){
- frame = new JFrame("Forest");
- GridLayout layout = new GridLayout(N, N);
- frame.setLayout(layout);
- for(int i = 0; i < N; i++){
- for(int j = 0; j < N; j++){
- labels[i][j] = new JLabel("");
- labels[i][j].setHorizontalAlignment(SwingConstants.CENTER);
- labels[i][j].setOpaque(true);
- frame.add(labels[i][j]);
- }
- }
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- frame.setSize(600, 600);
- frame.setResizable(false);
- frame.setLocation(300, 300);
- frame.setVisible(true);
- }
- private static void updateDisplay(){
- for(int i = 0; i < N; i++){
- for(int j = 0; j < N; j++){
- if(forest.getTileAt(i, j) == 'S'){
- labels[i][j].setBackground(new java.awt.Color(175, 255, 190)); //Saplings, Light Green
- } else if(forest.getTileAt(i, j) == 'T'){
- labels[i][j].setBackground(new java.awt.Color(93, 161, 108)); //Trees, Dark Green
- } else if(forest.getTileAt(i, j) == 'E'){
- labels[i][j].setBackground(new java.awt.Color(156, 90, 20)); //Elder trees, Dark Brown
- } else if(forest.getTileAt(i, j) == 'B'){
- labels[i][j].setBackground(new java.awt.Color(0, 0, 0)); //Bears, Black
- } else if(forest.getTileAt(i, j) == 'L'){
- labels[i][j].setBackground(new java.awt.Color(0, 100, 210)); //Lumberjacks, Blue
- } else{
- labels[i][j].setBackground(new java.awt.Color(255, 255, 200)); //Empty space, Pasty colour
- }
- //labels[i][j].setText(forest.getTileAt(i, j) + "");
- }
- }
- try {
- Thread.sleep(1);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement