[英]How to force repaint of a glass pane?
I have a Swing app with a glass pane over a map. 我有一个在地图上带有玻璃窗格的Swing应用程序。
It paints dots at certain positions. 它在某些位置绘制点。 When I click somewhere on the map, and the glass pane receives the message CONTROLLER_NEW_POLYGON_MARK
I want do display an additional dot at the position specified in the event data (see MyGlassPane.propertyChange
). 当我在地图上的某个位置单击时,玻璃窗格会收到消息CONTROLLER_NEW_POLYGON_MARK
我希望在事件数据中指定的位置显示一个附加点(请参阅MyGlassPane.propertyChange
)。
The glass pane class is called MyGlassPane
. 玻璃窗格类称为MyGlassPane
。 Using the debugger I validated that addPointToMark
is actually called in propertyChange
. 使用调试器,我验证了addPointToMark
实际上是在propertyChange
调用的。
But no additional dots appear on the screen. 但是屏幕上没有其他点出现。
How can I change the code so that PointSetMarkingGlassPane.paintComponent
is called whenever an event ( IEventBus.CONTROLLER_NEW_POLYGON_MARK
) is fired? 如何更改代码,以便在PointSetMarkingGlassPane.paintComponent
事件( IEventBus.CONTROLLER_NEW_POLYGON_MARK
)时调用IEventBus.CONTROLLER_NEW_POLYGON_MARK
?
public class PointSetMarkingGlassPane extends JComponent implements IGlassPane {
private final ILatLongToScreenCoordinatesConverter latLongToScreenCoordinatesConverter;
private final List<Point.Double> pointsToMark = new LinkedList<Point.Double>();
public PointSetMarkingGlassPane(final ILatLongToScreenCoordinatesConverter aConverter) {
this.latLongToScreenCoordinatesConverter = aConverter;
}
protected void addPointToMark(final Point.Double aPoint)
{
if (aPoint != null)
{
pointsToMark.add(aPoint);
}
}
@Override
protected void paintComponent(final Graphics aGraphics) {
for (final Point.Double pointToMark : pointsToMark)
{
final Point positionInScreenCoords = latLongToScreenCoordinatesConverter.getScreenCoordinates(pointToMark);
drawCircle(aGraphics, positionInScreenCoords, Color.red);
}
}
private void drawCircle(Graphics g, Point point, Color color) {
g.setColor(color);
g.fillOval(point.x, point.y, 10, 10);
}
}
public class MyGlassPane extends PointSetMarkingGlassPane implements PropertyChangeListener {
public MyGlassPane(ILatLongToScreenCoordinatesConverter aConverter) {
super(aConverter);
addPointToMark(DemoGlassPane.ARTYOM);
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (IEventBus.CONTROLLER_NEW_POLYGON_MARK.equals(evt.getPropertyName()))
{
addPointToMark((Point.Double)evt.getNewValue());
invalidate();
}
}
}
As I think invalidate()
only flags your component to check sizes and layout. 我认为invalidate()
仅标记您的组件以检查大小和布局。 You should call repaint()
to repaint your pane. 您应该调用repaint()
重新绘制窗格。
Also I am wondering why you use propertyChangeListener for mouse clicks. 我也想知道为什么您使用propertyChangeListener进行鼠标单击。 I would prefer just simple mouse listener + MouseAdapter and MouseEvent x, y, buttons state. 我只希望使用简单的鼠标侦听器+ MouseAdapter和MouseEvent x,y,按钮状态。
invalidate()
probably won't help you, as it flags a component for layout changes, not painting changes. invalidate()
可能对您没有帮助,因为它标记了组件以进行布局更改,而不是绘画更改。 Why not call repaint()
instead? 为什么不调用repaint()
呢?
For better performance, you could call the repaint
method which takes a Rectangle (or four ints representing a rectangle), so that only the newly added point is repainted; 为了获得更好的性能,可以调用repaint
方法,该方法采用一个Rectangle(或四个int代表一个矩形),以便仅重新绘制新添加的点; I would suggest changing the return type of addPointToMark
from void to java.awt.Point, and have it return the result of latLongToScreenCoordinatesConverter.getScreenCoordinates
, so MyGlassPane can derive a rectangle from that Point which can then be passed to a repaint method. 我建议将addPointToMark
的返回类型从void更改为java.awt.Point,并使其返回latLongToScreenCoordinatesConverter.getScreenCoordinates
的结果,以便MyGlassPane可以从该Point派生一个矩形,然后将该矩形传递给重绘方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.