How do I run 2 functions simultaneously using thread - java

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.

Related

How to make a small version of my aplication properly?

I'm developing a Java application that displays a counter, that sums one to itself every time a pixel in a certain position is equal to a color after a certain action. The number from the counter is obtained from a text file, and gets updated on close.
I'm quite new into Swing so I'm a bit confused on how should I manage the small version of my app, which is basically the same but smaller duh and always on top.
Since according to this thread making new JFrames is not ideal, especially since this application just shows an Integer in the end.
My thought was to make them as different classes that extend from JPanel. Is this okay? I'm a bit confused on how should I make the switch back and forth if so.
big version idea:
small version idea:
I don't see why making a small JFrame would be bad. Also, you cannot display anything on a JPanel without either a JFrame or a JWindow.
My final verdict is that it would be just fine to use a small JFrame, you aren't taking performance hits or anything like that.

Java- "Circular" selection in JTextPane?

I've been working on a GUI to handle DNA sequences. Most of the molecules will be plasmids, which are circular forms of DNA. I can get a sequence as a string and display it in a JTextPane, but I'm not sure how to handle cases where the user might want to select a section of the sequence that crosses from the end to the beginning of the sequence. One thing I've considered is displaying the sequence twice, so you can select the last part of the first section and the first part of the last section, then overwriting some function so that copying the text will put the correct sequence on the clipboard instead of the string that was actually selected. (I'll have to do that anyway, I'm displaying the forward and reverse strands of the DNA, then a blank line, so that 1 "line" of actual sequence becomes 3 lines of text.
Is there some trick to circular strings that I just don't know about?
Do you know how other programs handle the selection of pieces of circular DNA? You might be able to get some inspiration from other software, like alignment viewers (supporting circular DNA) in this list on Wikipedia: en.wikipedia.org/wiki/List_of_alignment_visualization_software.
I think it would be wise to decide on how you want the GUI to work first, and then start working on your code. Otherwise you risk wasting a lot of time on implementing ideas that you end up not using.
You could even consider offering both a circular and a linear view on the same sequencing data, as this screenshot from the CLC Sequence Viewer shows:

ParkingSimulation infinite while loop crisis

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.

Slick2D Movement Issue

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.

Java | I need to make an applet but I don't know how to begin

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.

Categories

Resources