Take the 2-minute tour ×

Possible Duplicate:
I want to create an invisible clickable object over an image in Java

So I want my java game to respond in a specific way when the user clicks within certain x and y values. Specifically, I want the game to move to an image in an array when the user clicks on a certain part of the image. I already have a class that has an array with all of the images in it, and that automatically paints the first image. It's done in JFrame. Also, however I get the program to do this, should I create a new class that extends this one and then place all of the code for getting the game to respond to certain clicks within the new class? Here's my code so far:

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

public class drawPictures extends JFrame implements MouseListener { //it implements this because I want the user to click stuff
    //Now I need to declare the images that serve as my levels variables ahead of time.
    protected static Image levelOne;
    protected static Image levelTwo;
    protected static Image levelThree;
    protected static Image levelFour;
    protected static Image levelFive;
    protected Graphics g = this.getGraphics();  
    //Done declaring.

    //Now to load the images
    private static Image loadImage(String imgFileName) { 
        Image img = null;
        try {
            Toolkit tk = Toolkit.getDefaultToolkit();
            img = tk.getImage(imgFileName);
        } catch (Exception e) {
            System.err.println("Image not found: "+ imgFileName);

        return img;
    } //done loading the images

 static Image [] pictureArray = new Image[5]; { //This is the array that will store all of the images
     //otherwise known as the "levels"
 pictureArray[0] = levelOne; //each "slot" in the array is taken up by one 
 //of the images that serves as the level
 pictureArray[1] = levelTwo;
 pictureArray[2] = levelThree;
 pictureArray[3] = levelFour;
 pictureArray[4] = levelFive;

  * Now that the actual array that stores the levels has been created
  * I need to create a method that "paints" the level, 
  * and moves on to the next one when the user clicks on something.
  * I also need to create a box with dimensions 151x159 
  * overtop of the happy tomato in the first level.
  * That will be the 
 public drawPictures() {
     super("One of These Things Doesn't Belong...");

     setSize(1500, 750);
     setDefaultCloseOperation(EXIT_ON_CLOSE); // Creates the "x" box.
     setVisible(true); // Makes the window visible.    


 public void paint(Graphics g) {
     g.drawImage(pictureArray[0], 100, 100, this);

 public static void start() 
  * this entire method exists for the sole purpose of loading the images
  * that I placed in the variables that I declared above.
    levelOne = loadImage("Level 1.jpg");
    levelTwo = loadImage("Level 2.jpg");
    levelThree = loadImage("Level 3.jpg");
    levelFour = loadImage("Level 4.jpg");
    levelFive = loadImage("Level 5.jpg");

    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

     * @param args
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new drawPictures(); 
share|improve this question

marked as duplicate by Andrew Thompson, durron597, DocMax, Kurtis Nusbaum, evilone Dec 7 '12 at 5:21


I don't see your jpanel and where are you adding your images? –  Smit Dec 6 '12 at 0:58
"move to an image" What does that mean? Change to display that image? –  Andrew Thompson Dec 6 '12 at 4:35

1 Answer 1

I guess that you search some to get to know certain x and y values. That is possilbe via MouseEvent check documentation an concretely look at method of this class getPoint().

somethhing like this can work

public void mouseClicked(MouseEvent arg0) {

Point p = arg.getPoint();

share|improve this answer
