bluesky11
bluesky11

Reputation: 15

Mouselistener not working with if statement

I have one last error in a 2d program game in which the player clicks on a randomly appearing object(essentially whack a mole) and then shows how many times the player caught the creature.

Everything works except when trying to increment the player's score when the target is pressed. If I turn off the if statement, the MouseListener increments by +1 every time I press the mouse. It appears that the error is in my 'if' statement. I had an original suggestion by the compiler when using '.equals' that said the statement is unlikely, so I investigated the data types and changed the if statement to use '==' instead of '.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();
  }

}
}

Any suggestions to help point me in the right direction. Apologies in advance for sloppy code. Thanks.

Upvotes: 0

Views: 374

Answers (1)

slipperyseal
slipperyseal

Reputation: 2778

You are checking the exact position of the creature - the top left corner.

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

So the user will have to click on the exact pixel of the top left corner of the creature. Try something like this which accounts for the creatures size in pixels..

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

This will give you a simple box to click within. You could also try to detect where you have clicked on actual pixels of the creature rather then the background.

Upvotes: 1

Related Questions