[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3953
  • Last Modified:

undecorated JFrame dragging

I have an undecorated JFrame (which also has a glasspane attached).  In my glasspane, I have a mouse adapter listening for drag events to the components that it stores (JLabels with icons).  I would like to be able to reposition the JFrame itself, but haven't been able to work out how to code that in.

In my glasspane, I have an ArrayList which stores all of the JLabels and I test against their positions on the drag event as such:

public void arrowIconDragged(java.awt.event.MouseEvent evt) {
       
        ///get point of mouse click
        Point p = evt.getPoint();
        JLabel l = null;
        Rectangle r = null;
       
        //compare point with all labels and move position if necessary
        for(int i=0; i<arrowImages.size(); i++) {
            l = (JLabel)arrowImages.get(i);
            r = l.getBounds();
            if(r.contains(p)) {
                l.setLocation(evt.getX() - l.getWidth()/2, evt.getY() - l.getHeight()/2 );
            }
        }        
    }

Now that must be a little inefficient seeing as how I test against every single object each time the mouse is moved a single pixel, but it's the only way I've gotten it to work thus far. How can I put in a check so that if it doesn't fall within one of the object, then the frame itself needs to be moved?  Since it's undecorated I don't have a window frame to grab.

Thanks,
David
0
lomidien
Asked:
lomidien
  • 2
  • 2
1 Solution
 
CEHJCommented:
Maybe something like:

Point mousePos = e.getPoint();
Component[] components = ...; // all your contained components
boolean moveWindow = true;
for(int i = 0;i < components.length;i++) {
      Rectangle bounds = components[i].getBounds();
      if (bounds.contains(mousePos)) {
            moveWindow = false;
            break;
      }
}
0
 
RomanRegaCommented:
If i get you, you have a JPanel, which you want to move.
To move it the user must drag some JLabel placed on the JFrame.
To do that you are using an ivible glassPanel to check where the user click and then find the relative JLabel.

Are you sure is the best idea.

Can't you add a MauseListener do every JLabel and get rid of the glassPanel.

I have no time now to do a proper test, but maybe you should do somehting like this (pseudo code):



int centerx=myJFrame.getWidth()/2;
int centery=myJFrame.getHeight()/2;

//link this evebt to ALL your JLabels
public void arrowIconDragged(java.awt.event.MouseEvent evt) {
    JLabel caller=(JLabel)evt.getComponent();
    Point loc=caller.getLocation();
   
    int dx=(loc.x+evt.getX())-centerx;
    int dy=(loc.y+evt.getY())-centery;
   
    Point frameLoc=myJFrame.getLocation();
    frameLoc.translate(dx, dy);
    myJFrame.setLoaction(frameLoc);//finally scroll the JPanel
}

I have no way to test it now. Test it and report me any problem. I will try to help you.
0
 
lomidienAuthor Commented:
Ok, I implemented the boolean test.......not sure why I didn't think of that.  :(  I'm appologize but perhaps I'm just not too good with dragging or maybe the coordinate system has be confused, but I did the following:

public void arrowIconDragged(java.awt.event.MouseEvent evt) {
       
        ///get point of mouse click
        Point p = evt.getPoint();
        JLabel l = null;
        Rectangle r = null;
        boolean moveWindow = true;
       
       
        //compare point with all labels and move position if necessary
        for(int i=0; i<arrowImages.size(); i++) {
            l = (JLabel)arrowImages.get(i);
            r = l.getBounds();
            if(r.contains(p)) {
                l.setLocation(evt.getX() - l.getWidth()/2, evt.getY() - l.getHeight()/2 );
                moveWindow = false;
            }                
        }
       
        if(moveWindow) {
            //current window position
            int xPos = parent.getLocation().x;
            int yPos = parent.getLocation().y;
           
            //mouse position relative to window
            int mxPos = evt.getX();
            int myPos = evt.getY();
            System.out.println(xPos + " " + yPos + " " + mxPos + " " + myPos);
           
            parent.setLocation(mxPos - xPos, myPos - yPos);
        }
       
       
    }

This makes the window coordinates reach -30,000 practically instantly.  The readout looks like this:
340 150 334 232
-6 82 680 300
686 218 -12 164
-698 -54 1372 436
2070 490 -1396 -108
-3466 -598 4139 980
7605 1578 -6932 -1196
-14537 -2774 15210 3156
29747 5930 -29074 -5548

Is there some blaring problem above?

Thanks,
David
0
 
lomidienAuthor Commented:
Yep, I probably should add the individual mouse listeners to each JLabel...and I'll do that.  Now the only remaining question is in terms of moving the panel.  I have to take into effect the current position of the mouse in relation to the frame in order for the user to be able to grab anywhere and have the frame actually transition nicely.  Where am I going wrong above?

Thanks,
David
0
 
RomanRegaCommented:
Maybe NOW i get you:
the JLabels are like anchor todrag to move the frame... Ok.

Sorry i understood something else before :)


try changhing like so in the source i gave you above:

//link this event to ALL your JLabels
public void arrowIconDragged(java.awt.event.MouseEvent evt) {
    JLabel caller=(JLabel)evt.getComponent();
    int dx=(evt.getX()-caller.getWitdth()/2);
    int dy=(evt.getY()-caller.getHeight()/2);
    Point frameLoc=myJFrame.getLocation();
    frameLoc.translate(dx, dy);
    myJFrame.setLocation(frameLoc);//finally scroll the JPanel
}

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now