gpt4 book ai didi

Java Swing mousePressed 和 getSource() 未在 JPanel 上显示绘制的形状

转载 作者:行者123 更新时间:2023-12-02 01:37:33 25 4
gpt4 key购买 nike

我目前正在尝试开发一个谜题。我得到的只是我的应用程序以及我的游戏 field 和游戏组件。下一步是单击我的一个游戏 block 以选择它,并能够使用箭头键移动它(此外,我希望它们仅移动,如果下一步(将是 100 像素)不包含任何其他游戏件)。

我当前遇到的问题:在我的主 JPanel 上使用 addMouseListener() ,然后使用 getSource() 仅返回我的Playing-field(在我的代码中称为 View),但我需要它返回所需的游戏 block (例如 topLeft)。我已经尝试将 getSource() 转换为 Piece,但这不起作用(无法将 View 转换为 Piece)。

因此,我需要找到一种方法来添加鼠标监听器,该监听器返回被单击的游戏 block ,以便我可以更改位置并检查与任何其他游戏 block 的任何碰撞。提前致谢!

感谢@camickr编辑了代码。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
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);
}

private final static int[] SHAPE_X = { -100, 100, 100, 0, 0, -100 };
private final static int[] SHAPE_Y = { -100, -100, 0, 0, 100, 100 };

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

View view = new View();
frame.setContentPane(view);
view.addMouseListener(new MouseAdapterMod(view));

Shape polyShape = new Polygon(SHAPE_X, SHAPE_Y, SHAPE_X.length);
Piece topLeft = new Piece(Color.YELLOW, polyShape, 0, 100, 100);
view.pieces.add(topLeft);

Piece topRight = new Piece(Color.CYAN, polyShape, 90, 300, 100);
view.pieces.add(topRight);

Piece bottomRight = new Piece(Color.GREEN, polyShape, 180, 300, 300);
view.pieces.add(bottomRight);

Piece bottomLeft = new Piece(Color.RED, polyShape, 270, 100, 300);
view.pieces.add(bottomLeft);

Piece square = new Piece(Color.ORANGE, new Rectangle(200, 200), 0, 100, 100);
view.pieces.add(square);

frame.setVisible(true);
}

}

class View extends JPanel {

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

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

Graphics2D gc = (Graphics2D) g;
for (Piece piece : pieces) {
piece.draw(gc);
}

}
}

class Piece {
final Color color;
final Shape shape;
final int angle;

int x;
int y;

Piece(Color color, Shape shape, int angle, int x, int y) {
this.color = color;
this.shape = shape;
this.angle = angle;
this.x = x;
this.y = y;
}

void draw(Graphics2D gc) {
AffineTransform tf = gc.getTransform();
gc.translate(x, y);
gc.rotate(Math.toRadians(angle));
gc.setColor(color);
gc.fill(shape);
gc.setTransform(tf);
}

Shape getShape() {
return shape;
}
}

class MouseAdapterMod extends MouseAdapter {

final View view;

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

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

最佳答案

So, I need to find a way to add a mouse listener which returns the gaming-piece that was clicked

您可以使用 MouseEvent 中的 getX() 和 getY() 方法。

然后,您迭代“ block ”ArrayList,并对每个 Piece 中包含的 Shape 调用 contains(... 方法查看鼠标点是否包含在棋子中。

因此,您还需要向“Piece”类添加一个 getShape(...) 方法,以便可以访问每个 Piece 的 Shape .

编辑:

所以你的基本逻辑可能是这样的:

//Shape polyShape = new Polygon(SHAPE_X, SHAPE_Y, SHAPE_X.length);
//Piece topLeft = new Piece(Color.YELLOW, polyShape, 0, 100, 100);
Polygon topLeftPolygon = new Polygon(SHAPE_X, SHAPE_Y, SHAPE_X.length);
topLeftPolygon.translate(100, 100);
//topLeftPolygon = ShapeUtils.rotate(...); // do rotation when required
Piece topLeft = new Piece(Color.YELLOW, topLeftPolygon);

那么draw(..)方法中的绘画代码就是:

gc.setColor(color);
gc.fill(shape);

无需转换或翻译。

编辑2:

所以使用形状:

//topLeftPolygon = ShapeUtils.rotate(...); // do rotation when required
//Piece topLeft = new Piece(Color.YELLOW, topLeftPolygon);
Shape topLeftShape = ShapeUtils.rotate(...); // do rotation when required
Piece topLeft = new Piece(Color.YELLOW, topLeftShape);

这当前与您的 Piece 类匹配,无论如何它都需要一个 Shape 对象。请考虑所建议的概念,不要假设发布的代码是完美的,因为它显然尚未经过测试。

关于Java Swing mousePressed 和 getSource() 未在 JPanel 上显示绘制的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55003607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com