Search code examples
javaimageswingpaintcomponentmouselistener

How do i switch images using mouse events?


Im working on an assignment where an image moves around and when the user clicks on the image it will change and as soon as it moves again via timer class, the images chages back to the original. As of now I can click the image to change it, but it wont change back when it is time to move again. Is there a way to change back after it moves?

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=900;
private Random generator = new Random();  
private ImageIcon image, image1;
private Timer timer;
private int x, y;  
private int catchCount=0; 

public Creature()  
{  
    image = new ImageIcon ("pikachu.png");
    image1 = new ImageIcon ("pokeball.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) && between(event.getX(), x, x + image.getIconWidth()) &&  between(event.getY(), y, y + image.getIconHeight()))
                {
                    System.out.println("CAUGHT ONE!");
                    catchCount++;
                    move();
                    image=image1;

                }

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

}
}

Solution

  • i hope i understand what do you try to achieve. first you need 3 Images:

    private ImageIcon imageToDraw, image1, image2;
    

    Creature will look like this now:

    public Creature()  
    {  
        image1 = new ImageIcon ("pikachu.png");
        image2 = new ImageIcon ("pokeball.png");
        imageToDraw = image1;
        ...
    }
    

    in move() you should set the image to image1:

    public void move()
    {
        imageToDraw = image1;
        timer.start();
        x = generator.nextInt( 1900 );
        ...
    }
    

    dont forget to change image to imageToDraw in paint():

    public void paintComponent(Graphics page)
    {  
        super.paintComponent(page);  
        imageToDraw.paintIcon (this, page, x, y);
        ...
    }
    

    remove move(); from onclick-event and change image to imageToDrwa in click-action:

    public void mouseClicked(MouseEvent event)
       {  
    
           if((event.getButton() == MouseEvent.BUTTON1) && between(event.getX(), x, x + image.getIconWidth()) &&  between(event.getY(), y, y + image.getIconHeight()))
                {
                    System.out.println("CAUGHT ONE!");
                    catchCount++;
                    //move(); should be removed
                    imageToDraw=image1;
    
                }
    
       }
    

    edit:move(); removed from onclick-event