在秋千图标的顶部作画?

Paint over the top of a swing Icon?

我正在渲染一堆包含图标的 JLabel。我想通过在它们的顶部绘制某种线条或符号来有条件地注释这些图标。是否可以在 swing 中的图标上绘制?下面是我的 JLabel 的示例:

我用来封装包含我的 JLabel 的 waypoint 组件的 class:

/**
 * Waypoint to be drawn on the map
 */
public class Waypoint extends DefaultWaypoint {
    protected JLabel label;
    private final long id;
    private final EntityType type;

    public Waypoint( long id ) {
        this.id = id;
        this.type = null;
        this.label = null;
    }

    public Waypoint( long id, EntityType type, Location coord ) {
        super( locToGeoPos(coord) );
        this.id = id;
        this.type = type;
        this.label = new JLabel();
    }

    public void setIcon( ImageIcon icon ) {
        label = new JLabel( icon, JLabel.CENTER );
        label.setBorder( new LineBorder(Color.BLACK) );

      }

    public void setBackgroundColor( Color bgrColor ) {
        label.setOpaque( true );
        label.setBackground( bgrColor );
    }

    public void setToolTipText( String tooltipText ) {
        label.setToolTipText( "<html>" + tooltipText + "</html>" );
    }

    public JLabel getLabel() {
        return label;
    }

    public void annotateIcon() {
        // TODO
    }

任何帮助将不胜感激,谢谢!

是的,有可能:

        Image img = ImageIO.read(new File("IMAGE PATH HERE"));
        ImageIcon icon = new ImageIcon(img);

        JLabel label = new JLabel(icon, JLabel.CENTER) {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.RED);
                g.drawOval(0, 0, 10, 10);
            }
        };

这里,我覆盖了JLabelclass的paintComponent方法。 super.paintComponent(); 行将执行组件的默认绘制。之后我们简单地在上面涂漆。

是的,你可以这样做。这是一个完整的工作示例。您可以适当缩放图标。这绘制了图像。等待 2 秒,然后在图像上绘制一条对角红线。

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DrawingOverIcon extends JPanel {
    
    
    static JFrame f = new JFrame();
    
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }
    
    public static void main(String[] args) {
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DrawingOverIcon doi = new DrawingOverIcon();
        ImageIcon icon = new ImageIcon("f:/download.png");
        JLabel label = new JLabel(icon);
        doi.setBackground(Color.white);
        f.add(doi);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        
        doi.add(label);
        doi.repaint();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
        }

        // From here down is what you would do to modify the
        // icon.
        Image image = icon.getImage();
        BufferedImage bi = new BufferedImage(image.getWidth(null),
                image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        
        Graphics2D g2d =
                (Graphics2D) bi.getGraphics();
        g2d.drawImage(image,0,0,null);
        g2d.setStroke(new BasicStroke(10));
        g2d.setColor(Color.RED);
        g2d.drawLine(0, 0, 500, 500);
        icon.setImage(bi);
        doi.repaint();
    }
    
}