【问题标题】:Java - How to move a rectangle that's drawn by Graphics2D?Java - 如何移动由 Graphics2D 绘制的矩形?
【发布时间】:2014-02-21 03:32:42
【问题描述】:

我正在尝试移动 Graphics2D 绘制的矩形,但它不起作用。当我做 x += 1;它实际上将其移动了 1 个像素并停止。如果我说,x += 200;它不是在每次更新时都将其移动一次 200 像素,而是一次。

public void paint(Graphics g)
{
    super.paint(g);

    g.setColor(Color.WHITE);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());

    g.setColor(Color.RED);
    g.fillRect(x, 350, 50, 50);

    x += 1;
}

int x 在 void paint 之外被调用,以确保它不会每次都增加为 150。 画得很好,只是不动,我尝试使用线程并使用 while 循环,所以当线程运行时,它会移动,但没有运气。

【问题讨论】:

标签: java graphics


【解决方案1】:

您应该使用java.swing.Timer 来制作动画,而不是使用while 循环或其他线程。这是基本结构

Timer(int delay, ActionListener listener)

其中 delay 是您希望在重绘之间延迟的时间,listener 是具有要执行的回调函数的侦听器。你可以这样做,改变x的位置,然后调用repaint();

    ActionListener listener = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            if (x >= D_W) {
                x = 0;
                drawPanel.repaint();
            } else {
                x += 10;
                drawPanel.repaint();
            }
        }
    };
    Timer timer = new Timer(250, listener);
    timer.start();

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

public class KeyBindings extends JFrame {

    private static final int D_W = 500;
    private static final int D_H = 200;
    int x = 0;
    int y = 0;

    DrawPanel drawPanel = new DrawPanel();

    public KeyBindings() {
        ActionListener listener = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                if (x >= D_W) {
                    x = 0;
                    drawPanel.repaint();
                } else {
                    x += 10;
                    drawPanel.repaint();
                }
            }
        };
        Timer timer = new Timer(100, listener);
        timer.start();
        add(drawPanel);

        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class DrawPanel extends JPanel {

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.GREEN);
            g.fillRect(x, y, 50, 50);
        }

        public Dimension getPreferredSize() {
            return new Dimension(D_W, D_H);
        }
    }

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

这是一个运行示例

【讨论】:

  • 可以用图片代替这个矩形吗?我用 g.drawImage(...) 尝试过,但没有帮助。
【解决方案2】:

使用仿射变换。通过简单地使用 AffineTransform 类,您可以在 x 或 y 轴上沿屏幕平移图形对象。

这是一个链接,其中包含有关该类及其功能等的信息:https://docs.oracle.com/javase/8/docs/api/java/awt/geom/AffineTransform.html

这是另一个网站,可以为您提供有关翻译和其他转换的示例:http://zetcode.com/gfx/java2d/transformations/

并且在增加 x 或 y 进行平移时,必须调用 repaint() 函数来重新绘制图形对象。

【讨论】:

    猜你喜欢
    • 2016-08-30
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多