Getting the X and Y position of the mouse when it is clicked on the image in JLabel
I have a Image displayed inside JLabel using ImageIcon feature.
The JLabel is contained inside a JPanel.
When image is clicked, I want to get the mouse position relative to the image.
I am trying to solve this problem by adding mouse li开发者_运维百科stener to the JLabel which contains the image.
Apparently, even if I set the size of JLabel with the width and height of the image, it seems that JLabel automatically stretches out to the size of JPanel.
So when below code runs, if the size of image is smaller than the JPanel, getting X and Y position returns the unwanted value.
Is there a way that I can specifically limit the size of the JLabel so that getting X position and Y position of the mouse will work fine?
Or better way to get a mouse position relative to the image? (which would be better)
ImageIcon icon = new ImageIcon(image);
JLabel imageLabel = new JLabel(icon);
imageLabel.setSize(image.getWidth(null), image.getHeight(null));
imageLabel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(imageLabel, e.getX()+" "+e.getY());
}
});
imageLabel.validate();
panel.add(imageLabel);
panel.validate();
It is all about the layout.
See this example for the above image.
It would seem this use-case requires a GridBagLayout
or BoxLayout
.
Your probably seeing problems because the Layout manager is controlling the size of your label, not the setSize() call.
I'd probably create my a JPanel derived class (instead of a JLabel) that implements paintComponent by drawing the image. Then i'd override getPreferredSize() and getMaximumSize() of the JPanel to return the dimensions of the image.
Using the label will cause troubles because there's going to be padding, offsets and such.
it seems that JLabel automatically stretches out to the size of JPanel.
problem is here
imageLabel.setSize(image.getWidth(null), image.getHeight(null));
if is there only one JLabel
, then
myPanel.setLayout(new BorderLayout());//JPanel has by default FlowLayout
myPanel.add(myLabel, BorderLayout.CENTER)
if is there more that one JLabel with Image/IconImage and should be better look for GridLayout
EDIT
in all cases (for reall picture) before anything
1) you have to determine picture ratio 3:2, 4:3 16:10 ..., determine CropRatio then you have to :
setPreferredSize(new Dimension(x/cropRatio, y/cropRatio)) for JLabel
by using paintComponent g2d.fillRect(0, 0, x/cropRatio, y/cropRatio);
2) put JLabel to the JScrollPane, but then will be hold original amout of pixels
Your probably seeing problems because the Layout manager is controlling the size of your label, not the setSize() call.
I'd probably create my a JPanel derived class (instead of a JLabel) that implements paintComponent by drawing the image. Then i'd override getPreferredSize() and getMaximumSize() of the JPanel to return the dimensions of the image.
Using the label will cause troubles because there's going to be padding and such.
But in the end, what's causing our label to expand is the layout manager. Most like the easiest way to do what you want is to make sure your layout manager isn't expanding the label, by picking a layout manager that keeps the component at it's preferred size.
精彩评论