title: Using mouseEntered
body: <p>I'm trying to make a game where you die when the object rock hit you but I'm failing to use the MouseEntered method. Could someone show me how? I also want to ask about loading all the sprite image. I success to load them in but fail to make the rocks change from image to image.</p>
  
  <p>Tester class</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.image.BufferedImage;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Random;
  
  import javax.swing.JFrame;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  
  
  public class Tester extends JPanel{ 
  
      public JFrame window = new JFrame("Rock stuff");
      public Timer tmr ;
      public ArrayList&lt;Rock&gt; rocks = new ArrayList&lt;Rock&gt;();
  
  
      public Tester(){
          window.setBounds(500, 400, 500, 500);
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.setVisible(true);
          window.add(this);
  
  
  
          tmr = new Timer(1, new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  for(Rock r:rocks)
                      r.move();
                  repaint();
              }
          });
  
          this.addMouseListener(new MouseListener() {
  
              @Override
              public void mouseReleased(MouseEvent e) { }
  
              @Override
              public void mousePressed(MouseEvent e) {
                  for(Rock r:rocks) {
                      if(r.contains(e.getPoint())){
                          Rock newRock = r.split();
                          if(newRock != null){
                              rocks.add(newRock);
                              rocks.add(newRock);
                          }
                          else{
                              rocks.remove(r);
                              if(rocks.size()==0){
                                  tmr.stop();
                                  repaint();
                                  JOptionPane.showMessageDialog(window, "Game Over");
                              }
                          }
                          return;
  
                      }
                  }
              }
  
              @Override
              public void mouseExited(MouseEvent e) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void mouseEntered(MouseEvent e) {
                  for(Rock r:rocks){
                      if(r.contains(e.getPoint())){
                          rocks.clear();
                          JOptionPane.showMessageDialog(window, "Game Over");
                      }
                  }
  
              }
  
              @Override
              public void mouseClicked(MouseEvent e) {
  
              }
          });
          addrock(10);
  
  
          tmr.start();;
      }
  
  
      private void addrock(int count) {
          Random rnd = new Random();
          for(int i=0;i&lt;count;i++)
              rocks.add(new Rock( rnd.nextInt(getWidth()),
                      rnd.nextInt(getHeight()),
                      32,32,
                      (rnd.nextBoolean()?1:-1),
                      (rnd.nextBoolean()?1:-1),
                      true
                      ) );
      }
  
      public void paint(Graphics g){
          super.paint(g);
          Rock.sw = getWidth();
          Rock.sh = getHeight();
          for(Rock r:rocks) r.draw(g);
  
      }
  
      public static void main(String[] args) {
          new Tester();
      }
  }
  </code></pre>
  
  <p>Rocks class</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Graphics;
  import java.awt.Rectangle;
  import java.awt.image.BufferedImage;
  import java.io.IOException;
  import java.util.Random;
  
  public class Rock extends Rectangle{
  
      public static int sw, sh;
  
      public int dx, dy;
      public boolean isMoving;
      public BufferedImage sprite;
      //================================================= Constructors
  
      public Rock(int x, int y, int width, int height, int dx, int dy, boolean isMoving) {
          super();
          this.width = width;
          this.height = height;
          this.x = x;
          this.y = y;
          this.dx = dx;
          this.dy = dy;
          this.isMoving = isMoving;
          this.sprite = init();
      }
  
      //================================================= Methods
      public BufferedImage init(){
          BufferedImageLoader loader = new BufferedImageLoader();
          BufferedImage spriteSheet = null;
          try {
              spriteSheet = loader.loadImage("hero.png");
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          SpriteSheet ss = new SpriteSheet(spriteSheet);
          sprite = ss.grabSprite(0, 0, 32, 32);
          return sprite;
      }
  
      public void move() {
          if(isMoving == false) return;
  
          x += dx;
          y += dy;
  
          if(x + width &gt; sw) {
              x = sw - width;
              dx = -dx;
          }
  
          if(y + height &gt; sh) {
              y = sh - height;
              dy = -dy;
          }
  
          if(x &lt; 0) {
              x = 0;
              dx = -dx;
          }
  
          if(y &lt; 0) {
              y=0;
              dy = -dy;
          }
      }
  
      public Rock split(){
          Rock r = null;
          Random rnd = new Random();
          if(width &gt; 20){
              dx=(rnd.nextBoolean()?1:-1);
              dy=(rnd.nextBoolean()?1:-1);
              r = new Rock(x,y,width,height,dx,dy,true);
          }
  
          return r;
      }
  
      public void draw(Graphics g) {
          g.drawImage(sprite,x,y,null);
      }
  
  }
  </code></pre>