I'm a beginning computer science student and we've been asked to complete a project that animates random generated car objects moving to randomly generated parking spots on a city map grid. I've developed a GUI for the buttons, text fields, and text areas. Everything works as required except for at the end of the animation, when all cars have reached parking spots, I need to display analytics in my JTextArea. My buttons are set up properly and I will show you where the code takes place below:
else if (e.getSource() == start) {
setAnimate(true);
if(simulator.simulationFinished()) {
createAnalytics();
}
}
So here I have implemented an action listener on the button "start" that begins the animation. Currently it:
currently it begins the animation and all the cars travel to the parking spots as intended.
it displays analytics that are derived from before the animation began (Analytics include: car ID, number of moves, average number of spots tried, average distance travelled)
then the animation will conclude.
If I press the start button again it will display the proper analytics.
I know that in order for the program to display the right analytics the moment the program finishes I most likely need a while loop, however I haven't been able to dream anything up that will not create an infinite loop and require me to manually terminate the program via console.
while(!simulator.simulationFinished()) {
if(simulator.simulationFinished() == true)break; {
createAnalytics();
}
}
I've also tried this among several hundred other variations of all the loops in existence. Following from my logic, I need the while loop so that it will keep checking to see if the simulation is finished so that I can execute my method that generates analytics, but if don't give the while loop something to do it just goes on forever and crashes. I'm at a loss, any help would be appreciated. Thanks.
First: no, you don't need a while loop necessarily. You could use the observer pattern instead (for example) and your object would be notified as soon as the simulation had finished. As to your question: the if inside the while is obviously superfluous (as is the == true). The real problem seems to be that simulator.simulationFinished() never returns true. Could you post the code of that function and the code (and any code that directly influences the return value of it)?
Without going into details, there's a serious problem with your while loop.
See, it loops as long as simulator.simulationFinished() returns false.
However, within the loop, you check for the opposite - which will never happen.
I would recommend using listeners of some sort, although the requirement is not clear enough for me to advise any further.
Related
I want to move 2 characters simultaneously on screen when their respective keys are pressed.
I am making a java swing game. Wherein I want take inputs from 2 players using "AWSD" and another using "up down right left arrow" . I want both the character to move simultaneously when pressed their respective keys.I thought of making 2 threads and calling them everytime the key is pressed, but isn't working as expected. Any Solutions?
To make several character move simultaneously you do not need threading at all. What you need is a game loop, which
checks events
calculates the world
renders the world
If you know there are two characters, it means when checking the events you need to figure out player controls for two characters. When you calculate the world, you need to figure out new positions for both of the characters.
And finally render everything so the user can see the new world.
This needs to be repeated fast enough to have a smooth movement.
So im creating a game of blackjack as part of an assignment for my college. I have to create some kind of method to allow the player to Hit or Fold.I decided to write this part of code which checks the input:
do {
System.out.println(playernames[j]+" (H/F)");
playerinput=input.next();
} while (!((playerinput.equals("H"))||(playerinput.equals("F"))));
Everything runs smoothly until it reaches this loop.The workspace im working(eclipse) doesnt show any syntax problems nor any java exceptions occur.I tried editing this to find what is happening by using a temp boolean set to false before the loop and after the loop it gets set to true.The output didnt change meaning it didnt get past the loop.
This code works, but only if the user types a capital H or F. Maybe that's the problem? If you want to allow both lower case and upper case letters, you can use playerinput.equalsIgnoreCase("H").
Also, input.next() will only return after the user has pressed enter. Just pressing the H or F key is not enough.
I'm sorry if the title is vague. It's because I don't know exactly what it is called.
I am creating a program for a school project and I'm trying to figure out a shorter version of a code that I already got.
The interface looks like this:
Interface
The other parts of the program are not really necessary to put on here. They just input texts/strings in the first row textfields and shifts down one column every time new info is put in the first row.
The buttons on the right (the ones with the [x]) are the ones that are coded. They remove the text/string in the row next to them and shifts up the texts from below (if there are any) by one column.
The code for the first button is such:
CC01.setText(CC02.getText());
SC01.setText(SC02.getText());
SU01.setText(SU02.getText());
SD01.setText(SD02.getText());
SR01.setText(SR02.getText());
CC02.setText(CC03.getText());
SC02.setText(SC03.getText());
SU02.setText(SU03.getText());
SD02.setText(SD03.getText());
SR02.setText(SR03.getText());
CC03.setText(CC04.getText());
SC03.setText(SC04.getText());
SU03.setText(SU04.getText());
SD03.setText(SD04.getText());
SR03.setText(SR04.getText());
CC04.setText(CC05.getText());
SC04.setText(SC05.getText());
SU04.setText(SU05.getText());
SD04.setText(SD05.getText());
SR04.setText(SR05.getText());
CC05.setText(CC06.getText());
SC05.setText(SC06.getText());
SU05.setText(SU06.getText());
SD05.setText(SD06.getText());
SR05.setText(SR06.getText());
CC06.setText(CC07.getText());
SC06.setText(SC07.getText());
SU06.setText(SU07.getText());
SD06.setText(SD07.getText());
SR06.setText(SR07.getText());
CC07.setText(CC08.getText());
SC07.setText(SC08.getText());
SU07.setText(SU08.getText());
SD07.setText(SD08.getText());
SR07.setText(SR08.getText());
CC08.setText(CC09.getText());
SC08.setText(SC09.getText());
SU08.setText(SU09.getText());
SD08.setText(SD09.getText());
SR08.setText(SR09.getText());
CC09.setText(CC10.getText());
SC09.setText(SC10.getText());
SU09.setText(SU10.getText());
SD09.setText(SD10.getText());
SR09.setText(SR10.getText());
CC10.setText("");
SC10.setText("");
SU10.setText("");
SD10.setText("");
SR10.setText("");
So, yes, I have ten of these buttons. And the code for each button reduces by one 5-line code. For example, Button1's code is the one you see up top, then Button2's code starts from the [CC02...], the Button3 starts from [CC03], etc.
I think I used the brute force method of this code which I don't think is efficient and makes my code too long (the code for the ten [x] buttons alone is around 400 lines).
I asking of there is a much shorter way of doing this method.
Thanks.
So, I converted a game to Slick2D. The movement is broke, and I am at a loss. Before, we used KeyPressed and keyReleased methods, but now with Slick2D movement isn't working right.
Yea, nothing has gone right with converting to Slick2D. First the launcher, which I had a help topic on before, and now this. Though, the other topic was an issue with WebStart hating code.
You can only move right, using A. And you can't stop moving. Am I using the right methods? How can I fix it? Any help is greatly appreciated!
Here is a PasteBin link to the code, if it helps! http://pastebin.com/GRH86Yuw
I'm a fan of Slick, and I'd be happy to help.
The fundamental difference is that Slick is a polling model, not an event-driven model when it comes to input. Basically, in your logic update method you loop through the keys bound to your events, and check to see if any of the keys are currently pressed, and then trigger those events. For a number of reasons that I can go into if you like, polling tends to work better for games, especially with a large number of keys. It's just a different way of doing things, an not that complicated. The biggest upside is that you get centralized input processing a single method, instead of having it spread across multiple KeyListener instance objects.
If you want to look at Pedestrians - a simple pedestrian sim implemented in Slick - you can see an example of how to handle input in Slick.
Specifically, I handle input in this file (lines 192-295), inside the processInput method. Basically, you pass in a reference to the GameContainer object (the Slick object that contains your game), and from that you can get an instance to the Input instance that will allow you to check which keys are pressed, what mouse buttons are clicked, etc.
I need to make kind of a game in Java but I am a total beginner. It has to be applicable to the web and I need to use images on it and action listeners. So, do you recommend any site to know how to begin?
The description of the game (it is not really a game but it implements things that usually are in a game) is this:
Show a matrix of images of 3x3 elements, then, hide them and put instead empty squares. The images shown in the matrix, must remain in the lower part of the screen just below the empty squares and they must be randomly positioned. The user, must click one image and put it on the correct empty square. The result, must be, how many images were correctly positioned, the time it took to end the game, the time between mouse clicked and released for each image.
For additional information, I want you to know that this application is for a friend of mine who studies medicine. He wants this program to test patients who accident and receive hits on their heads. You may think that the description I gave you may not be a good software for that purpose, and in fact, it may be not, but, once I know the management of all that is required (Images, MouseListeners, how to introduce it to a web etc), I will be able to make a better product. So, please tell me, how can I begin?. What do I need to know?
I would start here. Except for some startup boilerplate and the restrictions of the sandbax (which, based on your description, you will b unlikely to encounter), there is no fundamental difference in an applet from normal code.