简体   繁体   中英

Get Icon position inside JLabel

I'm trying to display a grid (and other items) over the Icon of a JLabel, but i can't figure out how to get the icon coordinates according to its layout.

PS Since i need also mouse support i can't just hack my paint operations inside the icon using setIcon()

public class ExtendedJLabel extends JLabel implements MouseMotionListener
{
    private final int GRID_STEP = 16;

    public ExtendedJLabel()
    {
        super();
        setOpaque(true);
        setBackground(UIManager.getColor("controlShadow");
        addMouseMotionListener(this);
    }

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

        if(icon == null)
            return;

        // Icon Origin
        int ox = ...;
        int oy = ...;

        // Draw grid
        g.setColor(UIManager.getColor("controlShadow"));

        for(int row = 0; row < getIcon().getIconHeight() / GRID_STEP; row++)
            g.drawLine(ox, oy + row * GRID_STEP, ox + getIcon().getIconWidth(), oy + row * GRID_STEP);

        for(int col = 0; col < getIcon().getIconWidth() / GRID_STEP; col++)
            g.drawLine(ox + col * GRID_STEP, oy, ox + col * GRID_STEP, oy + getIcon().getIconHeight());
    }

    @Override
    public void mouseMoved(MouseEvent me)
    {
        // ...
    }

    @Override
    public void mouseDragged(MouseEvent me)
    {
        // ...
    }
}

i can't figure out how to get the icon coordinates according to its layout.

I believe you can use the SwingUtiltities.layoutCompoundLabel(...) method.

You can:

  1. create a custom JLabel and override the paintComponent() method to paint the grid, or
  2. use a JLayer .

Since I haven't played with JLayer much, here is an example using this approach:

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

public class LabelIconGridUI extends LayerUI<JComponent>
{
    @Override
    public void paint(Graphics g, JComponent c)
    {
        super.paint(g, c);

        Graphics2D g2 = (Graphics2D) g.create();
        g2.setColor( Color.ORANGE );

        JLayer jlayer = (JLayer)c;
        JLabel label = (JLabel)jlayer.getView();

        Rectangle viewR = new Rectangle();
        viewR.width = label.getSize().width;
        viewR.height = label.getSize().height;
        Rectangle iconR = new Rectangle();
        Rectangle textR = new Rectangle();

        String clippedText = SwingUtilities.layoutCompoundLabel
        (
            label,
            g2.getFontMetrics(),
            label.getText(),
            label.getIcon(),
            label.getVerticalAlignment(),
            label.getHorizontalAlignment(),
            label.getVerticalTextPosition(),
            label.getHorizontalTextPosition(),
            viewR,
            iconR,
            textR,
            label.getIconTextGap()
        );

        int gridSize = 10;
        int start = iconR.x + gridSize;
        int end = iconR.x + iconR.width;

        for (int i = start; i < end; i += gridSize)
        {
            g2.drawLine(i, iconR.y, i, iconR.y + iconR.height);
        }

        g2.dispose();
    }

    private static void createAndShowUI()
    {
        JLabel label = new JLabel("Some Text" );
        label.setIcon( new ImageIcon( "DukeWaveRed.gif" ) );

        label.setVerticalAlignment( JLabel.CENTER );
        label.setHorizontalAlignment( JLabel.CENTER );
        label.setVerticalTextPosition( JLabel.BOTTOM );
        label.setHorizontalTextPosition( JLabel.CENTER );

        LayerUI<JComponent> layerUI = new LabelIconGridUI();
        JLayer<JComponent> layer = new JLayer<JComponent>(label, layerUI);

        JFrame frame = new JFrame("Label Icon Grid");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( layer );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM