TheNamesZero
TheNamesZero

Reputation: 39

How do i record a mouse event on a image that keeps moving?

I am trying to figure out how to keep track the number of times a user clicks onto an image i have on a panel. As of right now, it only records anywhere on the panel rather than just on the image. The image keeps moving and the user is trying "catch" it by clicking onto it. So, how do i only keep track of the number of times the image is clicked?

Here is my code:

Main:

import java.awt.*;
import javax.swing.*;

public class Catch_The_Creature 
{
    public static void main(String[] args) 
    {
        JFrame frame = new JFrame("Catch the Creature");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JOptionPane.showMessageDialog(frame, "Catch Pikachu!");
        frame.getContentPane().add(new Creature());
        frame.pack();
        frame.setVisible(true);
    }
}

Panel:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Creature extends JPanel 
{   
    private static final int DELAY=700;
    private Random generator = new Random();  
    private ImageIcon image;
    private Timer timer;
    private int x, y;  
    private int catchCount=0;  

    public Creature()  
    {
        image = new ImageIcon ("pikachu.png");
        timer =  new Timer(DELAY, new MoveListener());
        x = generator.nextInt( 1900 );
        y = generator.nextInt(1000);
        addMouseListener (new MouseClickedListener());  
        setBackground (Color.green);  
        setPreferredSize(new Dimension(1900,1000));
        timer.start();
    }  

    //Draws the image.
    public void paintComponent(Graphics page)
    {  
        super.paintComponent(page);  
        image.paintIcon (this, page, x, y); 
        page.drawString("Pikachus Captured: " + catchCount, 10, 35);  
        setFont(new Font("Arial", Font.BOLD,35));
    }

    //Method for moving the image.
    public void move()
    {
        timer.start();
        x = generator.nextInt( 1900 );
        y = generator.nextInt(1000);

        if (timer.isRunning())
        {
            x = generator.nextInt( 1900 );
            y = generator.nextInt(1000);    
        }
        repaint();
    }

    //Method for getting the number of times caught.
    public int getCatchCount()  
    {  
        return catchCount;  
    }  

    //Makes the image move
    private class MoveListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            move();
            repaint();  
        } 
    } 

    //Records when the user clicks the image.
    private class MouseClickedListener extends MouseAdapter  
    {  
        public void mouseClicked(MouseEvent event)
        {      
            if (event.getButton() == MouseEvent.BUTTON1)
            {
                catchCount++;
            }
        }
    }

    //Unused
    public void mouseEntered(MouseEvent arg0) {}
    public void mouseExited(MouseEvent arg0) {}
    public void mousePressed(MouseEvent arg0) {}
    public void mouseReleased(MouseEvent arg0){}
}

Upvotes: 0

Views: 400

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347234

You know 4 things, the x/y position and width/height of the image.

From this, you could create a Rectangle and use the Rectangle#contains method to test if the mouse point is within the given area

Updated with example

@Override
public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 1) {
        Rectangle bounds = new Rectangle(x, y, image.getIconWidth(), image.getIconHeight());
        if (bounds.contains(e.getPoint())) {
            catchCount++;
        }
    }
}

Upvotes: 1

isnot2bad
isnot2bad

Reputation: 24444

Just check, if the mouse position is within the boundaries of the image:

public void mouseClicked(MouseEvent event) {  
    if ((event.getButton() == MouseEvent.BUTTON1) &&
        between(event.getX(), x, x + image.getIconWidth()) &&
        between(event.getY(), y, y + image.getIconHeight()) {

        catchCount++;
    }
}

private static boolean between(int x, int lower, int upper) {
    return (x >= lower) && (x <= upper);
}

Upvotes: 0

DoubleDouble
DoubleDouble

Reputation: 1493

Notice that your

addMouseListener (new MouseClickedListener());

is added to your Creature() class, which extends JPanel.

So your panel is a creature, when you probably want your panel to have a creature instead.

If it were me, I would make Creature extend JComponent instead, and add a creature to a regular JPanel.

Upvotes: 0

Related Questions