//Created by Skrabal, Matt

import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
import javax.swing.*;
import java.util.*;

public class RollAnyDicev1 extends JApplet implements ActionListener
{
	private JTextField tf_sides;
	private JTextField tf_numdice;
	private JButton roll;
	private JButton clear;
	private JLabel l_sides;
	private JLabel l_numdice;
	private String str_sides;
	private String str_numdice;
	private int int_sides;
	private int int_numdice;
	private JPanel panel;
	
	public void init()
	{
		Container c = getContentPane();
		
		tf_sides = new JTextField(3);
		tf_numdice = new JTextField(3);
		roll = new JButton("ROLL");
			roll.addActionListener(this);
		clear = new JButton("CLEAR");
			clear.addActionListener(this);
		l_sides = new JLabel("Sides");
		l_numdice = new JLabel("Numdice");
				
		panel = new JPanel();
		panel.add(l_sides);
		panel.add(tf_sides);
		panel.add(l_numdice);
		panel.add(tf_numdice);
		panel.add(roll);
		panel.add(clear);
		
		c.setBackground(Color.white);
		c.add(panel, BorderLayout.SOUTH);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton)e.getSource();
		
		if(button == roll)
		{
			str_sides = tf_sides.getText();
			int_sides = Integer.parseInt(str_sides);
			
			str_numdice = tf_numdice.getText();
			int_numdice = Integer.parseInt(str_numdice);
			
			repaint();
		}
		if(button == clear)
		{
			tf_sides.setText("");
			tf_numdice.setText("");
			int_sides = 0;
			int_numdice = 0;
			repaint();
		}
	}

	public void paint(Graphics g)
	{
		super.paint(g);
		
		Random randint = new Random();
		
		for(int i = 1; i < int_numdice+1;i++)
		{
			int rolled = randint.nextInt(int_sides)+1;
			int pos_x;
			int pos_y;
			if((i%5) == 0)
			{
				pos_x = 5;
				pos_y = (i/5);
			}
			else
			{
				pos_x = (i%5);
				pos_y = (i/5)+1;
			}
			
			g.drawString(String.valueOf(rolled), pos_x*100-50, pos_y*100-50);
		}
	}
}

