- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试开发一个谜题。我得到的只是我的应用程序以及我的游戏 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/
我创建这个是为了当在鼠标的 x 和 y 坐标处按下鼠标时画一条鱼。但我似乎没有调用drawfish 方法。我找不到它不起作用的原因。我将非常感谢任何帮助。 /*FishTank*/ impor
我用 Java 编写了一个简单的实用程序,可以让鼠标单击任意位置,每 X 毫秒一次: import java.awt.Robot; import java.awt.event.InputEvent;
我正在尝试做一些非常基本的事情:编写程序,在两点之间的框架上画一条线:按下鼠标的点和释放鼠标的点。 我有这些类(class): import java.awt.Graphics; public cla
我知道有很多关于 mousePressed 无法正常工作的线程,但我还没有找到问题的答案。我编写了一个程序,只是为了好玩和体验,但是我在使用 Robot 类的 mousePressed 方法时遇到了问
我正在制作有关处理的动画。然后,我有一个关于灯的问题。通常,我的代码比较长。但是,我制作了一个简单的代码,对初学者也很有用。 void setup() { size(400, 400, P3D);
当用户点击它时,我试图获取 JOGL 中像素的颜色。如果我将以下代码放入显示方法中(来自GLEventListener),它就可以正常工作。 FloatBuffer buffer = FloatBuf
所以我有一个标签,我想做下一个,默认情况下它以红色背景颜色开始,当我第一次单击(鼠标按下)时,我将背景颜色更改为绿色。 现在,我希望当我第二次按下它时,一旦我再次按下它,它就会变回红色。 就像是红色的
在 mousePressed 方法中,我无法使用任何与图形相关的方法,例如此代码将运行但不起作用 import org.newdawn.slick.GameContainer; import org.
我正在尝试在按钮 4 上使用鼠标事件。使用 时返回的值 int moused = MouseInfo.getNumberOfButtons(); 方法是5。除了右键单击按钮4之外,我不知道还有什么按钮
我有一小段原型(prototype)代码。 我试图让它在 mousePressed() 或 mouseClicked() 上单击的线条的颜色会改变。 我这辈子都无法弄清楚! 任何帮助将不胜感激! 我到
在下面的代码中,点击标签本应使其隐藏,但该方法从未被调用!我在这里错过了什么? package com.hermaryopto.lib.output.message; import java.
我正在编写一个程序,鼠标监听器 mousePressed() 似乎没有响应。我现在已经编写了一些 GUI 程序,并且比较了这些代码,我没有发现任何显着差异可以解释缺少鼠标监听的原因。以下代码不完整,其
我创建了一个 foreach 循环,它创建 ImageView 并使用 javafx 显示它们,但是找到的每个 ImageView 都必须有一个类似于 mousepressed 事件的事件,我已经编写
import java.awt.Frame; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt
我正在 Mac OSX 上使用 Eclipse 测试一个简单的 java 图形程序,代码如下: public void mousePressed(MouseEvent e) { System.
我已经安装了所有 p5 并且工作正常,但是每当我开始使用 mousePressed() 它就不起作用(keyPressed 也不起作用)。这是有问题的部分: //press play butt
我正在尝试构建一个简单的应用程序,当您在屏幕上按下/单击时它会播放一首歌曲。一旦您按下/单击,涟漪效应就会发生,同时也会产生视觉效果。 每次按下/单击时都会发生涟漪,但在我的代码中(每次单击都会重新启
我正在尝试访问 mousePressed ProcessingJS 片段中的属性,但得到 undefined。 这是我到目前为止尝试过的: $(document).ready(function(
为下面的函数运行println(game.gameOn) 仍然输出false。 为什么会这样模棱两可? 解决方案是什么? 也许我不能将对象作为参数传递给函数 mousePressed() void m
我创建了几个用于显示值的 JLabels(来自 MySQL),它们必须通过 mousePressed 进行切换,但每次单击后所有值都会覆盖其他值。 在显示新值时需要清除以前的文本吗?也许有比 JLab
我是一名优秀的程序员,十分优秀!