I have a game (standalone client application in this case) for which I do active rendering.
I launch a Frame object, and have a separate thread that calls an activePaint() method, in which I do my drawing. This all works great.
So now I’ve added a window bar, which is just a Rectangle at the top of the frame, where I draw an image. Using Listeners, I tried to implement mouse drag / window moving based on mouse interactions at the location of the window bar.
What I get is some pretty bad wobbling when the window is moved, though it does sort of drunkenly work. I’m not sure why, though. Hoping someone may have a suggestion for me.
Here are some pieces of relevant code:
In the main Frame’s activePaint():
public void activePaint() {
Graphics myGraphics = getGraphics();
bufferGraphics.drawImage( mainBackground, 0, 0, this );
if( DRAGGING_WINDOW ) {
setLocation( MainFrame.X_LOC, MainFrame.Y_LOC );
return;
}
myGraphics.drawImage( imageBuffer, 0, 0, this );
myGraphics.dispose();
}
In the listener - mousePressed():
if( MainFrame.TOP_BAR_BOUNDS.contains( clickX, clickY ) ) {
MainFrame.LAST_X_DRAG = clickX;
MainFrame.LAST_Y_DRAG = clickY;
MainFrame.DRAGGING_WINDOW = true;
}
In the listener = mouseReleased():
if( MainFrame.TOP_BAR_BOUNDS.contains( clickX, clickY ) ) {
MainFrame.DRAGGING_WINDOW = false;
}
And the entire mouseDragged:
public void mouseDragged( MouseEvent e ) {
int clickX, clickY;
clickX = e.getX();
clickY = e.getY();
// Main window controls
if( MainFrame.DRAGGING_WINDOW ) {
int xDragDiff = clickX - MainFrame.LAST_X_DRAG;
int yDragDiff = clickY - MainFrame.LAST_Y_DRAG;
MainFrame.X_LOC = MainFrame.X_LOC + xDragDiff;
MainFrame.Y_LOC = MainFrame.Y_LOC + yDragDiff;
MainFrame.LAST_X_DRAG = clickX;
MainFrame.LAST_Y_DRAG = clickY;
}
}