Question: // Can Someone help explain how to run main method I have here 3 classes I can compile 2 of them individual except Dice Driver
// Can Someone help explain how to run main method I have here 3 classes I can compile 2 of them individual except Dice Driver I understand that this has my main method but when I compile it I get error. class interface or enum expected? what am I suppose to run in main to make it compile am i soppuse to initialize a value for it? can someone please explain thanks//
Create a simple one-player GUI dice game using two dice. The player enters a bet in a text field and then clicks a button to enter the bet in text field and roll the dice. If the roll is 7 or 11, the player wins three times the bet. Otherwise, the player loses the bet.
/ * The java code is modified into * three classes called DiceDriver * ,RollDicePanel and Dice. * */ //DiceDriver.java import javax.swing.*; public class DiceDriver { public static void main(String[] args) { JFrame window = new JFrame(); window.setTitle("Dice Demo"); //set size of the window window.setSize(350, 350); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //call add to add instance of RollDicePanel window.add(new RollDicePanel()); //set visible true window.setVisible(true); }//end main }
------------------------------------------------------------------------------------------------------
//RollDicePanel.java import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; public class RollDicePanel extends JPanel { private Dice _left; // component for one Dice private Dice _right; private JButton rollButton; private JPanel dicePanel; public RollDicePanel() { _left=new Dice(); _right=new Dice(); rollButton = new JButton("Roll"); rollButton.setFont(new Font("Sansserif", Font.PLAIN, 24)); rollButton.addActionListener(new RollListener()); dicePanel = new JPanel(); dicePanel.setLayout(new GridLayout(1, 2, 4, 0)); dicePanel.add(_left); dicePanel.add(_right); this.setLayout(new BorderLayout()); this.add(rollButton, BorderLayout.NORTH); this.add(dicePanel , BorderLayout.CENTER); } private class RollListener implements ActionListener { public void actionPerformed(ActionEvent e) { _left.roll(); _right.roll(); } } }
------------------------------------------------------------------------------------------------------
//Dice.java import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.util.Random; import javax.swing.JPanel; public class Dice extends JPanel { private int _value; // value that shows on face of Dice private int _diam = 9; // Diameter of spots private static Random random = new Random(); // random generator
public Dice() { setBackground(Color.white); //-- Preferred size is set, but layout may change it. setPreferredSize(new Dimension(60,60)); roll(); // Set to random initial value } public int roll() { int val = random.nextInt(6) + 1; // Range 1-6 setValue(val); return val; } public int getValue() { return _value; } public void setValue(int spots) { _value = spots; repaint(); // Value has changed, must repaint } 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; } } private void drawSpot(Graphics g, int x, int y) { g.fillOval(x-_diam/2, y-_diam/2, _diam, _diam); }//end drawSpot
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
