Java moving rectangle faster?

So, I'm trying to make a popular game pong in java. I made a player rectangle and set up an action listener, so I'm ready to move the player up and down the screen. But I ran into a problem. When I move the player, I have the choice of moving X pixels per move.

But if I set the movable X pixels, let's say 1. Then the player moves too slowly. If I set X pixels to 10, then it will skip 9 pixels and the animation will look rude. How can I smooth the animation and still move fast?

here is the code:

public void keyPressed(KeyEvent e) { if(e.getKeyCode() == keyUp){ playerYCordinate -= 10; }else if(e.getKeyCode() == keyDown){ playerYCordinate += 10; } repaint(); } public void keyReleased(KeyEvent e) { if(e.getKeyCode() == keyUp){ }else if(e.getKeyCode() == keyDown){ } repaint(); } 
+4
source share
5 answers

Do not rely on the key retry features provided by the system. They can occur at a low speed (which causes the problems you describe) and depend on the settings of the system keyboard.

I recommend that you use keyPressed to start Timer and keyReleased to stop it.

A timer can schedule TimerTask every 20 milliseconds or so, which can update the coordinates and call repaint() . (Don't worry about calling repaint() frequently. If the EDT can't keep up, it will compress successive calls to redraw it in one paint.)

+4
source

How can I smooth the animation and still move fast?

One way is to draw translucent versions of a moving object along a path that it could have made if it had collected fewer pixels per turn.

eg. for a 10-pixel move, draw a version with an opacity of 10% for the first move pixel, 20% for the second, etc.

Moving block

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MovingBlock { MovingBlock() { final JPanel gui = new JPanel() { private static final long serialVersionUID = 1L; int x = 0; int step = 60; public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; x+=10; Color fg = getForeground(); for (int ii=x-step; ii<x; ii+=4) { double transparency = (double)(x-ii)/(double)step; Color now = new Color( fg.getRed(), fg.getGreen(), fg.getBlue(), (int)(255*(1-transparency))); g2.setColor(now); g2.fillRect(ii, 3, 5, 10); } if (x>getWidth()) { x=0; } } }; gui.setBackground(Color.BLACK); gui.setForeground(Color.GREEN.darker()); gui.setPreferredSize(new Dimension(400,16)); ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { gui.repaint(); } }; Timer timer = new Timer(20, listener); timer.start(); JFrame f = new JFrame("Moving Block"); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.setContentPane(gui); f.pack(); f.setLocationByPlatform(true); f.setVisible(true); } public static void main(String[] args) throws Exception { SwingUtilities.invokeLater(new Runnable() { public void run() { new MovingBlock(); } }); } } 
+2
source

Games traditionally use a game loop to control the application, rather than the event-driven system that controls Swing.

You may have a variable that stores the movement of the paddle, for example, an enumeration with UP , DOWN and STATIONARY . Your key listener has set the value of this variable, and then the game loop will check the variable and update the position of the paddle accordingly.

This method separates the application speed from the speed of the environment in which it runs.

+2
source
  • do not use KeyListener for Swing JComponents

  • use KeyBinding for Swing JComponents

  • don't draw a JFrame there JPanel and use the paintComponent() method instead of paint() , your movement will be smooth and natural

+1
source

As long as you depend on keystrokes to control your rectangles, you can only move as fast as keyrepeat events (or as fast as the user can enter). Either listen to something else like mouse movement, or adjust the keyboard for a higher repetition rate.

0
source

All Articles