Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Delay/Wait/Pause in Java + AirBrushing

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Delay/Wait/Pause in Java + AirBrushing

    I have two problems I am trying to solve.

    I am using the program RealJ and JDK 1.6

    (I seem to have a hard time finding tutorials on the internet because of this could anyone recommend a way to search?)

    First thing I am trying to do is make Java wait. I cant seem to find a workable code. And I cant seem to slow it down by simply slugging it with complex number problems) Could anyone tell me what I can use to make Java wait?


    The other thing I am trying to do is AirBrushing (like Microsoft Paint)
    The idea is to draw a random dot in a random location over the area of a circle.
    Its extremely easy for a square..


    public void mouseMoved (MouseEvent e) 
       	{
       	repaint();
       	}
    for(int i=1; i<120; i++)	
    	{	q=(int)(Math.random()*120+1+599);	
    		w=(int)(Math.random()*120+1+399);
     
    		bg.drawRect(q,w,1,1);
    	 }
    This simulates the general idea..but I'm not much for doing math. How would I do it over a circle?
    I figured select a starting point(mousex,mousey) and draw a 1v1 square at a random direction,random distance over a fixed max distance...but I cannot for the life of me figure out how to do this(Help with sin/cos would be good)


  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Delay/Wait/Pause in Java + AirBrushing

    Thread.sleep() -- See the Java API for more details.

    The Java API: Java Platform SE 6
    Last edited by literallyjer; October 27th, 2009 at 07:31 AM.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Delay/Wait/Pause in Java + AirBrushing

    I've tried using that but the problem is I cannot repaint while it is being used as it halts the entire program (except for showStatus(""); I noticed)
    how can I repaint like this?



    if (x>200 && y>200)
    			for(int i=1; i<20; i++)	
    			{
    			if (i%2 != 0)
    			{	
     
    				try
    				{
    					showStatus("a");
    					Thread.sleep(90);
    					 repaint(); 
     
    				}
    				catch(Exception ee) {}
    			}
    			else if (i%2 == 0)
    			{
    				try
    				{
    					showStatus("b");
    					Thread.sleep(90);
     					repaint();
    				}
    				catch(Exception ee) {}
    	                }
    			}

    Each time it sleeps the showStatus changes but it does not repaint.

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Delay/Wait/Pause in Java + AirBrushing

    So are you saying that you want a certain action to happen every 90ms but you want the repaint method to happen all the time?

    // Json

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Delay/Wait/Pause in Java + AirBrushing

    Create a separate thread that will do the action every 90ms, but have your main thread do the re-painting.

  6. #6
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Delay/Wait/Pause in Java + AirBrushing

    For some reason it didn't click that you were using Swing. You have to be really careful when using multiple threads with Swing. I suggest you look into SwingUtilities.invokeLater() and SwingUtilities.invokeAndWait().

  7. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Delay/Wait/Pause in Java + AirBrushing

    I would avoid the multiple threads solution and just have an update method and a repaint method. This is how I write my games btw.

    You call both methods all the time but in the update method you check the time since last update and make sure that the code inside the method only gets run every 90 milliseconds. That should sort you out.

    // Json