So, I have a map, that I want to be able to draw rectangles on to highlight an area. When the mouse is released a permanent rectangle is drawn on the map that persists until the mouse is dragged again to start the creation of a new rectangle. While the mouse is being dragged the rectangle outline should be created as it goes along.
The persisting rectangle is removed when the mouse is reclicked which given the application would mean that a new drag event would be started.
What happens is that the first rectangle is painted correctly and everything is fine, but subsequent rectangles that are currently being dragged have the corner cut off (image link at bottom).
If I click and then wait for the image to repaint before I start dragging then this problem does not exist, as well as if I sleep the thread before beginning to draw the rectangle in onMouseDragged so that it has time to repaint.
I'd like a more elegant solution than those to allow the screen to repaint before the rectangle created in onMouseDragged is shown on the screen. So what is the best way to get the repaint to complete without zapping part of the drawn rectangle?
Note that despite the look of the outline, the persisting rectangle that gets drawn is correct.
This is what the rectangle looks like
public void onMousePressed(MapMouseEvent ev)
{
startPos = new Point(ev.getPoint());
drawer.removeDrawings(pane.getMapContent());
pane.repaint();
pane.setIgnoreRepaint(true);
}
public void onMouseDragged(MapMouseEvent ev)
{
super.onMouseDragged(ev);
if (enabled) {
ensureGraphics();
if (dragged)
{
// because we're in XOR mode, this has the effect of erases the previous rectangle
graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
}
else
{
}
rect.setFrameFromDiagonal(startPos, ev.getPoint());
graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
dragged = true;
}
}
public void onMouseReleased(MapMouseEvent ev)
{
super.onMouseReleased(ev);
if (dragged) {
ensureGraphics();
dragged = false;
drawer drawable = new drawer();
drawable.drawRectangle((Graphics2D) parentComponent.getGraphics().create(), rect.x, rect.y, rect.width, rect.height, pane.getMapContent());
graphics.dispose();
graphics = null;
pane.setIgnoreRepaint(false);
pane.repaint();
}
}