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..
Code :
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)
Re: Delay/Wait/Pause in Java + AirBrushing
Thread.sleep() -- See the Java API for more details.
The Java API: Java Platform SE 6
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?
Code :
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.
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
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.
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().
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