Jump to content

question to know the event in which image?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
stack

stack

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
hi,
i have a question:ex: if i have 4 images (image1, image2, image3, image4) and i want message1 appear if i click in image1, message2 appear if i click in image2, message3 appear if i click in image3, and message4 appear if i click in image4

how can I know if the mouse event in image1 or 2 or 3 or 4 without using the position of these images in the board?

i want a method to tell me the event in which image...

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Use the getSource() method.

[highlight="Java"]public void actionPerformed(ActionEvent e){
if(e.getSource().equals(image1)) {
//show message 1
} else if(e.getSource().equals(image2)) {
//show message 2
}
[/highlight]

#3
stack

stack

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
thank you,, and i will try it

#4
stack

stack

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
but why if i click in the image1 in this code the message didn't appear and the event didn't work??


import javax.swing.JApplet;

import java.awt.event.MouseListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseMotionListener;

import java.awt.Graphics;

import javax.swing.ImageIcon;

import javax.swing.JOptionPane;




public class SS extends JApplet

{

private ImageIcon image1;


public void init(){

image1 = new ImageIcon("instruction.jpg");

}


public void paint(Graphics g){ //paint


 image1.paintIcon(this,g,0,0);


  addMouseListener(


    new MouseAdapter()

	 {//MouseAdapter


	 public void mousePressed(MouseEvent e){ //press


    if(e.getSource().equals(image1)) {  //msg


    System.out.printf("YOU CLICK IN IMAGE1");


	 JOptionPane.showMessageDialog( null, "GOOD MORNING ", 

    "HI",JOptionPane.INFORMATION_MESSAGE  );


} //msg


}//press

	 }//MouseAdapter


	 );//addMouseListener


} //paint





}//SS


#5
stack

stack

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
:confused:

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Not sure at the moment, but you may need to implement MouseListener

#7
stack

stack

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
can you try my code please???

#8
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
You added a mouse listener to image1.

#9
stack

stack

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

Sidewinder said:

You added a mouse listener to image1.

i didn't understand what did you mean??:confused:
please explain more..
why if i click in image1 in this code the message didn't appear and the event didn't work??

#10
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I meant to say - You never added a mouse listener to image1.

Simple doing a
[highlight=Java]addMouseListener()[/highlight]
Adds the mouse listener to the root content panel. You have to add the mouse listener to the object you want it to listen to a la
[highlight=Java]image1.addMouseListener()[/highlight]

#11
stack

stack

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

Sidewinder said:

I meant to say - You never added a mouse listener to image1.

Simple doing a
[highlight=Java]addMouseListener()[/highlight]
Adds the mouse listener to the root content panel. You have to add the mouse listener to the object you want it to listen to a la
[highlight=Java]image1.addMouseListener()[/highlight]

the event didn't work with me..and this error appear during compile:
SS.java:21: cannot find symbol
symbol : method addMouseListener(<anonymous java.awt.event.MouseAdapter>)
location: class javax.swing.ImageIcon
image1.addMouseListener(
^
1 error