Quote:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyApp extends JFrame
{
private JLabel _aLabel, _comp, _comproll, _comptotal;
private JLabel _user, _userroll, _usertotal;
private JPanel _buttonPanel, _labelPanel1, _labelPanel2, _diePanel;
private JButton roll;
private JButton pass;
private Die _leftDie; // component for one die
private Die _rightDie;
int roll;
public MyApp(String title)
{ super(title);
this.setSize(600,450);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
_aLabel = new JLabel("Pig 2-Dice Java Game");
this.add(_aLabel, java.awt.BorderLayout.NORTH);
_aLabel.setHorizontalAlignment(JLabel.CENTER);
_leftDie = new Die();
_rightDie = new Die();
_diePanel = new JPanel();
_diePanel.setLayout(new GridLayout(0,1));
_diePanel.add(_leftDie);
_diePanel.add(_rightDie);
this.add(_diePanel, java.awt.BorderLayout.CENTER);
_comp = new JLabel("Computer");
_comproll = new JLabel("Round Score: " + roll);
_comptotal = new JLabel("Total Score: ");
_labelPanel1 = new JPanel();
_labelPanel1.setLayout(new GridLayout(0,1));
_labelPanel1.add(_comp);
_labelPanel1.add(_comproll);
_labelPanel1.add(_comptotal);
// put JPanel into ContentPane of Frame
this.add(_labelPanel1, java.awt.BorderLayout.WEST);
_user = new JLabel("User");
_userroll = new JLabel("Round Score: " + roll);
_usertotal = new JLabel("Total Score: " );
// instantiate JPanel and put buttons on JPanel
_labelPanel2 = new JPanel();
_labelPanel2.setLayout(new GridLayout(0,1));
_labelPanel2.add(_user);
_labelPanel2.add(_userroll);
_labelPanel2.add(_usertotal);
// put JPanel into ContentPane of Frame
this.add(_labelPanel2, java.awt.BorderLayout.EAST);
JButton roll = new JButton("Roll");
roll.addActionListener(new RollListener());
JButton pass = new JButton("Pass");
_buttonPanel = new JPanel();
_buttonPanel.setLayout(new GridLayout(1,0));
_buttonPanel.add(roll);
_buttonPanel.add(pass);
this.add(_buttonPanel, java.awt.BorderLayout.SOUTH);
this.setVisible(true);
}
private class RollListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
_leftDie.roll();
_rightDie.roll();
}
}
public static void main(String [] args) {
MyApp myApp = new MyApp("A Game Of Fookin Pig");
}
}
Die.java Quote:
import java.awt.*;
import javax.swing.*;
public class Die extends JComponent {
private static final int SPOT_DIAM = 12;
private static final int a = 120;
private int _faceValue, die1, die2, roll, score;
public Die() {
setPreferredSize(new Dimension(120,120));
roll();
}
public int roll() {
die = (int)(6*Math.random() + 1);
roll = die1 + die2;
repaint();
return roll;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.RED);
g2.fillRect(0, 0, a, a);
g2.setColor(Color.BLACK);
g2.drawRect(0, 0, a-1, a-1);
switch (die1) {
case 1:
drawSpot(g2, a/2, a/2);
break;
case 3:
drawSpot(g2, a/2, a/2);
case 2:
drawSpot(g2, a/4, a/4);
drawSpot(g2, 3*a/4, 3*a/4);
break;
case 5:
drawSpot(g2, a/2, a/2);
case 4:
drawSpot(g2, a/4, a/4);
drawSpot(g2, 3*a/4, 3*a/4);
drawSpot(g2, 3*a/4, a/4);
drawSpot(g2, a/4, 3*a/4);
break;
case 6:
drawSpot(g2, a/4, a/4);
drawSpot(g2, 3*a/4, 3*a/4);
drawSpot(g2, 3*a/4, a/4);
drawSpot(g2, a/4, 3*a/4);
drawSpot(g2, a/4, a/2);
drawSpot(g2, 3*a/4, a/2);
break;
}
}
private void drawSpot(Graphics2D g2, int x, int y) {
g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
}
}
So far, the program shows the layout and the dice generated from pressing the Roll button. I still have a long way to go with the game itself, but kinda hit a wall. Im pretty sure its quite simple and im just missing something.