TITLE:JDesktopPaneにJInternalFrameを吸着させる

JDesktopPaneにJInternalFrameを吸着させる

編集者:Terai Atsuhiro~

作成日:2007-01-01
更新日:2021-03-21 (日) 04:15:18
  • category: swing folder: MagneticFrame title: JDesktopPaneにJInternalFrameを吸着させる tags: [DesktopManager, JDesktopPane, JInternalFrame] author: aterai pubdate: 2007-01-01T08:38:12+09:00 description: JDesktopPaneとJInternalFrameの距離が近くなった場合、これらを自動的に吸着させます。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPnXoiDZI/AAAAAAAAAeQ/9SMGwoIqOi8/s800/MagneticFrame.png

概要

JDesktopPaneJInternalFrameの距離が近くなった場合、これらを自動的に吸着させます。

概要

JDesktopPaneとJInternalFrameの距離が近くなった場合、これらを自動的に吸着させます。

サンプルコード

#spanend
#spanadd
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
#spanend
#spanadd
desktop.setDesktopManager(new MagneticDesktopManager());
#spanend

#spandel
#screenshot
#spanend
#spanadd
// ...
#spanend
#spanadd
class MagneticDesktopManager extends DefaultDesktopManager {
#spanend
  @Override public void dragFrame(JComponent frame, int x, int y) {
    Container c = SwingUtilities.getAncestorOfClass(JDesktopPane.class, frame);
    if (c instanceof JDesktopPane) {
      JDesktopPane desktop = (JDesktopPane) c;
      int e = x;
      int n = y;
      int w = desktop.getSize().width - frame.getSize().width - e;
      int s = desktop.getSize().height - frame.getSize().height - n;
      if (isNear(e) || isNear(n) || isNear(w) || isNear(s)) {
        super.dragFrame(frame, getX(e, w), getY(n, s));
      } else {
        super.dragFrame(frame, x, y);
      }
    }
  }

#spandel
**サンプルコード [#o8501b2d]
#spanend
 private class MagneticListener extends MouseInputAdapter {
   private final JInternalFrame frame;
   private final Point loc = new Point();
   public MagneticListener(JInternalFrame frame) {
     this.frame = frame;
   }
   public void mousePressed(MouseEvent e) {
     Point p1 = frame.getLocation();
     Point p2 = SwingUtilities.convertPoint(frame, e.getPoint(), frame.getDesktopPane());
     loc.setLocation(p2.x-p1.x, p2.y-p1.y);
   }
   public void mouseDragged(MouseEvent me) {
     JDesktopPane desktop = frame.getDesktopPane();
     Point ep = me.getPoint();
     Point pt = SwingUtilities.convertPoint(frame, ep.x-loc.x, ep.y-loc.y, desktop);
     int e = pt.x;
     int n = pt.y;
     int w = desktop.getSize().width -frame.getSize().width -e;
     int s = desktop.getSize().height-frame.getSize().height-n;
     if(isNear(e) || isNear(n) || isNear(w) || isNear(s)) {
       int x = (e<w)?(isNear(e)?0:e):(isNear(w)?w+e:e);
       int y = (n<s)?(isNear(n)?0:n):(isNear(s)?s+n:n);
       desktop.getDesktopManager().dragFrame(frame,x,y);
     }
   }
   private boolean isNear(int c) {
     return (Math.abs(c)<10);
   }
 }
  private static int getX(int e, int w) {
    return e < w ? isNear(e) ? 0 : e : isNear(w) ? w + e : e;
  }

-&jnlp;
-&jar;
-&zip;
  private static int getY(int n, int s) {
    return n < s ? isNear(n) ? 0 : n : isNear(s) ? s + n : n;
  }

#spandel
**解説 [#oa45d781]
#spanend
#spandel
上記のサンプルでは、JDesktopPaneとJInternalFrame(タイトルがMagneticFrameの方だけ)の距離が10pt以下になった場合、DesktopManager#.dragFrame(JInternalFrame,int,int)メソッドを使ってJInternalFrameの位置を変更することで、吸着を行っています。
#spanend
  private static boolean isNear(int c) {
    return Math.abs(c) < 10;
  }
#spanadd
}
#spanend
#spanadd
View in GitHub: Java, Kotlin
  • 課題
    • アイコン化している時に吸着できない

解説

DesktopManager#dragFrame(JInternalFrame,int,int)メソッドをオーバーライドすることでJInternalFrameの配置座標を調整しています。上記のサンプルでは、JDesktopPaneJInternalFrameの距離が10px以下になった場合、それぞれの辺が吸着するよう設定しています。

コメント

参考リンク

コメント