//Created by Skrabal, Matt

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class RollDice extends JApplet
	implements ActionListener
{
	private JButton roll;
	private JButton reset;
	private int die1;
	private int die2;
	
  public void init()
  {
    Container c = getContentPane();
    
    roll = new JButton("ROLL");
    roll.addActionListener(this);
    
    reset = new JButton("RESET");
    reset.addActionListener(this);
    
    JPanel panel = new JPanel();
    panel.add(roll);
    panel.add(reset);
    
    c.setBackground(Color.black);
    c.add(panel, BorderLayout.SOUTH);
  }
  
  public void actionPerformed(ActionEvent e)
  {
  	JButton button = (JButton)e.getSource();
  	
  	Random numgen = new Random();
  	
  	//sets dice to number 1-6, notice each die is set independently
  	//this will come in play when drawing the dots
  	if(button == roll)
  	{
  		die1 = numgen.nextInt(6)+1;
  		die2 = numgen.nextInt(6)+1;
  		repaint();
  	}
  	
  	//setting the dice both to 0 will erase the dots
  	//since there is no case for 0
  	else if(button == reset)
  	{
  		die1 = 0;
  		die2 = 0;
  		repaint();
  	}
  	
  }
  
  public void paint(Graphics g)
  {
  	super.paint(g);
  	
  	//units to space the dice
  	//unets(little units) to space the dots on the dice
  	//unoni(big units) to space for the second die
  	final int xunit = getWidth()/5;
  	final int yunit = (getHeight()-33)/3;
  	final int xunet = xunit/9;
  	final int yunet = yunit/9;
  	final int xunoni = xunit*2;
  	
  	//draws die1 outline
  	g.setColor(Color.white);
  	g.drawRect(xunit, yunit, xunit, yunit);
  	//draws die2 outline
  	g.setColor(Color.white);
  	g.drawRect(3 * xunit, yunit, xunit, yunit);
  	
  	//this will set the color of the dots
  	//without having to write it in every case
  	g.setColor(Color.red);
  	
  	//draws die1 dots, from left to right, up to down
  	switch(die1)
  	{
  		case 1:
  		g.fillOval(xunit+(4*xunet), yunit+(4*yunet), xunet, yunet);
  		break;
  		case 2:
  		g.fillOval(xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);
  		break;
  		case 3:
  		g.fillOval(xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunit+(4*xunet), yunit+(4*yunet), xunet, yunet);
  		g.fillOval(xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);
  		break;
  		case 4:
  		g.fillOval(xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunit+(7*xunet), yunit+(1*yunet), xunet, yunet);
  		g.fillOval(xunit+(1*xunet), yunit+(7*yunet), xunet, yunet);
  		g.fillOval(xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);
  		break;
  		case 5:
  		g.fillOval(xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunit+(7*xunet), yunit+(1*yunet), xunet, yunet);  		
  		g.fillOval(xunit+(4*xunet), yunit+(4*yunet), xunet, yunet);
  		g.fillOval(xunit+(1*xunet), yunit+(7*yunet), xunet, yunet);
  		g.fillOval(xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);
  		break;
  		case 6:
  		g.fillOval(xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunit+(7*xunet), yunit+(1*yunet), xunet, yunet);
  		g.fillOval(xunit+(1*xunet), yunit+(4*yunet), xunet, yunet);  		
  		g.fillOval(xunit+(7*xunet), yunit+(4*yunet), xunet, yunet);
  		g.fillOval(xunit+(1*xunet), yunit+(7*yunet), xunet, yunet);
  		g.fillOval(xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);  		
  	}
  	
  	//draws die2 dots, from left to right, up to down
  	switch(die2)
  	{
  		case 1:
  		g.fillOval(xunoni+xunit+(4*xunet), yunit+(4*yunet), xunet, yunet);
  		break;
  		case 2:
  		g.fillOval(xunoni+xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunoni+xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);
  		break;
  		case 3:
  		g.fillOval(xunoni+xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunoni+xunit+(4*xunet), yunit+(4*yunet), xunet, yunet);
  		g.fillOval(xunoni+xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);
  		break;
  		case 4:
  		g.fillOval(xunoni+xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunoni+xunit+(7*xunet), yunit+(1*yunet), xunet, yunet);
  		g.fillOval(xunoni+xunit+(1*xunet), yunit+(7*yunet), xunet, yunet);
  		g.fillOval(xunoni+xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);
  		break;
  		case 5:
  		g.fillOval(xunoni+xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunoni+xunit+(7*xunet), yunit+(1*yunet), xunet, yunet);  		
  		g.fillOval(xunoni+xunit+(4*xunet), yunit+(4*yunet), xunet, yunet);
  		g.fillOval(xunoni+xunit+(1*xunet), yunit+(7*yunet), xunet, yunet);
  		g.fillOval(xunoni+xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);
  		break;
  		case 6:
  		g.fillOval(xunoni+xunit+xunet, yunit+yunet, xunet, yunet);
  		g.fillOval(xunoni+xunit+(7*xunet), yunit+(1*yunet), xunet, yunet);
  		g.fillOval(xunoni+xunit+(1*xunet), yunit+(4*yunet), xunet, yunet);  		
  		g.fillOval(xunoni+xunit+(7*xunet), yunit+(4*yunet), xunet, yunet);
  		g.fillOval(xunoni+xunit+(1*xunet), yunit+(7*yunet), xunet, yunet);
  		g.fillOval(xunoni+xunit+(7*xunet), yunit+(7*yunet), xunet, yunet);  		
  	}
  	
  	//draws the total roll on the dice
  	//only when dice have values other than 0
  	if(die1+die2 != 0)
  	{
  		g.setColor(Color.white);
  		g.drawString(String.valueOf(die1+die2), xunoni+(3*xunet), (2*yunit)+(6*yunet));
  	}
  	
  }
  
  
}


