2

So I'm trying to make this program that allows the user to tag photos using java (kinda like Facebook tagging). I have already done loading the image, and making mouselistener when the user clicks an area of the image.

How do I make a JTextField appear when the user clicks a certain area of the photo?

I'm thinking that the JTextField can somewhat be the box where the user can enter his/her name as a tag for the photo.

Also, where do you think I should put the JTextField code? In main?

alicedimarco
  • 325
  • 1
  • 5
  • 20

3 Answers3

4

You can get the X and Y co-ordinates (as said by Daggeto). And then you can show your text field with setVisible(true)

1

MouseEvent.getX() and MouseEvent.getY() returns the horizontal x ant vertical y position of the event relative to the source component.

Then if you your image area described as x1,x2,y1,y2 you can check is clicked position in this area by this 'if':

int x0 = MouseEvent.getX();
int y0 = MouseEvent.getY();

if(x0>x1 && x0<x2 && y0>y1 && y0<y2){
    JTextField.setVisible(true);
}
Daggeto
  • 943
  • 1
  • 8
  • 17
  • `public class Mouse extends MouseAdapter{ public void mouseClicked(MouseEvent e){ x = e.getX(); y = e.getY(); object.drawing(x,y); } } ` So I just basically put that if statement in here? I have not made my JTextField yet though. Ugh I'm sorry, I do not know how to indent D: – alicedimarco Nov 02 '11 at 13:43
  • I think so. And when i said JTextField i meant object of this class. – Daggeto Nov 02 '11 at 13:46
1

just use the setVisible() function on the JTextField object and set its value whenever the user a certain portion of the image.

Jeris
  • 2,355
  • 4
  • 26
  • 38