如何在 Swing 中正确移动绘制的多边形

How do I properly move drawn Polygon in Swing

我目前正在开发一款 2D 游戏,这将是一个谜题。我把所有东西都设置好了,把我所有的棋子都添加到我的棋盘上(称为 View)并且它们都正确对齐了。

我的下一步是 select MouseEvent#mousePressed 的一块,以便能够使用箭头键和 KeyEvent#keyPressed 移动它。我目前 运行 遇到的问题是,每当我移动 window 或调整 window 大小时,我的作品都会重新绘制它们自己。如果我点击一个棋子并想移动它,其他棋子也会移动(在某种程度上,它们不应该移动,因为一步大约等于 100 像素)。

如果有人能告诉我我的问题在哪里并给我一些建议,我将不胜感激。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Puzzle {

    public static void main(String[] args) {
    SwingUtilities.invokeLater(Puzzle::new);
    }

    public Puzzle() {
    JFrame frame = new JFrame("Puzzle");
    frame.setSize(400, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    View view = new View();
    view.createPieces();
    frame.setContentPane(view);
    view.setFocusable(true);

    MouseAdapterMod listener = new MouseAdapterMod(view);
    view.addMouseListener(listener);
    view.addKeyListener(listener);

    frame.setVisible(true);
    }
}

class View extends JPanel {

    final List<Piece> pieces = new ArrayList<>();

    public View() {

    }

    void createPieces() {
    Piece topLeft = new Piece(100, 100, 0, Color.YELLOW);
    pieces.add(topLeft);
    }

    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D gc = (Graphics2D) g;
    for (Piece needle : pieces) {
        needle.translate(needle.getLocation().x, needle.getLocation().y);
        gc.setColor(needle.color);
        gc.fill(needle);
        gc.draw(needle);
    }
    }

}

class Piece extends Polygon {

    int x;
    int y;
    final Color color;
    Point location;

    Piece(int x, int y, int type, Color color) {
    this.x = x;
    this.y = y;
    this.color = color;
    this.location = new Point(x, y);

    int[] arrX = new int[] { 0, 100, 100, -100, -100, 0 };;
    int[] arrY = new int[] { 0, 0, -100, -100, 100, 100 };

    for (int drawIndex = 0; drawIndex < arrX.length; drawIndex++) {
        addPoint(arrX[drawIndex], arrY[drawIndex]);
    }
    }

    Point getLocation() {
    return location;
    }

    void setLocation(Point location) {
    this.location = location;
    }
}

class MouseAdapterMod implements MouseListener, KeyListener {

    final View view;
    Polygon current;

    public MouseAdapterMod(View view) {
    this.view = view;
    }

    @Override
    public void mousePressed(MouseEvent e) {
    for (Piece piece : view.pieces) {
        if (piece.contains(e.getX(), e.getY())) {
        current = piece;
        System.out.println(current);
        }
    }
    }

    @Override
    public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()) {
    case KeyEvent.VK_UP:
        current.translate(0, -100);
        view.repaint();
        break;
    case KeyEvent.VK_DOWN:
        current.translate(0, +100);
        view.repaint();
        break;
    case KeyEvent.VK_LEFT:
        current.translate(-100, 0);
        view.repaint();
        break;
    case KeyEvent.VK_RIGHT:
        current.translate(+100, 0);
        view.repaint();
        break;
    }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
    }

    @Override
    public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

    }
}

(抱歉缩进,Whosebug 搞砸了)

编辑: 但问题是,最终我想使用 Piece#setLocation 移动我的作品以始终跟踪它们当前的 x/y 坐标。我想,我需要在我的绘画中调用 Piece#getLocation 来根据位置绘制它们,但我不知道如何。使用 Piece#setLocation 字面上没有任何作用。

您可以不平移多边形,而是平移位置并将其用于绘画:

keyPressed 中,替换 current.translate 以翻译位置而不是整个多边形(例如,您可以覆盖 Piece#translate 以调用 current.getLocation().translate)。

View#paintComponent中替换:

needle.translate(needle.getLocation().x, needle.getLocation().y);

gc.translate(needle.getLocation().x, needle.getLocation().y);

并添加

gc.translate(-needle.getLocation().x, -needle.getLocation().y);

在 for 循环的末尾。 这会将整个 Graphics2D 转换为绘制一个多边形,然后将其还原。

如果您从 恢复到 Piece 代码,您可以通过以下方式移动棋子:

@Override
public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()) {
    case KeyEvent.VK_UP:
        view.translatePiece(0, -100);
        break;
    case KeyEvent.VK_DOWN:
        view.translatePiece(0, +100);
        break;
    case KeyEvent.VK_LEFT:
        view.translatePiece(-100, 0);
        break;
    case KeyEvent.VK_RIGHT:
        view.translatePiece(+100, 0);
        break;
    }
}

并添加到您的观点中:

void translatePiece(int dx, int dy) {
    if (current != null) {
        current.x += dx;
        current.y += dy;
        repaint();
    }
}

if (current != null) { ... } 测试将防止您的应用程序在您点击一个片段之前按箭头键时崩溃,目前它确实如此。