Results 1 to 15 of 15
Thread: Help with a dice game.
- 07-23-2009, 01:58 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 20
- Rep Power
- 0
Help with a dice game.
I made a little dice game. (below)
But i wonder how to put this in a frame.
Does anyone have any suggestions?
Java Code:import java.util.Random; import java.util.Scanner; public class Dice { public static void main(String args[]){ Scanner input = new Scanner(System.in); int money=400; int offer; int bet; while(money>0 || money>1000){ System.out.println("Your have got "+money+" dollars."); System.out.println("Place your Bet."); offer = input.nextInt(); money = money - offer; System.out.println("Guess wich number the dice will show."); bet = input.nextInt(); Random dice = new Random(); int roll; roll = 1+dice.nextInt(6); System.out.println("You throwed "+roll); if(bet == roll){ offer=offer*2; money=money+offer; } } if(money<0){ System.out.println("You are broke."); } else if(money>1000){ System.out.println("You won!"); } } }
But i have no idea how i could put these together.
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class ImagePanel extends JPanel { BufferedImage image; public ImagePanel(BufferedImage image) { this.image = image; } public void paintComponent(Graphics g) { super.paintComponent(g); int x = (getWidth() - image.getWidth())/2; int y = (getHeight() - image.getHeight())/2; g.drawImage(image, x, y, this); } public static void main(String[] args) throws IOException { String path = "image/dices.jpg"; BufferedImage image = ImageIO.read(new File(path)); ImagePanel contentPane = new ImagePanel(image); contentPane.setOpaque(true); contentPane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.weightx = 1.0; gbc.weighty = 1.0; for(int j = 0; j < 8; j++) { gbc.gridwidth = ((j+1)%2 == 0) ? GridBagConstraints.REMAINDER : GridBagConstraints.RELATIVE; } JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(contentPane); f.setSize(300,300); f.setLocation(200,200); f.setVisible(true); f.setTitle("Dice game"); f.setResizable(false); JPanel panel=new JPanel(); f.add(panel); } }
Last edited by hero; 07-23-2009 at 04:13 PM.
- 07-23-2009, 05:35 PM #2
There is the idea of MVC: model-view-controller.
You have a good start on your model: your Dice game.
You have to imagine how you want to interact with the user or in other words how you want the user to access/use your model/dice game.The tutorial has devices you can use to get user input, show scores, etc.
You can use images or do drawing as you like.
So we need ideas from you...
How many dice do you want to show/use?
Lesson: Using Swing Components
- 07-24-2009, 12:37 AM #3
Member
- Join Date
- Jul 2009
- Posts
- 20
- Rep Power
- 0
I have got one dice in the code.
I would like something similar to this. (below)
I didnt code this. I created it with paint.
image url:
i25.tinypic.com/bhbq12_th.jpgLast edited by hero; 07-24-2009 at 12:41 AM.
- 07-24-2009, 04:35 AM #4
You could use JLabels for the northwest and southeast corner components,
JTextFields for the center cmponents (or maybe a JSpinner or JComboBox for the "which number"/east component) and a JButton for the southwest component.
You could also use the image in the background of the parent panel if you like.
If you're going to draw the die, vis–a–vis use images in a JLabel, you could do that in a JPanel extension and add it to the center of a BorderLayout. You then would have the north and south sections in which to add your control components: JButton, JLabels and JTextFields/input components.
Then you instantiate your Dice class in the main class and allow the user to "use it" via the event listeners added to your control components, eg, the button would gather the data and run the game.
- 07-24-2009, 01:36 PM #5
Member
- Join Date
- Jul 2009
- Posts
- 20
- Rep Power
- 0
Okay, ill try that today. Thanks.
- 07-24-2009, 03:32 PM #6
Member
- Join Date
- Jul 2009
- Posts
- 20
- Rep Power
- 0
I am trying to write the program in one class.
But i have a few errors:
Java Code:Cannot instantiate the type Action The method getText() is undefined for the type JSpinner The method getText() is undefined for the type JSpinner
Or wich should i use if i had JTextFields?
Can anyone explain the error with the ActionListener?
Java Code:JLabel label1 = new JLabel("money: " +money); panel.add(label1); JLabel label2 = new JLabel(); panel.add(label2); JLabel label3 = new JLabel(); panel.add(label3); JButton button = new JButton("go"); panel.add(button); final JSpinner txtfield1 = new JSpinner(); panel.add(txtfield1); final JSpinner txtfield2 = new JSpinner(); panel.add(txtfield2); final int money=400; final int offer; final int bet; final int roll; while(money>1){ button.addActionListener(new Action()); label1.setText("money: " +money); money = money - offer; Random dice = new Random(); roll = 1+dice.nextInt(6); label2.setText("You rolled:" +roll); if(roll == bet){ label3.setText("You won!"); }else{ label3.setText("You lose!"); } } class Action implements ActionListener { public void actionPerformed (ActionEvent e){ offer = txtfield2.getText(); bet = txtfield1.getText(); } }
Last edited by hero; 07-25-2009 at 01:30 AM.
- 07-25-2009, 07:48 PM #7
Wich method should i use to get the numbers out of the JSpinners?
You can use a SpinnerNumberModel for your spinner.
You can get the value of the spinner or of the SpinnerNumberModel
Java Code:int value = ((Integer)spinner.getValue()).intValue(); int value = model.getNumber().intValue();
Or wich should i use if i had JTextFields?
Use the getText method to get the text from a JTextField.
Can anyone explain the error with the ActionListener?
Action is a Java interface. Change the class name to something else, eg, MyListener.
- 07-25-2009, 08:53 PM #8
Member
- Join Date
- Jul 2009
- Posts
- 20
- Rep Power
- 0
The getValue method works. Thanks.
But if i change Action to myListener it still gives an error.
Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problem: MyListener cannot be resolved to a type at ImagePanel.main(ImagePanel.java:81)
Java Code:while(money>0){ button.addActionListener(new MyListener()); label1.setText("money: " +money); money = money - offer; Random dice = new Random(); roll = 1+dice.nextInt(6); label2.setText("You rolled:" +roll); if(roll == bet){ label3.setText("You won!"); }else{ label3.setText("You lose!"); } } class MyListener implements ActionListener { public void actionPerformed (ActionEvent e){ offer = ((Integer)txtfield2.getValue()); bet = ((Integer)txtfield1.getValue()); } }
- 07-25-2009, 09:07 PM #9
Make sure it's not inside a constructor or a method.
It should be declared in class scope:
Java Code:class Pseudo { int offer, bet; public Pseudo() { ... // putting it in here is a problem } class MyListener implements ActionListener { public void actionPerformed (ActionEvent e){ offer = ((Integer)txtfield2.getValue()); bet = ((Integer)txtfield1.getValue()); } } }
- 07-25-2009, 09:24 PM #10
Member
- Join Date
- Jul 2009
- Posts
- 20
- Rep Power
- 0
But how can i acces my spinners if myListener is outside my main method?
- 07-25-2009, 09:29 PM #11
Move the spinner declarations into class scope, as member variables:
Java Code:class Pseudo { JSpinner spinner1; // declaration int offer, bet; public Pseudo() { spinner1 = new JSpinner(... // instantiation ... // putting it in here is a problem } class MyListener implements ActionListener { public void actionPerformed (ActionEvent e){ offer = ((Integer)spinner2.getValue()); bet = ((Integer)spinner1.getValue()); } } }
- 07-25-2009, 09:51 PM #12
Member
- Join Date
- Jul 2009
- Posts
- 20
- Rep Power
- 0
1 error left.
I have no idea how to fix this..
Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type ImagePanel is accessible. Must qualify the allocation with an enclosing instance of type ImagePanel (e.g. x.new A() where x is an instance of ImagePanel). at ImagePanel.main(ImagePanel.java:81)
Java Code:button.addActionListener(new MyListener());
Last edited by hero; 07-25-2009 at 10:04 PM.
- 07-26-2009, 12:32 AM #13Java Code:
class Pseudo { JButton button = new JButton(...); public Pseudo() { button.addActionListener(new MyListener()); } void someMethod() { while(money>0){ //button.addActionListener(new MyListener()); } } class MyListener implements ActionListener { public void actionPerformed (ActionEvent e){ ... } } }
- 07-26-2009, 01:32 AM #14
Member
- Join Date
- Jul 2009
- Posts
- 20
- Rep Power
- 0
Thanks man, now the only this i need to do is arranging my labels, spinners and button in my frame.
- 07-26-2009, 12:50 PM #15
Member
- Join Date
- Jul 2009
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Small Dice Program
By kimmelim in forum New To JavaReplies: 13Last Post: 02-15-2009, 02:01 AM -
Help with dice game...student seeking advice
By waparson in forum New To JavaReplies: 3Last Post: 07-21-2008, 04:31 PM -
Roll 2-Dice "Pig" Game Help
By King8654 in forum AWT / SwingReplies: 7Last Post: 04-07-2008, 07:58 PM -
Help debugging a dice game
By Windoze in forum New To JavaReplies: 7Last Post: 11-22-2007, 02:01 AM -
help debugging a dice game
By Windoze in forum Advanced JavaReplies: 0Last Post: 11-16-2007, 11:28 PM
Bookmarks