Java-Gaming.org
Java4K - to go         Javadoc:
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, peek at the official java tutorials or join us at irc #jgo.
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Trouble dragging a custom window  (Read 601 times)
0 Members and 1 Guest are viewing this topic.
Offline Noobtastic

JGO n00b
*

Posts: 46



« on: 2010-03-03 19:02:09 »

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

Offline CaptainJester

JGO Neuromancer
****

Posts: 1127
Medals: 6


Make it work; make it better.


« Reply #1 on: 2010-03-04 09:35:14 »

If you can use Java 1.6 this seems easier.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
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;
        }
    }
}

Offline Noobtastic

JGO n00b
*

Posts: 46



« Reply #2 on: 2010-03-04 10:49:16 »

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?

Games published by our own members! Go get 'em!
Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5509
Medals: 204


Hand over your head.


« Reply #3 on: 2010-03-04 14:59:19 »

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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Noobtastic

JGO n00b
*

Posts: 46



« Reply #4 on: 2010-03-04 16:56:19 »

Got this working, thanks all.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.128 seconds with 21 queries.