java - 在秋千图标的顶部绘画?
问题描述
我正在渲染一堆包含图标的 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
}
任何帮助将不胜感激,谢谢!
解决方案
对的,这是可能的:
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();
行将执行组件的默认绘制。我们只是简单地在它上面涂漆。
推荐阅读
- asp.net-mvc - IIS express 未运行 MVC 站点,未在 applicationhost.config 中创建站点
- ios - Swift iOS - 如何在单元格的 superView 中获取按钮框架
- php - `where in` 只取存储过程 mysql 中的第一个值
- android - 在 Android 中执行 oAuth 1.0 API
- javascript - Wordpress Owl Carousel 不显示自定义帖子类型
- node.js - 3.4版本的mongodb不区分大小写索引
- c# - 发布 Visual Studio 项目导致错误(项目正在构建并正确运行)
- swift - Swift - 有没有办法匹配错误而不抛出?
- docker - 使用 docker-compose push 将容器推送到注册表
- javascript - 如何在 Vue2 中通过 HTML 运行组件的方法