麻烦使用MouseMotionListener

时间:2017-05-29 08:47:51

标签: java swing boolean jpanel mousemotionlistener

我刚开始编码了一会儿。我正在尝试使用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++;

        }
}
}

1 个答案:

答案 0 :(得分:1)

  

那么如何从MouseMotionListener

中检索坐标

MouseMotionListenerMouseEvent传递给方法,该方法包含事件的各种属性,其中包括组件坐标上下文中事件的坐标

  

检查它是否在我的水果的40个像素内,如果它是

所以,这是基本的碰撞检测,有很多可能的方法可以解决这个问题,但是因为我很懒,我可能会考虑用Rectangle代表用户点击框

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

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

&#34;但为什么?&#34;你问?因为Rectangle有许多可用于碰撞检测的有用方法,包括

  • intersection
  • intersects
  • union

列举一些

因此,考虑到水果的x / y位置和大小,您只需创建第二个Rectangle并确定第一个是否达到第二个

  

如果是的话,将水果从框架上移开?

嗯,这将是一个设置标志的过程,其中主要循环&#34; (即你的TimerListener)告诉它水果已被击中,现在它已经到了主循环#34;将它从&#34; paint&#34;中删除列出并设置下一个对象

  

如果可能的话,不仅可以这样做,还可以在完成后播放声音

作为一般答案,是。一旦你能够发现碰撞已经发生,播放声音相对简单,我再次试图使用&#34;主循环&#34;当它检测到命中已经发生时,也可以这样做