0

I have picture as a JLabel and what I want to do is when I click the JLabel, there would be another Jlabel in a form of rectangle would appear.

I tried using paintComponent or paint and unfortunately, it doesn't work for my program. So I have to think that there are other ways, other than paintComponent and/or paint.

The aim is tag to a certain part of an image with a name, like in Facebook.

10
  • What is the ultimate use-case here? What feature is this implementing? I would tend to use an undecorated JButton to hold the Image. As to how to make the Rectangle appear, I would need to know what this is all about to give the best advice. Dec 1, 2011 at 10:11
  • Just like in facebook, when I click I want for a rectangle to appear. When you said that you use undecorated JButton, does that mean that the rectangle of the button have a rectangle hole in it? Dec 1, 2011 at 10:29
  • "Just like in facebook.." I don't use facebook. What feature does it offer by doing that? "does that mean that the rectangle of the button have a rectangle hole in it?" I meant an image in a button instead of "picture as a JLabel .. click the JLabel". Dec 1, 2011 at 10:32
  • The feature it offer is to tag a certain part of an image with a name. The picture is in the JPanel as a JLabel. I tried to use paintComponent but it doesn't work so I resorted to making the image as a JLabel. Dec 1, 2011 at 10:36
  • 1
    As someone that does use Facebook I think I can supply some of the big picture. Given an image with several people's faces the goal is to annotate the image with the names of the people in it. The way this happens is by clicking on the image. A (let's say) 50x50 rectangular border shows up where you click. If you don't like where it is you can move it around. The goal is to surround a face with the border. Once that is done you type the name into a separate dialog and the hit submit. The server now knows the name of the face at that position.
    – Pace
    Dec 1, 2011 at 13:02

1 Answer 1

0

Wow.

Based on Pace's description of the problem, you're going to have to do the following (I think):

  • The picture will have to be painted directly on a JPanel. This is so when someone clicks on the picture, you can get the mouse x and y coordinates through the JPanel action listener.

  • You will then create a JDialog with a transparent JPanel that has a border, a text box for the name, and an OK button together. (Might as well put everything together in one dialog window.) The JDialog will be movable, but you're going to have to create a JDialog listener that keeps track of the x and y coordinates of the top left edge or the center of the transparent JPanel in the JDialog.

The JDialog JPanel won't really be transparent. You'll have to create the illusion of transparency by noting the position of the JDialog JPanel on the picture JPanel, and copying the part of the image from the picture JPanel to the JDialog JPanel.

The rest should be rather straightforward, compared to getting the JDialog to work properly.

Edited to add: Here's an extension of JPanel that will draw a picture directly on the JPanel and process mouse pressed events.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

public class PicturePanel extends JPanel {

    private static final long serialVersionUID = 1L;

    protected Image picture;

    public PicturePanel(Image picture) {
        this.picture = picture;
        createPartControl();
    }

    protected void createPartControl() {
        new JPanel();
        int width = picture.getWidth(getParent());
        int height = picture.getHeight(getParent());
        addMouseListener(new CoordinateListener());
        setPreferredSize(new Dimension(width, height));
    }

    @Override 
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int width = picture.getWidth(getParent());
        int height = picture.getHeight(getParent());

        g.drawImage(picture, 0, 0, width, height, null);
    }

    public class CoordinateListener extends MouseInputAdapter {
        @Override
        public void mousePressed(MouseEvent event) {
            int x = event.getX();
            int y = event.getY();
            System.out.println("(" + x + ", " + y + ")");
        }
    }

}
2
  • Thank you for the response, the problem is that the paintComponent doesn't work on my program. That's why I resorted to using the JLabel for the image. Dec 2, 2011 at 8:38
  • @javaBeginner: Edited my answer to give you the skeleton of the JPanel that paints a picture. Dec 2, 2011 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.