如何在不断移动的图像上录制鼠标事件?

时间:2013-12-04 22:54:50

标签: java swing listener

我想弄清楚如何跟踪用户点击我在面板上的图像的次数。截至目前,它只记录面板上的任何位置,而不是仅记录在图像上。图像不断移动,用户通过点击它来“抓住”它。那么,我如何仅跟踪图像被点击的次数?

这是我的代码:

主:

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);
    }
}

面板:

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){}
}

3 个答案:

答案 0 :(得分:1)

你知道4件事,图像的x / y位置和宽度/高度。

由此,您可以创建一个Rectangle并使用Rectangle#contains方法测试鼠标点是否在给定区域内

更新了示例

@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++;
        }
    }
}

答案 1 :(得分:0)

请注意你的

addMouseListener (new MouseClickedListener());

被添加到您的Creature()类中,该类扩展了JPanel。

所以,当你可能希望你的小组改为拥有一个生物时,你的小组是一个生物。

如果是我,我会让生物扩展JComponent,并将生物添加到常规JPanel。

答案 2 :(得分:0)

如果鼠标位置在图像边界内,请检查:

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);
}