
/**
 * @(#)Forest.java
 *
 * Forest Applet application
 *
 * @author ....
 * Project Trees
 * @version 1.00 2012/7/16
 * URL:
 * http:/.......
 *
 */

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Color;

public class ForestTemplate extends JApplet implements ActionListener { 

    
    JButton randomButton; 
    JPanel button;
    Random r;
    
    public void init() {
        getContentPane().setBackground( Color.CYAN );
        button = new JPanel(new FlowLayout());
       
       setLayout( new BorderLayout()); 
       
        randomButton= new JButton("Create new forest");
        
       randomButton.addActionListener(this);
        
        button.add(randomButton);
        
        this.add(randomButton,BorderLayout.NORTH);
        
        

    }// end init

    //Method that builds a tree based on random x and y coordinates and random color
    public void paintTree( Graphics g, int x, int y, Color treeColor){

        g.setColor(new Color(130,57,20));
        g.fillRect(x+15,y+30,20,30);
        
        g.setColor((treeColor));
        g.fillOval(x,y,50,50);
        
       
       

 
    
        }//end paintTree
        
        
 public void actionPerformed(ActionEvent event){
       
       
       if(event.getSource()== randomButton){
           repaint();
        }
    }
   
    public void paint(Graphics g) {
        
        super.paint(g);
        
        
        r = new Random();
        //You can write a loop instead of repeating the lines below

        Color treeColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));//generate random color for tree
        g.setColor(treeColor);
        int x=r.nextInt(400);          //generate random x coordinate
        int y=r.nextInt(400);          //generate random y coordinate
        
        paintTree(g,x,y,treeColor);            //call to build tree #1
        
         treeColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));//generate random color for tree
        g.setColor(treeColor);
         x=r.nextInt(400);          //generate random x coordinate
         y=r.nextInt(400);          //generate random y coordinate
        
        paintTree(g,x,y,treeColor);            //call to build tree #1
        
        paintTree(g,x,y,treeColor);            //call to build tree #2
        
         treeColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));//generate random color for tree
        g.setColor(treeColor);
         x=r.nextInt(400);          //generate random x coordinate
         y=r.nextInt(400);          //generate random y coordinate
        
        paintTree(g,x,y,treeColor);            //call to build tree #3
        
         treeColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));//generate random color for tree
        g.setColor(treeColor);
         x=r.nextInt(400);          //generate random x coordinate
         y=r.nextInt(400);          //generate random y coordinate
        
        paintTree(g,x,y,treeColor);            //call to build tree #4
        
         treeColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));//generate random color for tree
        g.setColor(treeColor);
         x=r.nextInt(400);          //generate random x coordinate
         y=r.nextInt(400);          //generate random y coordinate
        
        paintTree(g,x,y,treeColor);            //call to build tree #5
        
         treeColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));//generate random color for tree
        g.setColor(treeColor);
         x=r.nextInt(400);          //generate random x coordinate
         y=r.nextInt(400);          //generate random y coordinate
        
        paintTree(g,x,y,treeColor);            //call to build tree #6
        
         treeColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));//generate random color for tree
        g.setColor(treeColor);
         x=r.nextInt(400);          //generate random x coordinate
         y=r.nextInt(400);          //generate random y coordinate
        
        paintTree(g,x,y,treeColor);            //call to build tree #7
         treeColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));//generate random color for tree
        g.setColor(treeColor);
         x=r.nextInt(400);          //generate random x coordinate
         y=r.nextInt(400);          //generate random y coordinate
        
        paintTree(g,x,y,treeColor);            //call to build tree #8
         treeColor = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));//generate random color for tree
        g.setColor(treeColor);
         x=r.nextInt(400);          //generate random x coordinate
         y=r.nextInt(400);          //generate random y coordinate
        
        paintTree(g,x,y,treeColor);            //call to build tree #9
        
       randomButton.repaint();
        
        
     
        
        

        
    }//end paint

}