Jump to content

How to put a delay in a While loop Java

TheMidnightNarwhal

It's a school project, we're doing a splash screen. This isn't a program that is going to be sold to people now...

 

Sorry I didn't notice it was a school project. In that case, by all means half-ass it.

Link to comment
Share on other sites

Link to post
Share on other sites

Please dont give advice like that.

Its non deterministic, wastes massive amounts of cpu resources and wont even work when you compile for release (compiler will probably filter it out when you turn on optimizations).

Use the sleep function or even better: no loading bar at all.

Why would you want to let your users wait if there is nothing to load?

It looks like I'm to much used to programm embedded systems with C without an OS. The sleep functions is better for writhing a "serious" programms.

For me it looked like a fun and/or school project and I offered the simplest and most inelegant solution. A busy wait is not that bad for a quick test.

Mineral oil and 40 kg aluminium heat sinks are a perfect combination: 73 cores and a Titan X, Twenty Thousand Leagues Under the Oil

Link to comment
Share on other sites

Link to post
Share on other sites

value = value + 15;loadingbar.setValue(value );  

try this

 

 

 

Ok my code is this and it waits like 2 seconds then goes instnatly to 100.

 

int SHORTWAIT = 10000;int LONGWAIT = 10000 ;     int compteur = 1;int value=0;        while(compteur <24){compteur++;value = value + 15;loadingbar.setValue(value ); for(long i = 0; i < LONGWAIT; i++){for(long j = 0; j < SHORTWAIT; j++); }                }
Link to comment
Share on other sites

Link to post
Share on other sites

Use the sleep function or even better: no loading bar at all.

 

 

Even if I find documentation I can't appear to make it work...

 

 

 

 

 

Why would you want to let your users wait if there is nothing to load?

 

Because it will look cool ha ha.

Link to comment
Share on other sites

Link to post
Share on other sites

 

-snip-
import java.awt.BorderLayout;import javax.swing.JFrame;import javax.swing.JProgressBar;public class pbarTest extends JFrame{	JProgressBar jbar;		public pbarTest()	{		super();				jbar = new JProgressBar();		jbar.setValue(0);		this.add(jbar, BorderLayout.CENTER);				this.setSize(480, 600);		this.setTitle("Animate Progress Bar");		this.setVisible(true);	}		public void animate()	{		for(int i = 0; i<10; i++)		{			jbar.setValue(i*10);			try			{				Thread.sleep(100);			}			catch(InterruptedException ie)			{				System.err.println(ie.getMessage());			}		}			}		public static void main(String[] args)	{		pbarTest pbar = new pbarTest();				pbar.animate();	}}

here is an example. notice how i do not animate the progres bar in the constructor! wait until the window is fully on screen and then do the animation. this is a cheesy example since the progress bar is the only thing on the screen and takes up pretty much the entire window but at least you see that you can not do the animation before you have the window on the screen

Link to comment
Share on other sites

Link to post
Share on other sites

I have a question are you doing a specific project out of a book, or is this where you make something up?

 

What do you mean?

Link to comment
Share on other sites

Link to post
Share on other sites

Well, some dev classes have a book and some don't.  The ones with books tend to have programming challenges for hw out of the book, but some higher ones just are made up projects by the instructor.

 

Oh well I would say it's kinda both...? Because our teacher is giving us notes, well actually pages of his upcoming book he is going to release soon.

Link to comment
Share on other sites

Link to post
Share on other sites

Well, what exactly is the assignment he gave?  Also, what tool are you using?  Eclipse?

 

Well we just doing a game with a splash screen. The loading bar idea was just mine and was wondering how I could do a fake one as he is showing use how to do a real one next class but I was interested in a fake one.

 

Using Netbeans.

Link to comment
Share on other sites

Link to post
Share on other sites

Get some knowledge, which is worth learning. Thanks.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok my code is this and it waits like 2 seconds then goes instnatly to 100.

 

int SHORTWAIT = 10000;int LONGWAIT = 10000 ;     int compteur = 1;int value=0;        while(compteur <24){compteur++;value = value + 15;loadingbar.setValue(value ); for(long i = 0; i < LONGWAIT; i++){for(long j = 0; j < SHORTWAIT; j++); }                }

https://www.youtube.com/watch?v=JncgoPKklVE

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

Try this.

 

try{

 

####YOUR CODE###

Thread.sleep(5000)###5 seconds delay####

}

catch (Exception e)

{

 e.printStackTrace();

}

 

Doing a loop for 10000 will never delay your program the way you want it. It just loops around 10000 and can be done in an instant. So the best way to do this is to use Thread.sleep

Developer by day, Gamer by night

CPU - Intel i7 4770k | MOBO MSI G45 Gaming | RAM - G.Skill RipJaws X 1600mhz 4x4gb CL7 | CASE - NZXT H440 | GPU - MSI R9 290 | PSU - Corsair RM850 | SSD - Samsung 840 EVO 128gb | HDD - Western Digital Black 2TB

Link to comment
Share on other sites

Link to post
Share on other sites

int compteur = 1;
 int value=0;
        
while(compteur <24)
{  
  try

     {
             Thread.sleep(1000); //1000 milliseconds is one second.
     }

catch(InterruptedException ex)

    {
             Thread.currentThread().interrupt();
     }
    compteur
++;
    loadingbar.setValue(value = value + 15);   
}

 

 

edit : only saw the 2nd and 3rd page after posting xD o well ( he asked where he should put it here )

Needs Update

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×