首页 > 解决方案 > 在秋千图标的顶部绘画?

问题描述

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

在此处输入图像描述

我用来封装包含我的 JLabels的航点组件的类:

/**
 * 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
    }

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

标签: javaswingpaintcomponent

解决方案


对的,这是可能的:

        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);
            }
        };

在这里,我已经覆盖了类的paintComponent方法JLabel。该super.paintComponent();行将执行组件的默认绘制。我们只是简单地在它上面涂漆。


推荐阅读