有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何为我的程序获得流体运动?

程序运行良好,但当我按住键或更改键时,运动并不平稳。它会暂停一秒钟,然后变得平滑。我还是Java的初学者,所以请详细解释

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Game2015
{
   JFrame frame;
   DrawRect mainPanel;
   GridBagLayout gridbag;
   GridBagConstraints constraints;
   Data data;
   int width;
   int height;

   public static void main(String[] args)
   {
      new Game2015();
   }

   public Game2015()
   {
      frame = new JFrame();
      width = 400;
      height = 500;

      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.setVisible(true);
      frame.setSize(width, height);
      frame.setResizable(false);

      data = new Data();
      mainPanel = new DrawRect(data);
      mainPanel.setFocusable(true);
      mainPanel.setBackground(Color.WHITE);
      KeyHandler keys = new KeyHandler();
      mainPanel.addKeyListener(keys);

      frame.add(mainPanel);
      frame.validate();
      frame.setLocationRelativeTo(null);
      Thread thread = new Thread(new Runnable(){
         public void run()
         {
            while(true)
            {
               mainPanel.repaint();
               mainPanel.requestFocus(true);
            }
         }
      });
      thread.start();
   }

   public class KeyHandler implements KeyListener
   {
      public void keyReleased(KeyEvent event)
      {

      }
      public void keyTyped(KeyEvent event)
      {

      }

      public void keyPressed(KeyEvent event)
      {
         if(event.getKeyCode() == KeyEvent.VK_W || event.getKeyCode() == KeyEvent.VK_UP)
         {
            if(data.getY() >= data.getSpeed())
            {
               data.setY(data.getY() - data.getSpeed());
            }
            else
            {
               data.setY(0);
            }
         }
         else if(event.getKeyCode() == KeyEvent.VK_S || event.getKeyCode() == KeyEvent.VK_DOWN)
         {
            if(data.getY() + 95 <= height - data.getSpeed())
            {
               data.setY(data.getY() + data.getSpeed());
            }
            else
            {
               data.setY(height - 95);
            }
         }
         else if(event.getKeyCode() == KeyEvent.VK_A || event.getKeyCode() == KeyEvent.VK_LEFT)
         {
            if(data.getX() >= data.getSpeed())
            {
               data.setX(data.getX() - data.getSpeed());
            }
            else
            {
               data.setX(0);
            }
         }
         else if(event.getKeyCode() == KeyEvent.VK_D || event.getKeyCode() == KeyEvent.VK_RIGHT)
         {
            if(data.getX() + 75 <= width - data.getSpeed())
            {
               data.setX(data.getX() + data.getSpeed());
            }
            else
            {
               data.setX(width - 75);
            }
         }
         else if(event.getKeyCode() == KeyEvent.VK_ESCAPE)
         {
            frame.dispose();
            System.exit(0);
         }
      }
   }
}

绘制矩形的代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

public class DrawRect extends JPanel
{
   BufferedImage img_player;
   File file;
   Data data;

   public DrawRect(Data newData)
   {
      data = newData;
      try
      {
         file = new File("Images\\Player.png");
         img_player = ImageIO.read(file);
      }
      catch (Exception exception)
      {

      }
   }

   @Override
   public void paintComponent(Graphics graphics)
   {
      super.paintComponent(graphics);
      Graphics2D graphics2D = (Graphics2D) graphics;
      graphics2D.drawImage(img_player, data.getX(), data.getY(), null);
   }
}

数据类的代码如下:

public class Data
{
   int x, y, speed;

   public Data()
   {
      x = 0;
      y = 0;
      speed = 10;
   }

   public void setSpeed(int new_speed)
   {
      speed = new_speed;
   }

   public int getSpeed()
   {
      return speed;
   }

   public void setX(int new_x)
   {
      x = new_x;
   }

   public int getX()
   {
      return x;
   }

   public void setY(int new_y)
   {
      y = new_y;
   }

   public int getY()
   {
      return y;
   }
}

共 (0) 个答案