Trouble dragging a custom window

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;
		}
		
	}

If you can use Java 1.6 this seems easier.

import java.awt.Color;
import java.awt.Graphics;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;

public class Test extends JFrame implements KeyListener, MouseInputListener {
    private boolean locked = true;
    private int offsetX;
    private int offsetY;
    
    public Test() {
        setSize(400, 300);
        addKeyListener(this);
        setUndecorated(false);
        JPanel p = new JPanel() {
            public void paint(Graphics g) {
                super.paint(g);
                g.setColor(Color.RED);
                g.fillRect(0, 0, 400, 20);
            }
        };
        p.addMouseListener(this);
        p.addMouseMotionListener(this);
        setContentPane(p);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String args[]) {
        new Test();
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            System.exit(0);
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {}
    @Override
    public void keyTyped(KeyEvent e) {}
    @Override
    public void mouseDragged(MouseEvent e) {
        if(locked) {
            setLocation(e.getXOnScreen() - offsetX, e.getYOnScreen() - offsetY);
        }
    }
    @Override
    public void mouseMoved(MouseEvent e) {}
    @Override
    public void mouseClicked(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {}
    @Override
    public void mouseExited(MouseEvent e) {}
    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1) {
            offsetX = e.getX();
            offsetY = e.getY();
            if(offsetX < 400 && offsetY < 20) {
                locked = true;
            }
        }
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1) {
            locked = false;
        }
    }
}

Thanks for the response. Not sure if I can move to 1.6, perhaps so, though.

Is it “getXOnScreen()” that is coming from 1.6?

You can easily emulate that behaviour by traversing up the component hierarchy (until you found a Window/Frame/Dialog) and use getLocation() on every component.

Got this working, thanks all.