Results 1 to 15 of 15
  1. #1
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

    Question 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!"); 
    			} 
    		} 
    	}
    I also have a frame.
    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 03:13 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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

  3. #3
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

    Default

    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.jpg
    Last edited by hero; 07-23-2009 at 11:41 PM.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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.

  5. #5
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

    Default

    Okay, ill try that today. Thanks.

  6. #6
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

    Default

    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
    Wich method should i use to get the numbers out of the JSpinners?
    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 12:30 AM.

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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();
    See the javadocs or tutorial.

    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.

  8. #8
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

    Default

    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());
            	}
            }

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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());
            }
        }
    }

  10. #10
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

    Default

    But how can i acces my spinners if myListener is outside my main method?

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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());
            }
        }
    }

  12. #12
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

    Default

    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)
    this is line 81.
    Java Code:
    button.addActionListener(new MyListener());
    Last edited by hero; 07-25-2009 at 09:04 PM.

  13. #13
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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){
                ...
            }
        }
    }

  14. #14
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

    Default

    Thanks man, now the only this i need to do is arranging my labels, spinners and button in my frame.

  15. #15
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Small Dice Program
    By kimmelim in forum New To Java
    Replies: 13
    Last Post: 02-15-2009, 01:01 AM
  2. Help with dice game...student seeking advice
    By waparson in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 03:31 PM
  3. Roll 2-Dice "Pig" Game Help
    By King8654 in forum AWT / Swing
    Replies: 7
    Last Post: 04-07-2008, 06:58 PM
  4. Help debugging a dice game
    By Windoze in forum New To Java
    Replies: 7
    Last Post: 11-22-2007, 01:01 AM
  5. help debugging a dice game
    By Windoze in forum Advanced Java
    Replies: 0
    Last Post: 11-16-2007, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •