|
This is a simple program that "rolls" a couple of dice. It is divided into three source files.
|
RollDice.java - the main program / applet1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.awt.*;
import javax.swing.*;
///////////////////////////////////////////////////////// class RollDice
/** RollDice.java -- Program to display two dice and roll them.
@author Fred Swartz
@version 2004-04-20
This can run as an application because it has a main method.
It can also be used as an applet because it extends JApplet.
*/
// <applet code="RollDice.class" height="110" width="160"></applet>
public class RollDice extends JApplet {
//=============================================== applet constructor
/** Applet initialization requires putting the panel in applet.*/
public RollDice() {
this.setContentPane(new RollDicePanel());
}//end constructor
//====================================================== method main
/** Create JFrame and set content pane to a RollDicePanel. */
public static void main(String[] args) {
JFrame window = new JFrame();
window.setTitle("Dice Demo");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new RollDicePanel());
window.pack();
window.show();
}//end main
}//endclass
|
RollDicePanel.java provides the GUI to this demo program1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
// RollDice.java -- Program to display two dice and roll them.
// Fred Swartz - 2003-04-30
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
///////////////////////////////////////////////////////// class RollDice
/** RollDicePanel.java - Panel to build GUI for RollDice application/applet.
It subclasses JPanel to contain two dice and a button to roll them.
@author Fred Swartz
@version 2003-04-20
*/
public class RollDicePanel extends JPanel {
//=============================================== instance variables
private Die _left; // component for one die
private Die _right;
//end instance variables
//====================================================== constructor
/** Create border layout panel with two Die's and one button. */
RollDicePanel() {
//--- Create the dice
_left = new Die();
_right = new Die();
//--- Create the button to roll the dice
JButton rollButton = new JButton("Roll");
rollButton.setFont(new Font("Sansserif", Font.PLAIN, 24));
rollButton.addActionListener(new RollListener());
//--- Create panel for two dice
JPanel dicePanel = new JPanel();
dicePanel.setLayout(new GridLayout(1, 2, 4, 0));
dicePanel.add(_left);
dicePanel.add(_right);
//--- Add components to content pane
this.setLayout(new BorderLayout());
this.add(rollButton, BorderLayout.NORTH);
this.add(dicePanel , BorderLayout.CENTER);
}//end constructor
/////////////////////////////////// inner listener class RollListener
/** Inner listener class for Roll button. */
private class RollListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
_left.roll();
_right.roll();
}
}
}//endclass
|
Die.java implements the graphics and logic of a die1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
import java.awt.*;
import javax.swing.*;
////////////////////////////////////////////////////////////// class Die
/** Die.java - Component to display one die (dice). This class
subclasses JPanel to display the face, and it provides functions
to "roll", giving different random values.
Possible improvements:
<ul>
<li>Use Graphics2 anti-aliasing to smooth the dot edges.</li>
<li>Add a border.</li>
</ul>
@author Fred Swartz
@version 2003-04-30
*/
public class Die extends JPanel {
//=============================================== instance variables
private int _value; // value that shows on face of die
private int _diam = 9; // Diameter of spots
//end instance variables
//====================================================== constructor
/** Initialize to white background and 60x60 pixels. Initial roll.*/
public Die() {
setBackground(Color.white);
//-- Preferred size is set, but layout may change it.
setPreferredSize(new Dimension(60,60));
roll(); // Set to random initial value
}//end constructor
//====================================================== method roll
/** Produce random roll in range 1-6. Causes repaint().
@return Result of roll (1-6).
*/
public int roll() {
int val = (int)(6*Math.random() + 1); // Range 1-6
setValue(val);
return val;
}//end roll
//================================================== method getValue
/** Returns result of last roll.*/
public int getValue() {
return _value;
}//end setValue
//================================================== method setValue
/** Sets the value of the Die. Causes repaint().
@param spots Number from 1-6.
*/
public void setValue(int spots) {
_value = spots;
repaint(); // Value has changed, must repaint
}//end setValue
//============================================ method paintComponent
/** Draws spots of die face. */
public void paintComponent(Graphics g) {
super.paintComponent(g); // required
int w = getWidth();
int h = getHeight(); // should use to resize spots too.
switch (_value) {
case 1: drawSpot(g, w/2, h/2);
break;
case 3: drawSpot(g, w/2, h/2);
// Fall thru to next case
case 2: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
break;
case 5: drawSpot(g, w/2, h/2);
// Fall thru to next case
case 4: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
break;
case 6: drawSpot(g, w/4, h/4);
drawSpot(g, 3*w/4, 3*h/4);
drawSpot(g, 3*w/4, h/4);
drawSpot(g, w/4, 3*h/4);
drawSpot(g, w/4, h/2);
drawSpot(g, 3*w/4, h/2);
break;
}
}//end paintComponent
/** Utility method used by paintComponent(). */
//================================================== method drawSpot
private void drawSpot(Graphics g, int x, int y) {
g.fillOval(x-_diam/2, y-_diam/2, _diam, _diam);
}//end drawSpot
}//end class
|