Java: Random Delay between repaint()
Hi,
I would like to include a random delay or wait between repaint() function calls. The reason for this is to create a delay between a player's move and the machine's move so as to create the illusion that the machine "is thinking". In the code below, when the user clicks on the panel, two squares are drawn, one where s/he has clicked, the other slightly above. I'd like the square above to appear between 1000ms and 3500ms after the click (the square in the click location to appear immediately). For the time being, they appear at the same time. If someone could alter this code so that it does what I want, I think I'll understand what needs to be done in my actual program: Code:
import java.awt.*; Thanks in advance, rm |
It's not a good idea to delay calls to repaint() because repaint() may be called by things outside of your control, eg: the window being covered and then uncovered. Furthermore, since repaint() only sends a request to call paint(), the call to repaint() after the first square is added will always cause the second square to shown, since you are adding it before before paint() has a chance to be called.
Instead, try delaying the call to add(). Also: Code:
|
Thank you ntubski for your reply. But I'm having trouble understanding what you are saying. I don't want to know what not to do, I want to know what to do! :)
In the code example, the repaint() function is in the add() function, so when I say I want to delay the repaint() function, I actually mean delay the add() function which contains the repaint() function. In any case, I don't really care how it is does, it's the result that counts. The barred out code in your example/quote corresponds to a few lines just to stock the random delay time in a variable (between 1 and 3.5 seconds), it has nothing to do with an actual delay(), sleep() or wait() function. I named it "delay", but I could have named it "i" or anything. Would you know how to alter my code to get it to do what I want it to do? If not, any other takers? I'm really stuck. I don't know where to start or how to do this, hence the post. Many thanks, rm |
Quote:
Code:
//Timer sleep = new Timer(delay, ???); Quote:
Quote:
|
ntubski,
Quote:
I'm not all that great at event handling yet and this is mouse stuff, not other actions. In the other thread, the ButtonTextSetter was used, which is a Java class. I really don't know how to do this. I don't even know where to start! Thanks nevertheless, rm |
Mea culpa...
I just realized that the ButtonTextSetter class is not a Java class... Give me a little time... I'll try to figure it out... Sorry about the confusion, rm |
Okay, yesterday I was ready to give up completely and today I got an idea, but I don't know how to implement it, I really don't. It's probably not the way to go anyway, but here it is...
In the class which is the equivalent of the ButtonTextSetter class of this post I will create a similar class which hides the Rectangle2D object. It will then appear a random number of seconds afterwards. Code:
class MachineRectangleTool implements ActionListener { I'm sorry if it seems that I'm looking for someone to code for me, but it really isn't the case! I really don't know how to solve this problem and code talks to me more than explanations... The code provided in this message is probably way off, not the ideal solution... Could someone alter my original code to make it do what I want it to do? Many thanks for your patience, rm |
Quote:
Quote:
You're overthinking this. In the previous example we needed to delay the setText() function call, here we need to delay the add() function call. In the previous example the parameters to setText() were button2 and "O" (yes, the object we invoke the method on is effectively a parameter to the function). In this example the parameters are the MousePanel instance and p2. Since MouseHandler is a nested class the use of the MousePanel instance is implicit/hidden/obfuscated. In the previous example we pass the parameters (button2 and "O") to the constructor of ButtonTextSetter and setText() is called in the actionPerformed() method. In this example we will need to pass the parameters (MousePanel instance and p2) to the constructor of MachineRectangleTool (or equivalent, probably a different name should be used as there will be no rectangles involved) and call add() in the actionPerformed() method. You can refer to the MousePanel instance from inside MouseHandler as MousePanel.this. Quote:
|
Thanks for your efforts ntubski.
Please forgive me, but all of this is way over my head. Sincerely, rm |
ntubski,
Quote:
Here's the code: Code:
import java.awt.*; If you have any comments about the details of the above code, I'm all ears. Thanks again! rm |
Good to see you didn't give up after all.
Since you made Point2Tool a nested class of MousePanel, you don't need the mousePanel field (and indeed, that field is unused). It might be instructive to rewrite the program without using nested classes. |
All times are GMT -5. The time now is 02:38 AM. |