Results 1 to 4 of 4
- 03-11-2011, 06:05 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
HELP WITH DICE ROLL PROBLEM (netbeans)
All i need to do is print out the sum of the 2 dice being rolled... it seems fairly easy.. i just don't understand what variable to use to get that number. i've tried calling all the of classes that would seem to work but i always get a different number. this probably shouldn't be in the new to java because its complicated but i am new to java and this is what i get for doing complicated stuff.
so i have this fairly simple to understand class
public class RollDicePanel extends JPanel {
//================================================== ===== instance variables
private Die _leftDie; // component for one die
private Die _rightDie;
//private Die _botDie;
//================================================== ============ constructor
/** Create border layout panel with one button and two dice. */
RollDicePanel() {
//... Create the dice
_leftDie = new Die();
_rightDie = new Die();
this.setLayout(new BorderLayout(5, 5));
this.add(_leftDie , BorderLayout.WEST);
this.add(_rightDie, BorderLayout.EAST);
this.setBorder(BorderFactory.createEmptyBorder(5,5 ,5,5));
}
and the wee bit more complicated Die class that defines the die
public class Die extends JComponent {
//================================================== ============== constants
private static final int SPOT_DIAM = 9; // Diameter of spots
//================================================== ===== instance variables
private int _faceValue; // value that shows on face of die
//================================================== ============ constructor
/** Initialize to white background and 60x60 pixels. Initial roll.*/
public Die() {
//-- Preferred size is set, but layout may change it.
setPreferredSize(new Dimension(70,70));
roll(); // Set to random initial value
}
//================================================== ============ method roll
/** Produce random roll in range 1-6. Causes repaint().
* @return Result of roll (1-6).
*/
public int roll() {
int dice1 = (int)(6*Math.random() + 1);
// Range 1-6
setValue(dice1);
return dice1;
}
//================================================== ======== method getValue
/** Returns result of last roll.*/
public int getValue() {
return _faceValue;
}
//================================================== ======== method setValue
/** Sets the value of the Die. Causes repaint().
* @param spots Number from 1-6.
*/
public void setValue(int spots) {
_faceValue = spots;
repaint(); // Value has changed, must repaint
}
//================================================== == method paintComponent
/** Draws spots of die face. */
@Override public void paintComponent(Graphics g) {
int w = getWidth(); // Get height and width
int h = getHeight();
//... Change to Graphic2D for smoother spots.
Graphics2D g2 = (Graphics2D)g; // See note below
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
//... Paint background
g2.setColor(Color.GREEN);
g2.fillRect(0, 0, w, h);
g2.setColor(Color.BLACK);
g2.drawRect(0, 0, w-1, h-1); // Draw border
switch (_faceValue) {
case 1:
drawSpot(g2, w/2, h/2);
break;
case 3:
drawSpot(g2, w/2, h/2);
// Fall thru to next case
case 2:
drawSpot(g2, w/4, h/4);
drawSpot(g2, 3*w/4, 3*h/4);
break;
case 5:
drawSpot(g2, w/2, h/2);
// Fall thru to next case
case 4:
drawSpot(g2, w/4, h/4);
drawSpot(g2, 3*w/4, 3*h/4);
drawSpot(g2, 3*w/4, h/4);
drawSpot(g2, w/4, 3*h/4);
break;
case 6:
drawSpot(g2, w/4, h/4);
drawSpot(g2, 3*w/4, 3*h/4);
drawSpot(g2, 3*w/4, h/4);
drawSpot(g2, w/4, 3*h/4);
drawSpot(g2, w/4, h/2);
drawSpot(g2, 3*w/4, h/2);
break;
}
}
//================================================== ======== method drawSpot
/** Utility method used by paintComponent(). */
private void drawSpot(Graphics2D g2, int x, int y) {
g2.fillOval(x-SPOT_DIAM/2, y-SPOT_DIAM/2, SPOT_DIAM, SPOT_DIAM);
}
}
Heres My Tester
public class Tester extends JApplet {
public static void main(String[] args) {
JFrame window = new JFrame("Dice Demo");
Die player = new Die();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
window.setContentPane(new RollDicePanel());
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
System.out.println("what do i need here to output the sum of both rolled dice");
}
}
- 03-11-2011, 06:20 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
i put a counter in the switch statement to add up the faces of the die, but i have no clue how to transfer that value back to my tester without it getting changed
-
Where are you supposed to display the sum? In a JLabel that is on the GUI?
- 03-14-2011, 03:55 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
roll back
By haya in forum NetBeansReplies: 1Last Post: 06-07-2010, 07:02 PM -
Roll The dice
By Subhanrukh in forum New To JavaReplies: 8Last Post: 04-19-2010, 08:39 AM -
Need help with java rapid roll game
By blunderblitz in forum New To JavaReplies: 1Last Post: 03-02-2010, 12:36 PM -
Roll dice class with three dices
By nube07 in forum New To JavaReplies: 4Last Post: 07-14-2008, 01:37 AM -
Roll 2-Dice "Pig" Game Help
By King8654 in forum AWT / SwingReplies: 7Last Post: 04-07-2008, 06:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks