java - 在 Java 中动画矩形

标签 java swing animation draw

我一直在尝试让我使用 for 循环创建的这个矩形移动。这段代码所发生的一切就是有一个原始矩形,然后在该矩形旁边有一个新矩形。没有动画发生,只有那两个矩形显示在窗口上。有什么方法可以使这个矩形动画化?

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

public class Gunman extends JComponent {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public int x = 10;
    public int y = 10;
    public int width = 8;
    public int height = 10;
    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawRect (x, y, width, height);  
        g.fillRect (x, y, width, height);
        for(int i = 0; i<=1024; i++){
            g.setColor(Color.red);
            g.drawRect(x++, y, width, height);
            g.fillRect(x++, y, width, height);
        }
    }
}

最佳答案

在 paint 或 paintComponent 方法中不要有程序逻辑,从逻辑上讲,我的意思是带有“motion”的 for 循环,因为它不会工作。你想要

  • 几乎从不在 JComponent 的 paint 方法中绘制,而是在其 paintComponent 方法中绘制。
  • 不要忘记也调用 ​​super.paintComponent(g) 方法,通常作为 paintComponent(g) 覆盖中的第一个方法调用。
  • 使用 Swing Timer 逐步更改 x 和 y 值
  • 在做出更改后调用 JComponent 上的 repaint()

例如

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

public class Gunman extends JComponent {

   private static final long serialVersionUID = 1L;
   private static final int PREF_W = 900;
   private static final int PREF_H = 700;
   private static final int TIMER_DELAY = 30;
   public int rectX = 10;
   public int rectY = 10;
   public int width = 8;
   public int height = 10;

   public Gunman() {
      new Timer(TIMER_DELAY, new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent actEvt) {
            if (rectX < PREF_W && rectY < PREF_H) {
               rectX++;
               rectY++;
               repaint();
            } else {
               ((Timer)actEvt.getSource()).stop();
            }
         }
      }).start();
   }


   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.red);
      g.drawRect(rectX, rectY, width, height);
      g.fillRect(rectX, rectY, width, height);
   }

   public int getRectX() {
      return rectX;
   }

   public void setRectX(int rectX) {
      this.rectX = rectX;
   }

   public int getRectY() {
      return rectY;
   }

   public void setRectY(int rectY) {
      this.rectY = rectY;
   }

   private static void createAndShowGui() {
      Gunman mainPanel = new Gunman();

      JFrame frame = new JFrame("Gunman");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

}

关于java - 在 Java 中动画矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986285/

相关文章:

javascript - 如何使用 Raphael.js 让旋转动画在每次单击鼠标时触发?

java - 获取 TreeMap 中的三个最高值

java - AtomicLong.lazySet 的 C++ 端口

java - 找到最小值但为零

用于单击按钮的 Java swing ActionEvent

ios - 如何在动画期间清除 Collection View 单元格的 View /图层缓存?

java - JLabel 将纯文本包装为 JTextArea

java - 如何在JFrame的标题栏中添加图片图标?

java - 组合输入 validator

javascript - 为什么这些 Javascript 函数会产生不同的结果?