package dice;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Yahtzee extends JFrame implements ActionListener
{
//danny the dice
D6 danny;
D6 danny2;
D6 danny3;
D6 danny4;
D6 danny5;
JPanel leftSide; //will have control buttons on left
JPanel rightSide; //will have the dice on the right (D6s)
JButton rollAll; //roll all the dice
public static void main (String [] args)
{
Yahtzee game = new Yahtzee();
}
public Yahtzee()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout() );
//adding the panel
leftSide = new JPanel();
//set background color to light blue
leftSide.setBackground(new Color(200,200, 250));
//saying what side the blue will be on
add(leftSide,BorderLayout.WEST);
//setting things up on the right side
rightSide = new JPanel();
rightSide.setBackground(Color.PINK);
add(rightSide,BorderLayout.EAST);
//the five dice for rolling
danny = new D6();
rightSide.add (danny);
danny2 = new D6();
rightSide.add (danny2);
danny3 = new D6();
rightSide.add (danny3);
danny4 = new D6();
rightSide.add (danny4);
danny5 = new D6();
rightSide.add (danny5);
//rolls all the dice at once
rollAll = new JButton("Roll All");
leftSide.add(rollAll);
rollAll.addActionListener(this);
setSize (800,500);
setVisible(true);
} //end constructor
public void actionPerformed(ActionEvent e)
{
//will roll all of the dice so they all work
danny.roll();
danny2.roll();
danny3.roll();
danny4.roll();
danny5.roll();
repaint();
}
}
Heres program D6
package dice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JPanel;
public class D6 extends JPanel implements ActionListener
{
int face; //the number of dots when it comes to rest
JButton rollMe; //press this button to roll this D6
//constructor
public D6()
{
roll();
rollMe = new JButton("Roll!");
add (rollMe);
rollMe.addActionListener(this);
setBackground(Color.yellow);
setPreferredSize ( new Dimension(100,100));
setVisible(true);
}
//randomize the value of face
public void roll()
{
face = (int)(Math.random()*6+1);
}
//This
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==rollMe){roll();}
repaint();
}
//draws face number of dots
@Override
public void paint (Graphics g )
{
super.paint(g);
g.drawString(""+face,50,50);
}
}