I wanted to make a "memories" or "find pairs" game for Java, just to start with a basic game before starting something more difficult. My game works very well, but I just have 1 big problem. I don't really know how to hide the 2 images x seconds after the user clicks on the second one.
This is what is done:
The game is created with 4x4 buttons in a array
The values of the cards are distributed (name, position, icon)
The user then clicks on the first card, which shows up immediately
The user clicks on the second one, if the first is equal to the second one then disable the 2, if not then re-hide the 2.
But I don't know how I can make the program show the second card and then after x seconds hide the 2...
How can I solve this?
Depending on what framework you are using, there should be a timer utility available to you.
For instance, if you are using Swing, then you should be able to use javax.swing.Timer as suggested in the above comments. Follow this link for a simple Swing Timer tutorial. As they say on that page, swing timers can be used in one of two ways and one of those ways is:
To perform a task once, after a delay
This sounds like exactly what you are trying to achieve.
You could also try a library like Joda Time which has lots of features and options.
you should make match function and another unmatch function and inside the unmatch function
you can use setTimeout and give the two cards class(flip) when you choose them and after certain time and when they didn't match remove this flip class.
Related
I'm making a game right now and want to add a timer to count down to my end game screen, I can get the text to show up on my game screen but the numbers won't go down.
Any feedback would help!
Biased note: Greenfoot is a great tool to learn to program in Java. Keep going! But learn to read documentation[1], please.
You are calling subtractTime() only in the constructor Timer(). This way it will be called only once at creation time of your Timer object.
Call it in the act() method instead.
It will be called every time when the Greenfoot system lets all actors and the current world, well, act. The time between the calls is controlled by the "Speed" control below the world's frame, or by the specific methods of the class Greenfoot.
If you need "real-time" values like seconds, do some research how to obtain seconds or milliseconds.
[1]: You can read Greenfoot's documentation in the Editor window by double-clicking on "World" or "Actor" in the class diagram on the right.
I am just learning AWT / Swift / JavaFx recently and I feel like I have learned a lot but have barely scratched the surface. There might be a much easier way to do this BUT, I am trying to make a GUI button in eclipse that calculates the distance between two objects that the user creates. Lets call them Robots for now. So, I have one button that allows the user to create the Robots and it stores them in a DefaultListModel (listModel) and displays them in a Jlist (list) below the buttons. When the user then clicks on a robot, another button becomes actice and allows them to calculate the distance between them (one of the parameters of the robots is their location on a grid). I have all that worked out but my problem is that I am trying to make it to where they have to select two different Robots. At first I thought I could let them select two Robots and then make the computeDistance button becomes active, but I am not really sure how to do that because the only way I can select more than one object in the JList is to cntrl click and I don't want the user to have to know that trick.
My next idea was to allow the user to have one Robot selected and then give them a popup window displaying the other Robots and have them select one. Via showOptionsDialog, I have discovered how to make a custom JOptionPane, so I thought, why not make them buttons (probably will look awful, but I don't know how to make anything other than JOptionPane.showXxx at this point (like I said, only skin deep so far). Have tried consulting the javadocs, but right now that is a LOTTT to take in and have read a decent amount, I thought.
Ok, sorry if this is long, but is there a way, using my DefaultListMethod to make custom buttons? I tried a bunch of ways by creating Object[] options = {list.elements()}; etc but that doesn't work. Any help would be much appreciated!
I want to create a simple animation of a game so you can see the playout of a game. It should be an animation in which you just see the game Tic-Tac-Toe played. I have the states of the games in a description. So player 1 marks a cell = state 1; player 2 marks a cell = state 2 etc..
I currently have the game parsed in a ruby program; it will be easy to display just one state (like in the image), but how do I create an animation from it? Is there an easy way to do this? I'm open to solutions in every language but it shouldn't take to much time to implement. I want to show such an animation in a presentation.
<state1>
cell(1,1,x).
cell(1,2,o).
cell(1,3,o).
cell(2,1,o).
cell(2,2,x).
cell(2,3,x).
cell(3,1,b).
cell(3,2,b).
cell(3,3,x).
</state1>
<state2>
...
</state2>
Since you already know how to display one state, the way to animate that is to display each state one after the other in sequence.
Display the first state and set a timer, then when the timer goes off show the next state and set another delay, and so on through all the states.
Since you said you are open to all languages I don't really want to bother with any specific code beyond that high-level overview. For example, in Flash/ActionScript I would use TweenLite to display the symbols with a delay, but I don't know if that specific code would be of any use to you.
I've never programmed Ruby but it probably has a command like setDelay() or setTimer() or something. If not, the same effect can be accomplished with a main loop that will check the time each cycle and if the delay has been long enough it shows the next state.
For ruby you can have a awesome tic-tac-toe from https://github.com/grosser/tic_tac_toe
just do "gem install tic_tac_toe"
I'm doing a path-finding project as part of my 4th year software engineering degree.
We're suppose to give visual representation to a bunch of multi-agent pathfinding algorithm. The simplest one is A* adapted for multiagents.
Anyway our environment is a grid map where every cell can be either blocked or used as part of an agent's path. What I want to do is use animation to give a good representation of the final movement of the agent, but animating color change in my grid.
I.E paint every step in the path for a second or so with some color to show how the agent moves.
And the other thing I want to do is to represent the way the algorithm works by painting the changes in the open list and closed list of the A* algorithm while its doing its calculation.
I'm using an adapted version of the observer design pattern to send events from my algorithm layer to my controller and GUI layer.
What I want to do in the GUI layer is every time a tile is added to the open list, I want to have that cell painted in some color and then have it fade away according to a predefined timer or maybe later add a slider to control this timer.
I looked at the code here. It seems pretty simple, the problem is that every tile animation has to be independent of the others to allow the algorithm and everything to keep running and different animations to start.
So what's the best way to achieve the results I'm looking for? Should I just open a different thread for each animation or have a pre-made thread for each cell?
Would that be an overkill for the application, since there can be up to 1000 cells and therefore close to 1000 threads performing animation.
Another issue I think I might encounter is the fact that it might happen that a cell will start its color fading animation and then will have to restart and I don't want the two animations to go at the same time (only one thread performing animation for the same cell at the same time).
I hope I was clear enough with what I'm trying to achieve, if someone has any ideas or thought it could really help me with my project.
You can find Trident animation library useful. More information at http://kenai.com/projects/trident/pages/Home
I would consider a scenario with a single animation thread only. You might e.g. try the following strategy:
standard event thread for swing events
one worker thread for your logic
only one additional for all animations
This third thread manages all animation within your gui. Therefore it maintains a list of animation actions to perform together with their timestamp. Such an action can be e.g. "set color of cell [1,2] to CF0000 # 17:01:00" in an approriate data structure. This list of actions is then filled by the worker thread with the animation actions (e.g. you may add several actions at once for a fading cell - 1) set to 100% # now; 2) set to 75% # now+10s; 3) set to 50% # now+20s ...). Make sure that this list is properly synchronized since it will be accessed from both threads.
If you keep this list sorted by timestamp it is quite easy to determine the which actions this thread has to do at any time. This thread has then a quite simple loop, e.g. something like
while(true) {
AnimationAction action = list.get(0);
if(action!=null && action.timestamp <= now()) {
action.perform(); // <= be sure that paint events occur in the edt
list.remove(0);
continue;
}
sleep(...);
}
Note that you can determine the time to sleep from the timestamp of the next action but consider that a new animation event arriving might have to interrupt this. Otherwise you can also sleep for some short amount of time.
Regarding your second question this thread might remove any actions from this list on demand for a special cell if a new action arrives. Therefore you might also maintain a secondary data structure in order to do this efficiently.
I'd use javax.swing.Timer and AlphaComposite, as demonstrated here.
I am building a swing application. At some point, I have to start an "animation":
...
jpanel1.setBackground(Color.Black);
Delay(milli)
jpanel1.setBackground(Color.White);
...
and so on.
The gui itself and all the logic behind it work.It is just this time depended color-changing that does not. I have read, that swing is not thread safe, but all the examples I found showed me how to start another thread (for example in the background) but never how to stop the current swing-gui thread.
Edit:
The application should work as following:
configuration files are read, jframe is set up.
some simple questions are beeing asked
a dialogue is opened, which explains the animation.
after the user clicked "ok" the animation - some color flashing - is started. the color and the delay between the color-changing is depended on the configuration
another dialogue is opened and the programm continues -> new jpanel inside the jframe, buttons and so on.
the online thing that does not work are the delays between the color-changing. I understand now why it does not work and I am trying to build a timer, which activates a actionlister, which then changes the color and stops the timer... it just seems so much work for a simple delay... and I have to reorganize the entire animation in the application.
Take a look at: https://timingframework.dev.java.net/
and the samples that come in http://filthyrichclients.org/
They provide some very good information on how animation work and using the Timer framework. You'll have a good understanding of how it works.
I did a sample animation here with Swing after reading those:
count down demo app http://img580.imageshack.us/img580/742/capturadepantalla201004wd.png
Java application featuring blog.stackoverflow.com page ( click on image to see the demo video )
But I'm not even sure what is what you want to achieve.
EDIT
I read about the timing framework to understand better what is all about, but I actually didn't use it ( it is useful to create animations with no linear times - ie no every second as mine, but things like 1, 5, 3, 2 seconds )
The code I'm using in the demo above is exactly this:
final Timer imageTimer = new Timer();
imageTimer.schedule( new TimerTask() {
public void run() {
changeImage();
}
}, 0, 10000 ); //<-- every 10 seconds.
The animation for the "stackoverflowing" and the count down use a similar approach.
You do not want to stop the GUI thread, even if you want to have a flashing effect. This is because other basic actions, like repainting when the GUI is hidden by other windows, will be stalled. Take a look at Timer. It will allow you to have an event fired on an interval and you can handle that, in the GUI thread, in your actionPerformed method.
You will want to use the javax.swing.Timer class and not the java.util.Timer class.
The later is preferred when you need general timing the former is preferred for UI updating/changes.
See http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
You may also want to look at https://timingframework.dev.java.net/.
Do the timer on another thread and when the timer goes off it can send an update message for the animation to draw the next frame.
Another consideration is the delay itself. Don't pick a fixed delay-interval. Old games used to do that and they become unplayable on faster computers. Instead what the newer games do is use the speed of the current CPU to figure out how many update events they need a second at runtime, call it a 'delay-factor', and is set when the program starts up. . The timer uses the delay factor so the animation displays correctly even on machines of different clock-speed.