Good Day (or night) Everyone!
I was lately developing a little game and decided to use javax.swing (basically JFrame stuff xD) and KeyListener.
Now, my basic game loop was a thread, so it went smoothly with my KeyListener.
So now I was able to get input from the KeyListener, process the input, and make an output.
But then I had to make a function that would draw all the things that I wanted drawn on the JFrame.
This took quite a bit of the computing power I had and made my game slower. But that wasn't the problem.
The problem occurred when I had to use that function multiple times almost at once, which didn't let the KeyListener run at all.
So now my big question is, what would be the preferred method to prevent the interruption?
Can I use some sort of Thread but for voids to prevent this?
Big Thanks from Me to Whoever solves my question! :D
Related
I made a simple "snake" game in java. As an exercise I want to control the user-input and the graphical-preview of the game separately using interfaces.
I have created 2 interfaces for that: InputSystem and GraphicalSystem.
So my main-loop of the game looks something like that:
while (snake.isAlive()) {
byte direction = inputSystem.getDirection(snake); // get input from user
snake.moveDirection(direction); // move in the game
graphicalSystem.update(); // preview current game state
The problem I've encountered is the implementation of the InputSystem interface. More precisely I try to make it a keyboard-input kind of thing. (while my GraphicalSystem is a GUI that I have already implemented).
The only way I found to receive a keyboard input was using some kind of GUI. But my GraphicalSystem is already using GUI. I also don't want to add the keyListener into the GraphicalSystem, because it will miss the whole idea of separating inputs and graphics. Do you have any suggestions for what should I do? I would love any idea.
Thanks in advance :)
I have a fairly simple Java Application I created with JFrames. There is more than one JFrame in my program. I have a menu launching at the beginning of the program. After going through the menu, I have the menu JFrame disposed.
The main JFrame has a menu button that should launch the exact same menu at a later time. However, when you launch the menu from inside of the ActionListener (when you press the menu button), the JFrame doesn't launch properly. None of the components show up and colors are off.
However, when the menu is launched from outside of the ActionListener, the JFrame shows up perfectly. I have tried multiple methods to fix this, but none of them have worked.
My full program code is available by clicking here.
The main class is "LetsMultiply5.java". This class also sets up the ActionListener.
The JFrame causing the problem is "MenuWindow.java".
"LetsMultiply5.java" calls the "Booter.java" class, which then calls the "MenuWindow.java".
"MainWindow.java" is the JFrame that has the "Menu" button.
For proof, "SpeedModer.java" calls the menu window after it has been disposed, and works.
================================EDIT================================
Also, I'd like to let you know that I realize my code is a little bit messy. I am not sure how else to write the code for this program.
I am forced to use Thread.sleep(x); because the Swing timers aren't what I am looking for. The Swing timers activate an ActionListener when the timer goes off. I need a system that will wait a second before continuing on with the code.
I realize that the while (repeater==0) loop with ActionListeners inside of it seems crazy, but that was the only way I could get it to work. If I put a single ActionListener and just had the while loop do no code inside of it, nothing happens when I press the button.
I would, as MadProgrammer mentioned:
Advice: Scrap your current approach and start again.
However, the way that I have my program currently coded is the only way that I know how to get what I need to do done. I read the tutorials, but still don't know how to improve the code in the way that you told me.
I thank everyone for trying to tell me to improve my bad "Java grammar", but as far as I am concerned, I am not going to continue this program for the next 20 years and make my millions off of it.
I have looked at the Swing timers before and I understand the whole new Timer(speed, this); concept, but I don't understand how this would make my code any better.
If anyone would like to show me how to fix my ActionListeners or Thread.sleep(x); lines, please tell me. Thank you.
You're blocking the Event Dispatching Thread with Thread.sleep(3000); - Don't do this, it will prevent the UI from been painted
See Concurrency in Swing for more details about the problem and How to use Swing Timers for a possible solution
You may also want to consider having a look at The Use of Multiple JFrames, Good/Bad Practice? and consider using CardLayout or JTabbedPane
If you need to block the user temporarily (to display a error message or gather important details), consider using a modal JDialog. See How to Make Dialogs for more details
So I want to be able to simulate mouse clicks on my window/frame, but in the background. This means I cant use the Robot class because it takes control of the OSes mouse which is not what I want (please guys, I know of and use the Robot class, it's not what I need).
I want the program to think I clicked on part of it (x,y on the frame) even when minimized (doesnt have to minimized but at least out of focus). I dont want it to take over my computer since it should ideally be a background task.
I did a little bit of research before posting and I read that I could make an Applet and then use the "reflection class"(?) and mouselisteners to invoke mouse events on the frame itself rather than through the OS. Not sure if itll work cause the explanation was pretty meh and the guy said they could communicate individually if he had problems :/.
Kind of wondering if it's possible at this point. If it cant be done in java, I know a little python, so if there's a solution for it in python, that could work to.
TLDR: Need to simulate mouseclicks on my frame, but it cant use the os mouse and should ideally work when the application is out of focus/in the background.
Thanks in advance :D
I have a project due in about an hour and I need to figure out the problem in my code before submitting.
In essence, there are two classes: one is a game and the other is a gui that should contain that game, some useless buttons, and a picture.
I'm having trouble making it all fit. I think I have a problem with the JPanels. (There might be some useless looking methods, but I had plans for them and I'll see if I have enough time for them to come into fruition)
This is life and death for my grade. I'd be eternally grateful if you could fix this code for me.
I'm positive the problem is not in the nitty gritty of the second link. It's been tested on its own and works.
Here's the code
http://pastebay.net/1236108
http://pastebay.net/1236109
Here's what I get when I run the code:
Get rid of the window in the Hangman constructor. Hangman is already frame.
Use pack instead of setSize
Git rid of game1 and game2 panels, they are adding nothing and causing you all the problems.
You will also find that if you override the getPreferredSize methods of DrawingDemoPanel and SubKillerPanel and return an appropriate size, that they will layout better.
Alternativly, you could create a single game panel and use a GridLayout to add the DrawingDemoPanel and SubKillerPanel to it, then add this to the CENTER position of the main window it will allow out better...
I have a line drawn in my gui and I want to put the arrow moving on the line. It is abit of animation and I can't seem to find it on the google. I hope you guys can help me out with Java gui animation.
Use a Thread with a loop inside its run() method. On each step of the loop change the position of the arrow a little bit, repaint the user interface (or only the part that you know has changed) and wait for about 30ms or so.
The subject of UI animation is far beyond the scope of a single answer, you could literally write a book about it. Fortunately somebody has - Filthy Rich Clients.