Skip to main content

dragging

4 replies [Last post]
bran
Offline
Joined: 2005-07-14

Thanks for the wonderful little gem!

I'm doing a map on a JPanel and have implemented a grid on top of it with special UI. However the paintLayer() is not called when I drag the underlying map and is only called when I release the mouse.

Why is this?

Right now I'm trying to hack a solution on the mouse_dragged event and want to call the paintLayer() on every such event. But How can I grab a Graphics2D to call the event?

Any help is appreciated!

Bing

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pietblok
Offline
Joined: 2003-07-17

Hi Bing,

No need to call paintLayer. Just invoke setDirty(true). Something like the following example:

[code]
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;

import org.jdesktop.jxlayer.JXLayer;
import org.jdesktop.jxlayer.plaf.AbstractLayerUI;

public class GridLayerUI extends AbstractLayerUI {

@Override
protected void paintLayer(Graphics2D g, JXLayer layer) {
super.paintLayer(g, layer);
// paint the grid
}

@Override
protected void processMouseMotionEvent(MouseEvent event, JXLayer layer) {
super.processMouseMotionEvent(event, layer);
if (event.getID() == MouseEvent.MOUSE_DRAGGED) {
setDirty(true);
}
}

}
[/code]

Piet

bran
Offline
Joined: 2005-07-14

Hi Piet,

Thanks for your help.

However setDirty() caused extreme flickering for me while dragging. The underlying image layer jumped with every move.

I have came up with a hack which seemed to work with sligh flickering. The grid unfortunately disappears when I paused during drag.

AbstractLayerUI gridUI = new AbstractLayerUI() {
boolean isDragging = false;
@Override
protected void paintLayer(Graphics2D g2, JXLayer l) {
// this paints layer as is
if (!isDragging) {
super.paintLayer(g2, l);
}
// custom painting:
int w = l.getWidth();
int h = l.getHeight();
g2.setColor(new Color(192, 212, 192, 128));
g2.drawLine(0, h / 2, w, h / 2);
g2.drawLine(w / 2, 0, w / 2, h);
}

@Override
protected void processMouseEvent(MouseEvent e, JXLayer l) {
if (e.getID() == MouseEvent.MOUSE_RELEASED) {
this.isDragging = false;
}
}

@Override
protected void processMouseMotionEvent(MouseEvent e, JXLayer l) {
if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
this.isDragging = true;
this.paintLayer((Graphics2D) l.getGraphics(), l);
}
}
};

Any fix?

Bing

pietblok
Offline
Joined: 2003-07-17

Hi Bing,

Have you considered moving your dragging code as a whole from your customized JPanel to your GridUI? I guess the following is happening:

1) The dragging is detected by your customized JPanel and it repaints itself following the mouse movement (your GridUI is unaware of this)
2) The dragging mouse event is detected by your GridUI that again paints the complete view.

Piet

bran
Offline
Joined: 2005-07-14

That's an interesting idea. I'll think about it.

Thanks!