Results 1 to 19 of 19
  1. #1
    fifa4ever is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default Java rolling dice

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class Vb08_03 extends Applet
    implements ActionListener {
    	Button knop, knop2, knop3;
    
    	public void paint( Graphics g )
    	{
    		g.setColor( Color.red );
    		g.fillRoundRect( 450, 120, 100, 100, 30, 20 );
    		g.fillRoundRect( 650, 120, 100, 100, 30, 20 );
    		g.fillRoundRect( 850, 120, 100, 100, 30, 20 );
    		
    		g.setColor( Color.blue );
    		g.fillOval( 475, 140, 10, 10); //linksboven
    		g.fillOval( 518, 140, 10, 10); //rechtsboven
    		g.fillOval( 475, 187, 10, 10); //linksonder
    		g.fillOval( 518, 187, 10, 10); //rechtsonder
    		g.fillOval( 475, 163, 10, 10); //linksmiddenin
    		g.fillOval( 518, 163, 10, 10); //rechtsmiddenin
    		g.fillOval( 496, 163, 10, 10); //middenin
    	}
    	public void init() {
    		setLayout( null ); // geen standaard layout
    		knop = new Button ( "Werp dobbelsteen 1" );	
    		knop.setBounds( 440, 250, 120, 30 );
    		add( knop );
    		
    		knop2 = new Button ( "Werp dobbelsteen 2" );
    		knop2.setBounds( 640, 250, 120, 30 );
    		add( knop2 );
    		
    		knop3 = new Button ( "Werp dobbelsteen 3" );
    		knop3.setBounds( 840, 250, 120, 30 );
    		knop3.addActionListener( this );
    		add( knop3 );
    			
    		setBackground( Color.yellow );
    	}
    	
    	public void actionPerformed( ActionEvent e ) {
    		if( e.getSource() == knop3 ) {
    			g.fillOval( 518, 140, 10, 10); //rechtsboven
    			g.fillOval( 496, 163, 10, 10); //middenin
    			g.fillOval( 475, 187, 10, 10); //linksonder	
    		}
    	}
    }
    I am trying to make a script for 3 dices. When you click on the button the dice has to roll.
    I tried to make, when you click on "Werp dobbelsteen 3", then dice 3 has to get 3 rounds. But it isnt working.
    Can someone help? Also how do I make that when I click on "Werp dobbelsteen 3", that a random number comes in the dice?

    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) I suggest you use Swing (JPanel, JApplet, JButton) rather than AWT for your graphics library. The Swing library is much more flexible and robust.
    2) Read up on and use the layout managers as they will make it much easier for you to maintain a GUI's layout by offloading the hard work for you.
    3) Create a Dice object, which can extend from JLabel or JPanel if you use Swing that displays a dice and its dots. Give it a public method setNumber(int number) and then use that to change a field of the object, number. Then in Dice's paintComponent method (or paint if you absolutely must use AWT), use that number to decide how and where to draw the dots.
    4) Then all each button has to do is to change the number of the associated Dice object.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    The Random class(or Math.random()) can get you the random numbers you want.
    See the Graphics.drawString() method for how to draw a String.

    dice 3 has to get 3 rounds
    Not sure what this means? Perhaps if I spoke your language and could read what the button says.

  4. #4
    fifa4ever is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Can someone tell me why this script is not working well?
    Maybe someone can tell me the mistake , when you run it.

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class Vb08_03 extends Applet
    implements ActionListener {
    	Button knop, knop2, knop3, resetknop;
    	TextField tekstvak1, tekstvak2, tekstvak3;
    
    	int worp1() {
    		double r;
    		int aantalOgen1;
    		r = Math.random();// willekeurig getal tussen 0 en 1
    		r *= 6; // vermigvuldigen met 6
    		r += 1; // 1 bij op tellen
    		aantalOgen1 = (int) r; //afkappen geheel getal
    		return aantalOgen1;
    	}
    	int worp2() {
    			double w;
    			int aantalOgen2;
    			w = Math.random();
    			w *= 6;
    			w += 1;
    			aantalOgen2 = (int) w;
    			return aantalOgen2;
    	}
    	int worp3() {
    		double t;
    		int aantalOgen3;
    		t = Math.random();
    		t *= 6;
    		t += 1;
    		aantalOgen3 = (int) t;
    		return aantalOgen3;
    	}
    	
    	public void paint( Graphics g )
    	{
    		g.setColor( Color.red ); //dobbelstenen zijn rood
    		g.fillRoundRect( 450, 120, 100, 100, 30, 20 ); //dobbelsteen 1
    		g.fillRoundRect( 650, 120, 100, 100, 30, 20 ); //dobbelsteen 2
    		g.fillRoundRect( 850, 120, 100, 100, 30, 20 ); //dobbelsteen 3
    		
    		{
    			if(worp1() == 1 || worp1() == 3 || worp1() == 5)
    			{
    			g.setColor( Color.green );
    			g.fillOval( 496, 163, 10, 10); //middenin
    			}
    			if(worp1() > 1)
    			{
    				g.setColor( Color.green );
    				g.fillOval( 518, 140, 10, 10); //rechtsboven
    				g.fillOval( 475, 187, 10, 10); //linksonder
    			}
    			if(worp1() > 3)
    			{
    				g.setColor( Color.green );
    				g.fillOval( 518, 187, 10, 10); //rechtsonder
    				g.fillOval( 475, 140, 10, 10); //linksboven
    			}
    			if(worp1() == 6)
    			{
    				g.setColor( Color.green );
    				g.fillOval( 475, 163, 10, 10); //linksmiddenin
    				g.fillOval( 518, 163, 10, 10); //rechtsmiddenin
    			}
    
    		}
    	}
    	
    	public void init() {
    		setLayout( null ); // geen standaard layout
    		
    		resetknop = new Button( "Reset");
    		resetknop.setBounds( 640, 50, 120, 30 );
    		resetknop.addActionListener( this );
    		add( resetknop );
    		
    		
    		knop = new Button ( "Werp dobbelsteen 1" );	
    		knop.setBounds( 440, 250, 120, 30 );
    		knop.addActionListener( this );
    		add( knop );
    		
    		knop2 = new Button ( "Werp dobbelsteen 2" );
    		knop2.setBounds( 640, 250, 120, 30 );
    		knop2.addActionListener( this );
    		add( knop2 );
    		
    		knop3 = new Button ( "Werp dobbelsteen 3" );
    		knop3.setBounds( 840, 250, 120, 30 );
    		knop3.addActionListener( this );
    		add( knop3 );
    		
    		tekstvak1 = new TextField( 12 );
    		tekstvak1.setBounds (440, 300, 120, 30 );
    		add( tekstvak1 );
    		
    		tekstvak2 = new TextField ( 12 );
    		tekstvak2.setBounds ( 640, 300, 120, 30 );
    		add ( tekstvak2 );
    		
    		tekstvak3 = new TextField (12 );
    		tekstvak3.setBounds ( 840, 300, 120, 30);
    		add ( tekstvak3 );
    			
    		setBackground( Color.yellow );
    	}
    		
    	public void actionPerformed( ActionEvent e ) {
    		if( e.getSource() == knop ) {
    			int aantalOgen1;
    			aantalOgen1 = worp1();
    			tekstvak1.setText( "Aantal ogen = " + aantalOgen1 );
    		}
    		if( e.getSource() == knop2 ) {
    			int aantalOgen2;
    			aantalOgen2 = worp2();
    			tekstvak2.setText( "Aantal ogen = " + aantalOgen2 );
    		}
    		if( e.getSource() == knop3 ) {
    			int aantalOgen3;
    			aantalOgen3 = worp3();
    			tekstvak3.setText( "Aantal ogen = " + aantalOgen3 );
    		}
    		if( e.getSource() == resetknop ) {
    			tekstvak1.setText( "Aantal ogen = " );
    			tekstvak2.setText( "Aantal ogen = " );
    			tekstvak3.setText( "Aantal ogen = " );
    		}
    		repaint();
    	}
    
    	}

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    tell me the mistake
    Please describe what the program is supposed to do so we can tell what the mistake is.
    Also could you change the button text's to English?
    Last edited by Norm; 06-10-2010 at 02:40 PM.

  6. #6
    fifa4ever is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Okay I will try, but I am dutch.
    The program must throw 3 dices, with 3 different buttons.
    The textfield must give a random number 1-6 (which works). Then the dice has to give the number of rounds.
    Also the reset button has to reset everything. But I think if the draw works, then the reset works also)

    Translated script:

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class Vb08_03 extends Applet
    implements ActionListener {
    	Button button, button2, button3, resetbutton;
    	TextField textfield1, textfield2, textfield3;
    
    	int throw1() {
    		double r;
    		int aantalOgen1;
    		r = Math.random();// willekeurig getal tussen 0 en 1
    		r *= 6; // vermigvuldigen met 6
    		r += 1; // 1 bij op tellen
    		aantalOgen1 = (int) r; //afkappen geheel getal
    		return aantalOgen1;
    	}
    	int throw2() {
    			double w;
    			int aantalOgen2;
    			w = Math.random();
    			w *= 6;
    			w += 1;
    			aantalOgen2 = (int) w;
    			return aantalOgen2;
    	}
    	int throw3() {
    		double t;
    		int aantalOgen3;
    		t = Math.random();
    		t *= 6;
    		t += 1;
    		aantalOgen3 = (int) t;
    		return aantalOgen3;
    	}
    	
    	public void paint( Graphics g )
    	{
    		g.setColor( Color.red ); //dobbelstenen zijn rood
    		g.fillRoundRect( 450, 120, 100, 100, 30, 20 ); //dobbelsteen 1
    		g.fillRoundRect( 650, 120, 100, 100, 30, 20 ); //dobbelsteen 2
    		g.fillRoundRect( 850, 120, 100, 100, 30, 20 ); //dobbelsteen 3
    		
    		{
    			if(throw1() == 1 || throw1() == 3 || throw1() == 5)
    			{
    			g.setColor( Color.green );
    			g.fillOval( 496, 163, 10, 10); //middenin
    			}
    			if(throw1() > 1)
    			{
    				g.setColor( Color.green );
    				g.fillOval( 518, 140, 10, 10); //rechtsboven
    				g.fillOval( 475, 187, 10, 10); //linksonder
    			}
    			if(throw1() > 3)
    			{
    				g.setColor( Color.green );
    				g.fillOval( 518, 187, 10, 10); //rechtsonder
    				g.fillOval( 475, 140, 10, 10); //linksboven
    			}
    			if(throw1() == 6)
    			{
    				g.setColor( Color.green );
    				g.fillOval( 475, 163, 10, 10); //linksmiddenin
    				g.fillOval( 518, 163, 10, 10); //rechtsmiddenin
    			}
    
    		}
    	}
    	
    	public void init() {
    		setLayout( null ); // geen standaard layout
    		
    		resetbutton = new Button( "Reset");
    		resetbutton.setBounds( 640, 50, 120, 30 );
    		resetbutton.addActionListener( this );
    		add( resetbutton );
    		
    		
    		button = new Button ( "Throw dice 1" );	
    		button.setBounds( 440, 250, 120, 30 );
    		button.addActionListener( this );
    		add( button );
    		
    		button2 = new Button ( "Throw dice 2" );
    		button2.setBounds( 640, 250, 120, 30 );
    		button2.addActionListener( this );
    		add( button2 );
    		
    		button3 = new Button ( "Throw dice 3" );
    		button3.setBounds( 840, 250, 120, 30 );
    		button3.addActionListener( this );
    		add( button3 );
    		
    		textfield1 = new TextField( 12 );
    		textfield1.setBounds (440, 300, 120, 30 );
    		add( textfield1 );
    		
    		textfield2 = new TextField ( 12 );
    		textfield2.setBounds ( 640, 300, 120, 30 );
    		add ( textfield2 );
    		
    		textfield3 = new TextField (12 );
    		textfield3.setBounds ( 840, 300, 120, 30);
    		add ( textfield3 );
    			
    		setBackground( Color.yellow );
    	}
    		
    	public void actionPerformed( ActionEvent e ) {
    		if( e.getSource() == button ) {
    			int aantalOgen1;
    			aantalOgen1 = throw1();
    			textfield1.setText( "number = " + aantalOgen1 );
    		}
    		if( e.getSource() == button2 ) {
    			int aantalOgen2;
    			aantalOgen2 = throw2();
    			textfield2.setText( "number = " + aantalOgen2 );
    		}
    		if( e.getSource() == button3 ) {
    			int aantalOgen3;
    			aantalOgen3 = throw3();
    			textfield3.setText( "number = " + aantalOgen3 );
    		}
    		if( e.getSource() == resetbutton ) {
    			textfield1.setText( "number = " );
    			textfield2.setText( "number = " );
    			textfield3.setText( "number = " );
    		}
    		repaint();
    	}
    
    	}

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Thanks for the translation.
    Ok, you've describe what the program is supposed to do.
    Now can you describe what it doesn't do that you want it to do?
    The more you can describe what the problem is, the easier it is for us to see the problem and help.

  8. #8
    fifa4ever is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    It gives the wrong points in the dice.
    When I throw 4, it doesn't draw four points in the dice.
    And when I click "throw dice 2" , then dice 1 is drawing. (I havent yet made the code to draw the points in dice 2 and 3, because dice1 didnt work.)

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Several comments on your code:
    1) The positions on the screen to draw should NOT be hard coded (eg 500) they should be computed values relative to a base point.
    2) there are three methods that do the same thing: generate a random number 1-6. You only need one
    3) Your code continually gets a new random number as it draws a die. You should only get one and use that.
    worp1() == 1 || worp1() == 3 || worp1() == 5)
    4) Make a class for each die and have it get its next number and draw itself.
    5) The paint() method only has drawing code for one die. See 4) above.

  10. #10
    fifa4ever is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Can you put that points in the script?
    I dont get what I have to change exactly.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    I dont get what I have to change exactly
    .
    I listed 5 items. Which do you need more explanation about?
    For 1. Replace all literals here with variables:

    tekstvak1.setBounds (440, 300, 120, 30 );
    g.fillOval( 518, 140, 10, 10); //rechtsboven
    g.fillOval( 475, 187, 10, 10); //linksonder
    2) Remove worp2() & worp3() Use only worp1()

  12. #12
    fifa4ever is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Hmm okay. I deleted worp2() and worp3().
    How do I make variables? Xpos = ... ? And where to put that.
    Maybe you can put it in the script?
    But I think after this I still have the same error.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Design the GUI by putting it parts of it in different boxes. Then give a value to the x,y of the upper left of each box. Within each box, define the position of the individual parts of the GUI relative to the x,y for the box its in. Then to get the location of an individual part, you add its relative location in the box to the x,y for the box to get the absolute position. Each of the "boxes" could be a class/object. Each would be given its own x,y location and have draw methods that would use them to draw its die.

    A lot of this is done by layout managers. Eventually you'll want to study how to use them.

    I still have the same error
    Please explain.
    Last edited by Norm; 06-10-2010 at 07:30 PM.

  14. #14
    fifa4ever is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    The dices are working here. But the resetbutton (resetknop here in Dutch) isn't working. The button is throwing the dices instead of resetting everything. Maybe you can help with that?

    Java Code:
    import java.applet.*;
    import java.util.Random;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class DrieDobbelstenen extends Applet implements ActionListener {
    	
    	int worp;
    	JTextArea uitkomstVeld;
    	JButton werpKnop;
    	Random dobbelsteen = new Random();
    	JButton resetKnop;
    	
    	public void init()
    	{
    		werpKnop = new JButton("Werp 3 dobbelstenen");
    		werpKnop.addActionListener(this);
    		add(werpKnop);
    		uitkomstVeld = new JTextArea(1,20);
    		add(uitkomstVeld);
    		resetKnop = new JButton("Reset");
    		resetKnop.addActionListener(this);
    		add(resetKnop);
    	}
    	
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    		uitkomstVeld.setText("");
    		g.drawRoundRect(100,100,50,50,20,20);
    		g.drawRoundRect(175,100,50,50,20,20);
    		g.drawRoundRect(250,100,50,50,20,20);
    		int index = 0;
    		int xPos1 = 120;
    		int xPos2 = 135;
    		int xPos3 = 105;
    		while(index < 3)
    		{
    			worp = bepaalWorp();
    			System.out.println(worp);
    			uitkomstVeld.append("worp nr" + (index + 1 ) + " was " + worp +", ");
    			if(worp == 1 || worp == 3 || worp == 5)
    			{
    				g.fillOval(xPos1, 120, 10, 10); //4
    			}
    			if(worp > 1)
    			{
    				g.fillOval(xPos2, 105, 10, 10); // 3
    				g.fillOval(xPos3, 135, 10, 10); // 5
    			}
    			if(worp > 3)
    			{
    				g.fillOval(xPos2, 135, 10, 10); // 7
    				g.fillOval(xPos3, 105, 10, 10); // 1
    			}
    			if(worp == 6) {
    			g.fillOval(xPos1, 105, 10, 10); // 2
    			g.fillOval(xPos1, 135, 10, 10); // 6
    			}
    			index++;
    			xPos1 +=75;
    			xPos2 +=75;
    			xPos3 +=75;
    		}
    	}
    	public int bepaalWorp()
    	{
    		return dobbelsteen.nextInt(6) + 1;
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if( e.getSource() == resetKnop ) {
    			uitkomstVeld.setText( "Waarden gereset" );
    		}
    	{
    		repaint();
    	}
    	
    	}
    }

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Trace where execution goes when the reset button is pressed. Use println() for this.
    Does it go where you want it to go?
    Does the code where it goes do what you want?

  16. #16
    fifa4ever is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    The code is working well. Except the reset button.
    So I have to use println() but where do I have to put that?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Do you understand how listeners work? Your button should have a listener that calls a method when pressed. Where is that listener? Is it being executed? Does it do what its supposed to do?
    Add System.out.println("entered listener"); and System.out.println("Resetting xxx");
    at the appropriate places in your code to see where its executing.

  18. #18
    fifa4ever is offline Member
    Join Date
    Jun 2010
    Posts
    9
    Rep Power
    0

    Default

    Hmm when I did this:

    if(e.getSource() != resetKnop)
    {
    repaint();
    }

    It worked well.
    The textfields resetted well. But the dice not :(

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    use println() to display e.getSource() to see what value it has.
    Also use println() to display resetKnop and compare it with the above.
    But the dice not
    Where is the code to reset the dice? Where is the code to reset the textfields?
    How can one be executed without the other being executed?

Similar Threads

  1. Roll The dice
    By Subhanrukh in forum New To Java
    Replies: 8
    Last Post: 04-19-2010, 08:39 AM
  2. Java Program (rolling the dice)
    By genocist in forum New To Java
    Replies: 8
    Last Post: 03-01-2010, 03:15 PM
  3. Dice game issue- any Java gamblers ableto help?:P
    By mambalamba in forum New To Java
    Replies: 2
    Last Post: 12-17-2009, 06:49 PM
  4. Log4j problem - Logs are rolling into the previous date file
    By vaibhavborole in forum Advanced Java
    Replies: 0
    Last Post: 04-16-2009, 03:33 PM
  5. java help please- two sided dice
    By pat8 in forum Advanced Java
    Replies: 3
    Last Post: 07-01-2008, 02:25 AM

Posting Permissions

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