Why doesn't my oval move Java
I was writing a little snake like a game and I was doing a key event listener, so it listens when the arrow keys are pressed, it increases or decreases the position of the oval in the frame. Below is my code, what do you think is going on? I tried googling for a possible solution, but I ended up empty.
package snakegame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
/**
*
* @author PlaixHax0r
*/
public class SnakeGame extends JFrame{
int x, y;
public class ActionListener extends KeyAdapter{
public void KeyPressed(KeyEvent a){
int KeyCode = a.getKeyCode();
if(KeyCode == KeyEvent.VK_RIGHT){
x++;
}
if(KeyCode == KeyEvent.VK_LEFT){
x--;
}
if(KeyCode == KeyEvent.VK_DOWN){
y++;
}
if(KeyCode == KeyEvent.VK_UP){
y--;
}
}
public void KeyReleased(KeyEvent a){
}
}
public SnakeGame(){
addKeyListener(new ActionListener());
setTitle("Snake 1.0");
setVisible(true);
setSize(500, 500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
x=150;
y=150;
}
public void paint(Graphics g){
g.drawString("Welcome to Snake Empire", 180, 50);
g.fillOval(x, y, 15, 15);
repaint();
}
public static void main(String[] args) {
new SnakeGame();
}
}
source to share
Games are different things (although some use a swing to show a frame). These are problems in your game.
-
You are calling
repaint()
insidepaint()
. He schedules another paint event before it actually finishes. Calling it so many times would burst your processor. If you just need to move the oval, just call redraw at the end of your methodkeyPressed()
. -
You had no control over the timing. This results in your game running fast on a fast system and slowing down on a slow system. Try to find
game loops
-
Many games use multiple threads. A logical thread that is decoupled from
EDT
to prevent the processor from freezing and being able to enter input at normal speed. -
Avoid rendering directly to the frame. Recommended only if you want active rendering and build
BufferStrategy
(although you won't be using the paint method). -
You are not using double buffering. This makes the game flicker.
-
You cannot directly change the positions of objects directly in the input state. Just change their specific speeds and just add them in positions (also use negative speeds) when updating to ensure that objects are only updated at regular intervals.
-
Finally, try to find
java game development tutorials
. There's a simple structure here (tutorial) that gives a good introduction to the topic. (This is an example of a snake game).
Get rid of naming convention .NET
in java (running method names with capitals)
source to share
Several recommendations:
- Don't draw directly to the JFrame. Check out the drawing tutorials and you will see yourself drawing a JPanel or other JComponent method
paintComponent(...)
. - The first line of yours
paintComponent(...)
is usually a call to the super method:super.paintComponent(g)
- Do not call
repaint()
insidepaint(...)
orpaintComponent(...)
. This is a bad way to try to perform uncontrolled animation. - Use a Swing timer instead.
- Do not name the class the same as the main class, in particular, do not call it ActionListener as it will hardly help you to use the actual ActionListener if needed.
- KeyListeners only work if the component listening is custom and has focus.
- Avoid using KeyListeners with Swing apps, but use Key Bindings instead. Please find this site as this has been discussed many times with many examples, some of mine.
Please take a look at the Keyword Tutorial and then take a look at my sample code for using keybindings here and here .
source to share
The problems that I see, besides the observed hovercraft, are as follows:
-
you are not overriding any KeyListener (or KeyAdapter) method. Your method has a name
KeyPressed
and methods always start with a lowercase letter in Java. The method is calledkeyPressed()
. Always use the @Override annotation when you intend to override a method. Thus, when you don't, because you have a typo in the method name, you will get a compiler error:@Override public void keyPressed(KeyEvent event) { ... }
-
you don't call repaint () when a key is pressed, so there is no reason Swing is replicating your frame.
source to share