Mouselistener无法与if语句一起使用

时间:2018-07-12 02:40:39

标签: java

在2D程序游戏中,我犯了最后一个错误,即玩家单击一个随机出现的物体(本质上是重击一颗痣),然后显示玩家捕获该生物的次数。

一切正常,但在按下目标时尝试增加玩家的得分时除外。如果关闭if语句,则每次按下鼠标时MouseListener都会增加+1。看来错误在我的'if'语句中。编译器在使用'.equals'时提出了一个原始建议,即该语句不太可能,因此我调查了数据类型,并将if语句更改为使用'=='而不是'.equals'。

package activity1;

import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.Random;

public class CreaturePanel extends JPanel {

Random CreatureGenerator = new Random();

private final int WINDOW_WIDTH = 1000;
private final int WINDOW_LENGTH = 1000;
private Point point = null;
private int creaturecounter;
private final int creatureSize = 20;
private int creatureXPosition = 500;
private int creatureYPosition = 500;
private JLabel CreatureLabel;
private JTextField CreatureText;
private Timer timer;

public CreaturePanel () {               
CreatureLabel = new JLabel("Number of times Creature Caught: ");
CreatureText = new JTextField(10);
add(CreatureLabel);
add(CreatureText);          
addMouseListener(new CreatureMouseListener());          
timer = new Timer(5000, new CreatureListener());            
this.setFocusable(true);            
timer.start();
}       

public void paintComponent(Graphics page)
{
  super.paintComponent(page);
  page.setColor(Color.red);
  page.fillOval(getCreatureXPosition(), getCreatureYPosition(), creatureSize, creatureSize);
  }  

package activity1;

import javax.swing.JFrame;

public class CatchTheCreature {    

public static void main(String[] args) {
   JFrame frame = new JFrame("Catch the Creature!");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
   frame.setSize(1000, 1000);

   CreaturePanel PanelFinal = new CreaturePanel();
   frame.getContentPane().add(PanelFinal);              
   frame.pack();
   frame.setVisible(true);          
  }    
}

public int getCreatureYPosition() {
    return creatureYPosition;
}    

public void setCreatureYPosition(int creatureYPosition) {
    this.creatureYPosition = creatureYPosition;
}    

public int getCreatureXPosition() {
    return creatureXPosition;
}    

public void setCreatureXPosition(int creatureXPosition) {
    this.creatureXPosition = creatureXPosition;
}

private class CreatureMouseListener implements MouseListener {

   public void mousePressed (MouseEvent event)
    {       
        if(getCreatureXPosition() == event.getX() && getCreatureYPosition() == event.getY())
        {
            creaturecounter+=1;
            CreatureText.setText(Integer.toString(creaturecounter));
            repaint();
            }
        }        

    public void mouseClicked(MouseEvent arg0) {}
    public void mouseEntered(MouseEvent arg0) {}
    public void mouseExited(MouseEvent arg0) {}
    public void mouseReleased(MouseEvent arg0) {}   
}
private class CreatureListener implements ActionListener
{                   
public void actionPerformed(ActionEvent event)
{
  setCreatureXPosition(CreatureGenerator.nextInt(WINDOW_WIDTH));
  setCreatureYPosition(CreatureGenerator.nextInt(WINDOW_LENGTH));    
  repaint();
  }

}
}

任何有助于我指出正确方向的建议。事先为草率的代码道歉。谢谢。

1 个答案:

答案 0 :(得分:1)

您正在检查生物的确切位置-左上角。

    if (getCreatureXPosition() == event.getX() &&
        getCreatureYPosition() == event.getY())

因此,用户将必须单击生物左上角的确切像素。尝试使用类似的方法来说明生物的像素大小。

    if (event.getX() >= getCreatureXPosition() &&
         event.getX() < getCreatureXPosition() + creatureSize &&
         event.getY() >= getCreatureYPosition() &&
         event.getY() < getCreatureYPosition() + creatureSize)

这将为您提供一个简单的单击框。您还可以尝试检测您单击该生物的实际像素的位置,而不是背景。