gpt4 book ai didi

java - 使用 MouseMotionListener 时出现问题

转载 作者:行者123 更新时间:2023-12-02 13:03:20 26 4
gpt4 key购买 nike

我不久前才开始编码。我现在正在尝试使用 JPanel 制作 FruitNinja 类型的游戏,并且我几乎完成了所有操作(除了使游戏继续进行的循环以及其余的水果)/炸弹动画)

到目前为止,我只对 Apple.png 进行了动画处理(完成此操作后将开始对其他水果进行动画处理)

我正在尝试创建一个 checkHit 方法来检查我的鼠标是否经过图像(切过水果),因此 MouseMotionListener 需要使用。如果鼠标确实经过水果,我需要它消失/移出框架。

那么如何从 MouseMotionListener 检索坐标,检查它是否在我的水果的 40 像素范围内,如果在,则移动框架的该水果?如果可能的话,不仅要这样做,而且完成后还要播放声音。

我有另一位用户帮助我解决了这个问题,但我仍然不太明白。

import javax.swing.*;//imports JPanel class
import java.awt.*;//imports the Graphics class
import java.awt.event.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
//imports packages needed for the hovering


public class FruitNinja extends JPanel{


private Image dojo;
private Image apple;
private Image orange;
private Image pear;
private Image strawberry;
private Image banana;
private Image bomb;

//declares the import image variables

private javax.swing.Timer timer;

private int dx, dy;

private int vx =40;
//velocity of the object moving in the x direction
private int x=-10;

private int vy=40;
//velocity of the object moving in the y direction
private int y=770;


private int divide=(int)(2*Math.random())+1;//sets random number
//determines when the fruit will bounce off

public FruitNinja() { // a constructor to set up graphics windo
super();
setBackground(Color.WHITE);
loadImage();
dx = 25;
dy = 25;

timer = new javax.swing.Timer( 30, new TimerListener() );
//interval of 10 milliseconds
timer.start();
//timer is a facility for threads to schedule tasks for future execution in a background thread.
}

private void loadImage() {
ImageIcon ii = new ImageIcon("Dojo.jpg");
dojo = ii.getImage();

ImageIcon oo = new ImageIcon("Orange.ico");
orange = oo.getImage();

ImageIcon ss = new ImageIcon("Strawberry.png");
strawberry = ss.getImage();

ImageIcon bb = new ImageIcon("Banana.png");
banana = bb.getImage();

ImageIcon pp = new ImageIcon("Pear.png");
pear = pp.getImage();

ImageIcon aa = new ImageIcon("Apple.png");
apple = aa.getImage();

ImageIcon bo=new ImageIcon("Bomb.png");
bomb=bo.getImage();


//loads neccaseary images for this game


}@Override

public void paintComponent(Graphics g){ // draw graphics in the panel

super.paintComponent(g);// to make panel display correctly
g.drawImage(dojo, 0,0, this);
//draws out dojo
g.drawImage(apple, x,y, this);
g.drawImage(orange, -300,-300, this);
g.drawImage(pear, -300,-300, this);
g.drawImage(banana, -300,-300, this);
g.drawImage(strawberry, -300,-300, this);
//draws out the fruits somewhere
g.drawImage(bomb,-300,-300,this);
//draws out the bomb that will make the player lose

}

private class TimerListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

//implements the ActionListenter


x+=vx; //adds velocity to the x, which makes it travel in x direction

if (x<0){

vx=(-1*vx);
//tells the velocity to change direction
}

y+=vy; //adds velocity to the y, which makes it travel in y direction
if (y>=getHeight()||y<=getHeight()/4){

vy=(-1*vy);
//tells the velocity to change direction
}

repaint(); // causes paintComponent to be called by the JVM

}


}

public static void main(String[] args) {

FruitNinja panel = new FruitNinja(); // window for drawing
JFrame f = new JFrame(); // the program itself
f.setTitle("Fruit Ninja");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//when the X button is clicked, the program quits
f.setSize(1280,800);//size of the frame
Container pane = f.getContentPane();//pane refers to the interior of the JFrame

FruitNinja p1 = new FruitNinja();

pane.add(p1);//add the FacePanel object to the interior of the frame
f.setVisible(true);

}
}

如果我对这里的内容做错了什么或/以及如何在其中使用这个 MouseMotionListner ,请告诉我。我对此还很陌生,所以我不知道如何检索坐标或设置“MouseMotionListener”。如果我的问题不清楚,请随时进入评论并让我知道情况是否如此;我愿意更彻底地解释这个问题。

谢谢

编辑:

是这个方法吗?

      private class HandleMouse extends MouseAdapter {
//extends MouseAdapter rather than implement Mouse Adapter
public void mousePressed( MouseEvent e ){
Rectangle hitBox = new Rectangle(e.getX() - 20, e.getY() - 20, 40, 40);
Rectangle appleHitBox=new Rectangle(ax-128,ay-128,256,256);

Area area = new Area(hitBox);
area.intersect(new Area(appleHitBox));
if(!area.isEmpty()){
ax=-500;
ay=-500;
avx=0;
avy=0;
}
else{
numOfMisses++;

}
}
}

最佳答案

So how do I retrieve the coordinates from MouseMotionListener,

MouseMotionListener 传递 MouseEvent到方法中,其中包含事件的各种属性,这包括事件在组件坐标上下文中的坐标

check to see if it is within 40 pixels of my fruit and if it is

所以,这是基本的碰撞检测,您可以通过多种可能的方法来实现此目的,但由于我很懒,我可能会考虑使用矩形来表示用户点击框

Rectangle hitBox = new Rectangle(e.getX() - 20, e.getY() - 20, 40, 40);

这会生成一个围绕 MouseEvent 中心点 20 像素的框。

“但是为什么呢?”你问?因为Rectangle有许多有用的方法可用于碰撞检测,包括

  • 交叉路口
  • 相交
  • 联合

仅举几例

因此,给定水果的 x/y 位置和大小,您可以简单地创建第二个 矩形 并确定第一个是否命中第二个

and if it is, to move that fruit off the frame?

嗯,这将是一个设置标志的过程,“主循环”(即您的 TimerListener)告诉它水果已被击中,现在由“主循环”将其从“绘制”列表中删除并设置下一个对象

If possible, not only to do that but to also play a sound when that is done

作为一般答案,是。一旦您能够检测到发生了碰撞,播放声音就相对简单,当它检测到发生了碰撞时,我会再次尝试使用“主循环”来执行此操作

关于java - 使用 MouseMotionListener 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44238231/

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