CatchtheCreature

Hi, I have this game that I'm trying to get working. It's called CatchtheCreature.  The point of the game is to have a creature pop up at a random location on the screen at random times, and your supposed to try to click on him. Which will increment a counter that shows how many times you've clicked him. Also, if you click him he's supposed to disappear and then show up at another random location. I have the creature class pretty much done but I'm having a really hard time figuring out how to get the CreaturePanel class to make the creature show up at the random location and random time and the whole click thingy.  Below are my two classes. Any help would be appreciatted. Thank you.


//********************************************************************
//  CreaturePanel.java  
//  Demonstrates the use of graphical objects.
//********************************************************************

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;

public class CreaturePanel extends JPanel implements ActionListener, MouseListener
{
      private Random generator = new Random();//instantiates a new Random generator
      private Creature creature = new Creature();  //instantiates a new creature object.
      private Timer timer1, timer2; //instantiates two a new Timer
      private int DELAY;
      JLabel label = new JLabel();
      private Point p = null;
      int ClickCount;
      
      
      public CreaturePanel()
      {
            DELAY = generator.nextInt(2000) + 100;
            timer = new Timer(DELAY, this);
            setBackground (Color.white);
      setPreferredSize (new Dimension(400, 400));
            add(label);
            timer.start();
            this.addMouseListener(this);
      }
      
      
      
            public void actionPerformed(ActionEvent event)
         {
               // generate a new random location         
                  int x = generator.nextInt(getWidth() - creature.getWidth());    //image is 34 x 68
                    int y = generator.nextInt(getHeight() - creature.getHeight());
                  // send the coordinates to the Creature class.
                  creature.move(x, y);
                  ClickCount = creature.getClickCount();
                  
         }
                              
            
            
            
            public void mouseClicked (MouseEvent event)
            {
                  Point p = event.getPoint();
            creature.Clicked(p);
                  
                  // generate a new random location         
                  int x = generator.nextInt(getWidth() - creature.getWidth());    //image is 34 x 68
                    int y = generator.nextInt(getHeight() - creature.getHeight());
                  // send the coordinates to the Creature class.
                  creature.move(x, y);
                  ClickCount = creature.getClickCount();
            
            }
         
            public void mouseMoved (MouseEvent event) {}
      public void mousePressed (MouseEvent event) {}
      public void mouseExited (MouseEvent event) {}
      public void mouseDragged (MouseEvent event) {}
            public void mouseReleased (MouseEvent event) {}
            public void mouseEntered (MouseEvent event) {}
      
      
      public void paintComponent(Graphics g)
      {
            super.paintComponent(g);

            // paint the creature here
            creature.paint(g);
            // Update the hit counter
            label.setText("Total successful hits: " + ClickCount);
            }
}



//********************************************************************
//  Creature.java  
//
//  Demonstrates the use of graphical objects.
//********************************************************************

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.awt.event.*;
import java.awt.Graphics;

public class Creature
{
      
      
      private int clicked = 0; //variable that keeps track of how many times the creature has been clicked.
      private ImageIcon Cimage; //instantiates a new ImageIcon.
      
      private int x, y;
      public boolean visible = true;
      
      
      
      //Constructor:
      
      public Creature()
      {
            Cimage = new ImageIcon ("cookiemonster.jpg");
      }
            
            public boolean Clicked(Point p) // Check whether mouse click catches the creature.
            {
                  if (p.x>=x && p.x<=(x+Cimage.getIconWidth()) && p.y>=y && p.y<=(y+Cimage.getIconHeight()))// point is in bounds of creature
                  {
                              clicked++;
                              return true;
                  }
                  else
                        return false;
            }
            
            
            public int getWidth()
            {
                  return Cimage.getIconWidth();
            }
            
            public int getHeight()
            {
                  return Cimage.getIconHeight();
            }
            
            public int getClickCount() // return the number of times that the image has been clicked.
            {
                  return clicked;
            }
            
            
            public void move(int xMove, int yMove) //tells the creature where to move
            {
                  x = xMove;                   //(int)p.getX();
               y = yMove;                    //(int)p.getY();
            }
            
            public void paint(Graphics g) // has the creature repaint itself
            {
                  g.drawImage(Cimage.getImage(), x, y, Color.white, null);
            }
            


      }
romeotheriaultAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
you need to call repaint() after you have moved the creature.

and update you label in mouseClicked() instead of paintComponent()
0
 
sciuriwareCommented:
Make the creature a button that is located in a null Layout, so you can set the exact location.
The creature is the icon of the button.
You need to revalidate the panel in which the button appears or disappears.
The painting you do is too much work, let SWING do the painting for you.
<*>
0
All Courses

From novice to tech pro — start learning today.