//Package List
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

/**Course: CSci 1130-51
 * Project Description: This is an applet that builds a brick wall using loops based on user input.
 * @author Nandu Chintapalli
 * @version 1.00 2015/9/24
 * website: http://www.geocities.ws/nanducweb/Wall/Wall.html
 */

public class Wall extends JApplet implements ActionListener{

//Component declaration
    JLabel directions;
    JTextField input = new JTextField( 10 );
    JButton go;
    ImageIcon deathclawicon;
    JLabel BackgroundImage;
    //Variable declaration
    int userinput;
    Color randomColor;
    int R;
    int G;
    int B;
    
    //Method declaration
    public void init() 
    {
        getContentPane().setBackground( Color.WHITE );//Changes backround of JApplet to black
        
        this.setLayout(null);
        //adds image into flowlayout
        Image deathclaw = getImage( getCodeBase(), "Deathclaw.jpg" );
        deathclawicon = new ImageIcon( deathclaw );
        BackgroundImage = new JLabel( deathclawicon );
        
        //Set JButton and JLabel
        setLayout( new FlowLayout() );
        directions = new JLabel("Enter in any number between 1 and 20 and hit Go to try to keep the Deathclaw out!.");
        go = new JButton( "Go!" );
        go.setBackground( Color.GREEN );
        go.setFocusPainted( false );
        go.addActionListener( this );
        
        //adds JLabels and buttons and picture into flowlayout
        add (directions );
        add (input); 
        add( go );
        add( BackgroundImage );
    }
    //Key events declaration
     public void actionPerformed( ActionEvent ae )
    {
        String text = input.getText();
        userinput = Integer.parseInt( text );
        //validates if user entered a number with the given parameter
        if (userinput <= 20 )
        {
        repaint();
    }
    else 
    {
        JOptionPane.showMessageDialog(null, "Please choose a number between 1-20" );
    }
}

    //Method declaration 
    public void paint(Graphics g) 
    {
        super.paint(g);
        //integer decleration
        int startX = 20;
        int startY = 480;
        int width = 50;
        int height = 20;
        int spacing = 2;
        int xOffset = 0;
        //for loop to draw bricks
        for (int row = 0; row < userinput; row++) {
            int y = startY - (row * ( height + spacing));
            if ( row % 2 == 0) {
                xOffset = width / 2;
            } else {
                xOffset = 0;
            }
            for (int col = 0; col < 8; col++) {
                int x = xOffset + (startX + (col * (width + spacing)));
                System.out.println(x + "x" + y);
                R = (int)(Math.random()*256);
                G = (int)(Math.random()*256);
                B = (int)(Math.random()*256);
                randomColor = new Color(R, G, B);
                g.setColor( randomColor );
                g.fillRect( x, y, width, height);
                g.setColor( Color.BLACK );
                g.drawRect( x, y, width, height);
                g.drawRect( x, y, width + 1, height +1);
            }
    }
}
}
