如何为jLabel图标的特定区域着色(知道坐标)

时间:2019-07-17 11:12:00

标签: java swing

我在jFrame上将png图像设置为jLabel图标,但是我想给图片的某些部分上色。 我该怎么办?

代码段(jFrame上的jLabel):

private javax.swing.JLabel jLabel2;
jLabel2 = new javax.swing.JLabel();

jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/planimavo/planimetrie/Planimetria AVO piano terra 2017-1.png"))); // NOI18N

jLabel2.setText("jLabel2");

jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
    jLabel2MouseClicked(evt);
}
});

1 个答案:

答案 0 :(得分:0)

您需要特殊的图标实现。这是一个示例,如何在鼠标单击点绘制正方形。 (左键单击-在鼠标单击位置绘制白色矩形,右键单击-停止绘制白色矩形)。

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
 * <code>OverlayIconExample</code>.
 */
public class OverlayIconExample {

    private static final int OVERPAINT_RECT_SIZE = 100;

    private final Icon icon = new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg");

    private final OverpaintIcon overIcon = new OverpaintIcon(icon);

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new OverlayIconExample()::startUp);
    }

    private void startUp() {
        JFrame frm = new JFrame("Icon overpaint");
        JLabel label = new JLabel(overIcon);
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e)) {
                    overIcon.setPoint(e.getPoint());
                } else {
                    overIcon.setPoint(null);
                }
                label.repaint();
            }
        });
        frm.add(label);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    private static class OverpaintIcon implements Icon {

        private final Icon backgroundIcon;

        private Point point;

        /**
         * @param backgroundIcon
         */
        public OverpaintIcon(Icon backgroundIcon) {
            this.backgroundIcon = backgroundIcon;
        }

        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            backgroundIcon.paintIcon(c, g, x, y);
            g.setColor(Color.WHITE);
            if (point != null) {
                g.fillRect(point.x, point.y, OVERPAINT_RECT_SIZE, OVERPAINT_RECT_SIZE);
            }
        }

        @Override
        public int getIconWidth() {
            return backgroundIcon.getIconWidth();
        }

        @Override
        public int getIconHeight() {
            return backgroundIcon.getIconHeight();
        }

        public Point getPoint() {
            return point;
        }

        public void setPoint(Point point) {
            this.point = point;
        }

    }
}