i want to delay the entrance of the second die

how to set up a timer

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1788 Views - Last Post: 15 June 2010 - 04:27 PM Rate Topic: -----

#1 abeltre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-June 10

i want to delay the entrance of the second die

Posted 13 June 2010 - 06:35 PM

import java.awt.*;      // AWT = "Abstract Window Toolkit"
import java.awt.Graphics;//
import java.util.Random;
import java.util.Scanner;   

public class game  extends Frame
{
	die myDie, theComputerDie; 
	private closing windowDie;         // this is a variable 


	private int dicevalueOfComputer=0;	
	public game(){			
	}

	public void setup(int someX, int someY, int someValue)        //  Defines the characteristics of a die given a location and a face value. 

	{

		myDie= new die();

		myDie.userValues(someX,someY,  someValue);

		theComputerDie = new die();
		//dicevalueOfComputer=faceValue;

		dicevalueOfComputer=ComputerFaceValue();

		while(dicevalueOfComputer==0)
		{dicevalueOfComputer=ComputerFaceValue();}


		theComputerDie.userValues(dicevalueOfComputer,300,420  );

		setTitle("Playing Dice");  // title of window
		setLocation(780, 200);
		setSize (500,500);
		setBackground(Color.green);
		setVisible(true);
		windowDie = new closing();
		addWindowListener(windowDie);



		/*   Logic for winning the game */
		System.out.println(" ************************************************************ " );
		//System.out.println(" ************************************************************ " );

		int sum_of_diceValue = myDie.dicevaluentered+theComputerDie.dicevaluentered;



		if(myDie.dicevaluentered==theComputerDie.dicevaluentered)
			System.out.println("** This is tie. Try again  ****** " );
		else
		{

			if(sum_of_diceValue%2 == 0)//Value is even

			{
				if(myDie.dicevaluentered > theComputerDie.dicevaluentered)
					System.out.println("Congratulation !!  You have won the game " );
				else
					System.out.println("Sorry  !!  You have loss  the game " );
			}

			else
			{
				if(myDie.dicevaluentered < theComputerDie.dicevaluentered)
					System.out.println("Congratulation !!  You have won the game " );
				else
					System.out.println("Sorry  !  You have loss  the game " );

			}
		}
		//System.out.println(" ************************************************************ " );
		System.out.println(" ************************************************************ " );


		System.out.println(" ******** I hope you  had fun ! Bye !************** " );



	}

	public int ComputerFaceValue()

	{

		Random  Computer_face = new Random(); 
		int faceValue =Computer_face.nextInt(6);//Returns random int >= 0 and < 6
		return faceValue;


	}

	public void paint (Graphics pane)
	{
		/*This switch statement will work as my if statement because 
		 * it will test values like if statements would.
		 *This will take the value that the user entered for the 
		 *number of dots in one of the sides and it will go through 
		 *every case to find the right one.
		 */
		//switch (dicevaluentered)
		int dicevaluentered = myDie.dicevaluentered;
		int dicevalueOfComputer = theComputerDie.dicevaluentered;
		//System.out.println("dicevalueOfComputer " + dicevalueOfComputer );
		switch (dicevalueOfComputer)
		{
		case 1:
		{
			theComputerDie.drawOne(pane);
			theComputerDie.drawBlank(pane);
			break;
		}
		case 2:{
			theComputerDie.drawTwo(pane);
			theComputerDie.drawBlank(pane);

			break;
		}
		case 3:
		{	
			theComputerDie.drawThree(pane);
			theComputerDie.drawBlank(pane);

			break;

		}
		case 4:
		{
			theComputerDie.drawFour(pane);		
			theComputerDie.drawBlank(pane);

			break;
		}
		case 5:
		{
			theComputerDie.drawFive(pane);
			theComputerDie.drawBlank(pane);

			break;
		}
		case 6:
		{
			theComputerDie.drawSix(pane);
			theComputerDie.drawBlank(pane);
			break;
		}
		}
		/*********************************************************      */
		switch (dicevaluentered)
		{
		case 1:
		{
			myDie.drawOne(pane);
			myDie.drawBlank(pane);
			break;
		}
		case 2:{
			myDie.drawTwo(pane);
			myDie.drawBlank(pane);

			break;
		}
		case 3:
		{	
			myDie.drawThree(pane);
			myDie.drawBlank(pane);

			break;

		}
		case 4:
		{
			myDie.drawFour(pane);		
			myDie.drawBlank(pane);

			break;
		}
		case 5:
		{
			myDie.drawFive(pane);
			myDie.drawBlank(pane);

			break;
		}
		case 6:
		{
			myDie.drawSix(pane);
			myDie.drawBlank(pane);
			break;
		}
		}


		/******************************************************************************* */


		// note: use the repaint() method if opening file in a mac 
		// this will repaint the dice the way the user wants it 
	}   							
	// this method is presumably invoke by the system, and 
	// takes care of everything that needs to be drawn



}




/* Programmer: Angel Beltre
 * CSC 211
 * The purpose of the die class is to draw all the things needed 
 * to make the die look realistically
 */	
import java.awt.*;      // AWT = "Abstract Window Toolkit"
import java.awt.Graphics;//



class die  
{

	//private closing windowDie;         // this is a variable 
	private final int WIDTH= 100;		// these are the set values for the  dimensions of the triangle 
	private final int HEIGHT=100;
	//private int dicevaluentered=3;		// initial value before the user enters the dice wanted
	private final int RADIUS= 14;	
	public  int dicevaluentered=0;
	private int startingPointy = 100;	// these are the starting position coordinates 
	private int startingPointx = 100;


	public die()			 // this constructor will draw the window 
	{

	}
	/* uservalues method is use to change current values or 
	 * values that i have predetermined like the starting position and the number
	 * of the dice  
	 */	
	public void userValues(int dicevalue, int positionx, int positiony) 
	{
		startingPointx=positionx;
		startingPointy=positiony;
		dicevaluentered= dicevalue;
	}

	public void drawBlank (Graphics pane)         // Draws a "blank" die in the given frame.
	{
		/*There are two very important lines which will allow the user to increase the size of the box or dice 
		 * but without the dots.
		 */
		pane.setColor(Color.white);
		pane.drawRect (startingPointx,startingPointy,WIDTH,HEIGHT); // draws the front side
		pane.drawLine(startingPointx-20,startingPointy-20,startingPointx,startingPointy);
		pane.drawLine(startingPointx + 80,startingPointy-20,startingPointx-20,startingPointy-20);
		pane.drawLine(startingPointx + WIDTH,startingPointy,startingPointx + 80,startingPointy - 20); //line one 
		pane.drawLine(startingPointx, startingPointy + HEIGHT,startingPointx-20,startingPointy+70);//line two
		pane.drawLine(startingPointx-20,startingPointy+70,startingPointx-20,startingPointy-20);
	}
	/*The following methods draw the right number of dots on the face of the die*/

	public void drawOne(Graphics pane)   // This will allow the paint method to draw one dot in the dice 
	{
		drawDot(pane,startingPointx +50-5,startingPointy +50-5);
		//BOTTOM LEFT (SIDE)
		pane.fillOval(startingPointx -18, startingPointy+60,RADIUS/3, RADIUS);
		pane.fillOval(startingPointx -8, startingPointy+72,RADIUS/3, RADIUS);

		//TOPLEFT  AND  TOPRIGHT (SIDE)
		pane.fillOval(startingPointx -18, startingPointy-8,RADIUS/3, RADIUS);
		pane.fillOval(startingPointx -8, startingPointy+4,RADIUS/3, RADIUS);

		//LEFT BACK AND LEFT FRONT (TOP)
		pane.fillOval(startingPointx -8, startingPointy-18,RADIUS, RADIUS/3);
		pane.fillOval(startingPointx +4, startingPointy-8,RADIUS, RADIUS/3);

		//RIGHT BACK AND RIGHT FRONT (TOP)
		pane.fillOval(startingPointx +65, startingPointy-18,RADIUS, RADIUS/3);
		pane.fillOval(startingPointx +75, startingPointy-8,RADIUS, RADIUS/3);


		//MIDDLE TOP
		pane.fillOval(startingPointx+35,startingPointy-14, RADIUS,RADIUS/3);
	}
	public void drawTwo (Graphics pane)  //This line will draw two dots in the dice                     
	{


		drawDot(pane, startingPointx+15,startingPointy+15);	
		drawDot(pane, startingPointx + 75,startingPointy+75);
		//MIDDLE TOP
		pane.fillOval(startingPointx+35,startingPointy-14, RADIUS,RADIUS/3);
		//BOTTOM LEFT (SIDE)
		pane.fillOval(startingPointx -18, startingPointy+60,RADIUS/3, RADIUS);
		pane.fillOval(startingPointx -8, startingPointy+72,RADIUS/3, RADIUS);

		//TOPLEFT  AND  TOPRIGHT (SIDE)
		pane.fillOval(startingPointx -18, startingPointy-8,RADIUS/3, RADIUS);
		pane.fillOval(startingPointx -8, startingPointy+4,RADIUS/3, RADIUS);


	}
	public  void drawThree (Graphics pane) // This line will draw three dots 
	//(with methods drawOne and drawTwo)
	{

		drawDot(pane, startingPointx+15,startingPointy+15);	
		drawDot(pane, startingPointx + 75,startingPointy+75);
		drawDot(pane,startingPointx +50-5,startingPointy +50-5);
		//MIDDLE TOP
		pane.fillOval(startingPointx+35,startingPointy-14, RADIUS,RADIUS/3);
		//MIDDLE SIDE
		pane.fillOval(startingPointx-15,startingPointy+35, RADIUS/3,RADIUS);
		//LEFT BACK AND LEFT FRONT (TOP)
		pane.fillOval(startingPointx -8, startingPointy-18,RADIUS, RADIUS/3);
		pane.fillOval(startingPointx +4, startingPointy-8,RADIUS, RADIUS/3);

		//RIGHT BACK AND RIGHT FRONT (TOP)
		pane.fillOval(startingPointx +65, startingPointy-18,RADIUS, RADIUS/3);
		pane.fillOval(startingPointx +75, startingPointy-8,RADIUS, RADIUS/3);


	} 
	public  void drawFour (Graphics pane ) // this will draw 4 dots of a dice   
	{

		drawTwo(pane);
		drawDot(pane,startingPointx+75,startingPointy+15);
		drawDot(pane, startingPointx+15,startingPointy+75);
		//MIDDLE SIDE
		pane.fillOval(startingPointx-15,startingPointy+35, RADIUS/3,RADIUS);


	}

	public  void drawFive(Graphics pane) // this line will draw 5 dots of a dice 
	{
		drawDot(pane,startingPointx +50-5,startingPointy +50-5);
		drawDot(pane, startingPointx+15,startingPointy+15);
		drawDot(pane, startingPointx + 75,startingPointy+75);
		drawDot(pane,startingPointx+75,startingPointy+15);
		drawDot(pane, startingPointx+15,startingPointy+75);

		//MIDDLE SIDE
		pane.fillOval(startingPointx-15,startingPointy+35, RADIUS/3,RADIUS);
		//LEFT BACK AND LEFT FRONT (TOP)
		pane.fillOval(startingPointx -8, startingPointy-18,RADIUS, RADIUS/3);
		pane.fillOval(startingPointx +4, startingPointy-8,RADIUS, RADIUS/3);

		//RIGHT BACK AND RIGHT FRONT (TOP)
		pane.fillOval(startingPointx +65, startingPointy-18,RADIUS, RADIUS/3);
		pane.fillOval(startingPointx +75, startingPointy-8,RADIUS, RADIUS/3);

	}
	public  void drawSix(Graphics pane) // this method will draw six dots in the dice 
	{

		drawDot(pane, startingPointx+15,startingPointy+15);
		drawDot(pane, startingPointx + 75,startingPointy+75);
		drawDot(pane,startingPointx+75,startingPointy+15);
		drawDot(pane, startingPointx+15,startingPointy+75);
		drawDot(pane, startingPointx+15,startingPointy+45); 
		drawDot(pane, startingPointx+75,startingPointy+45);
		//LEFT BACK AND LEFT FRONT (TOP)
		pane.fillOval(startingPointx -8, startingPointy-18,RADIUS, RADIUS/3);
		pane.fillOval(startingPointx +4, startingPointy-8,RADIUS, RADIUS/3);

		//RIGHT BACK AND RIGHT FRONT (TOP)
		pane.fillOval(startingPointx +65, startingPointy-18,RADIUS, RADIUS/3);
		pane.fillOval(startingPointx +75, startingPointy-8,RADIUS, RADIUS/3);


		//MIDDLE SIDE
		pane.fillOval(startingPointx-15,startingPointy+35, RADIUS/3,RADIUS);
		//BOTTOM LEFT (SIDE)
		pane.fillOval(startingPointx -18, startingPointy+60,RADIUS/3, RADIUS);
		pane.fillOval(startingPointx -8, startingPointy+72,RADIUS/3, RADIUS);

		//TOPLEFT  AND  TOPRIGHT (SIDE)
		pane.fillOval(startingPointx -18, startingPointy-8,RADIUS/3, RADIUS);
		pane.fillOval(startingPointx -8, startingPointy+4,RADIUS/3, RADIUS);


	}

	// draws one dot, centered at given location
	public  void drawDot (Graphics pane, int centerX, int centerY)
	{
		pane.fillOval(centerX, centerY, RADIUS, RADIUS);

	}




}






//pane.fillOval(150-25,150-25,13,13);
//pane.filloval(startingx-10,startingpointy + 50, r/ number , radius)
//pane.filloval(startingx-10,startingpointy + 50, r , radius/num)




import java.awt.event.*; // Use to import the class of "windows"     

public class closing extends WindowAdapter  
{
	public void windowClosing(WindowEvent event)
	{
		System.exit(0);// 	this is the task that will allow users to close the window
	}
}


/* Programmer: Angel Beltre 
 * 
 */


import java.util.Scanner;    // To define a keyboard 
public class PROGRAM02 
{
	public static void main(String [] args)
	{

		game myGameTable =new game(); 


		Scanner keyboard= new Scanner (System.in);

		int faceval=0;  // variable to hold value entered for dice  

		//die mynewvalues = new die();  // this is a new object created to pass the values the user 
		// Enters here to the die class so it can execute the drawing

		System.out.println("Please enter the face of the dice");

		faceval = keyboard.nextInt();

		// This part will allow the user to enter any number 
		// but if the number is greater or less than a dice number 
		// it will ask again in the if statement

		while (( faceval<1)||(faceval>6))
		{
			System.out.println("Please enter a valid integer:");
			faceval = keyboard.nextInt();
		}

		//this part will ask for the location of the dice in the window

		System.out.println("enter Position of the die.");
		int newdrawingx= keyboard.nextInt();
		int newdrawingy=keyboard.nextInt();
		myGameTable.setup(faceval, newdrawingx, newdrawingy);


	}
}




Welcome at DIC

Please do not post your question in the topic title as it may be truncated
Please :code:

This post has been edited by pbl: 13 June 2010 - 06:43 PM
Reason for edit:: Your indentation is HORRIBLE. Fixed !


Is This A Good Question/Topic? 0
  • +

Replies To: i want to delay the entrance of the second die

#2 Delta62  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 55
  • Joined: 19-February 09

Re: i want to delay the entrance of the second die

Posted 13 June 2010 - 06:42 PM

Be sure to enclose your code in the proper code tags - it will be much more readable.

Take a look at the Function thread.sleep() if you want to pause execution.
http://java.sun.com/...tml#sleep(long)
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i want to delay the entrance of the second die

Posted 13 June 2010 - 06:57 PM

View PostDelta62, on 13 June 2010 - 07:42 PM, said:

Take a look at the Function thread.sleep() if you want to pause execution.

Not a good idea at all with Swing. Unless you really really know what you are doing
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i want to delay the entrance of the second die

Posted 13 June 2010 - 07:44 PM

View Postpbl, on 13 June 2010 - 07:57 PM, said:

View PostDelta62, on 13 June 2010 - 07:42 PM, said:

Take a look at the Function thread.sleep() if you want to pause execution.

Not a good idea at all with Swing. Unless you really really know what you are doing. You desserve a -1 for that reply :)

Where exactly do you want your delay implemented ?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: i want to delay the entrance of the second die

Posted 13 June 2010 - 07:55 PM

@Delta62: As pbl said, Swing and AWT are not Thread-safe. You should use Swing Timer instead.

@abeltre: Take a look at the Swing Timer class. The API provides an example of how to set up. Really, you just need to pass the delay in milliseconds as well as a reference to an ActionListener, and let it go. There are some other functions for starting, stopping, controlling repeat, etc. you may find useful. Also, with Java 1.5 and Java 6, you should really be using Swing components, not AWT components.

@pbl: You deserve a +1 for indenting so much code. :)
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: i want to delay the entrance of the second die

Posted 13 June 2010 - 07:59 PM

View Postmacosxnerd101, on 13 June 2010 - 08:55 PM, said:

@pbl: You deserve a +1 for indenting so much code. :)

Eclipse did it for me and as I am not paid on an hour basis :)
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2719
  • View blog
  • Posts: 11,431
  • Joined: 20-September 08

Re: i want to delay the entrance of the second die

Posted 14 June 2010 - 01:26 AM

You need to use a separate thread, and then use sleep on it, as was mentioned earlier. Attached is a possible implementation

Attached File(s)

  • Attached File  game.txt (6.46K)
    Number of downloads: 156

Was This Post Helpful? -1
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: i want to delay the entrance of the second die

Posted 14 June 2010 - 04:14 AM

@g00se: It's one thing from a newbie, but you should know better than to use Threads with Swing without being very, very careful and using some more advanced tools. Swing Timer is so much easier and safer.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2719
  • View blog
  • Posts: 11,431
  • Joined: 20-September 08

Re: i want to delay the entrance of the second die

Posted 14 June 2010 - 04:23 AM

Quote

but you should know better than to use Threads with Swing


This has nothing to do with Swing
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: i want to delay the entrance of the second die

Posted 14 June 2010 - 04:27 AM

The OP really should be using Swing, not AWT, with Java 1.5 and 6. Regardless, Threading really shouldn't be used with the Java GUI, as with the exception of a few methods, it isn't Thread-safe.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2719
  • View blog
  • Posts: 11,431
  • Joined: 20-September 08

Re: i want to delay the entrance of the second die

Posted 14 June 2010 - 04:31 AM

I'm not sure why the OP is using AWT - perhaps there are good reasons?

There's nothing wrong with the use of the extra thread as i've implemented it
Was This Post Helpful? 0
  • +
  • -

#12 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: i want to delay the entrance of the second die

Posted 14 June 2010 - 04:39 AM

Wiki says this ,

'Most AWT and Swing object methods are not thread safe: invoking them from multiple threads risks thread interference or memory consistency errors[2][3]. To avoid these problems, Swing standards states that all user interface components should be created and accessed only from the AWT event dispatch thread'.

Source
Was This Post Helpful? 1
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2719
  • View blog
  • Posts: 11,431
  • Joined: 20-September 08

Re: i want to delay the entrance of the second die

Posted 14 June 2010 - 04:45 AM

Quote

To avoid these problems, Swing standards states that all user interface components should be created and accessed only from the AWT event dispatch thread


That's not what's being done here. I can assure all of you that it's safe to call repaint from any thread ;)
Was This Post Helpful? 0
  • +
  • -

#14 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1989
  • View blog
  • Posts: 4,133
  • Joined: 11-December 07

Re: i want to delay the entrance of the second die

Posted 14 June 2010 - 04:45 AM

In an example like this I really don't think it matters. The GUI is only used to display some results. It's not as if different threads are accessing and altering data held by the GUI concurrently.

The small exception is showUserDie = true;, the value of which is only checked once that I can see. In the unlikely event that it is altered while the paint() method is running, the repaint will be queued anyway, and it will be time to dhow the user dice anyway.

As a general rule, it's best separate your own threads from the GUI in Java but for a simple example like this, it's really not a problem.
Was This Post Helpful? 1
  • +
  • -

#15 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2719
  • View blog
  • Posts: 11,431
  • Joined: 20-September 08

Re: i want to delay the entrance of the second die

Posted 14 June 2010 - 04:58 AM

Quote

The small exception is showUserDie = true;, the value of which is only checked once that I can see.


Yes. In theory it should be declared as

private volatile boolean showUserDie;


so please amend
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2