2017-03-03 4 views
1

JPanelJDialog이 있고 여기에 탭 분할 창이 있습니다. 끌어서 놓기를 사용하여 고스트 이미지가 JDialog을 넘어서고 싶습니다. 커서가 화면 전체에서 움직일 수 있으므로 어떻게 유령 이미지를 따라갈 수 있습니까?드래그 앤 드롭 중에 유령 이미지를 탭 분할 창 대화 상자 위로 이동하는 방법은 무엇입니까?

package PlannerManager; 

import GUIUtils; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.datatransfer.DataFlavor; 
import java.awt.datatransfer.Transferable; 
import java.awt.dnd.*; 
import java.awt.event.*; 
import java.awt.geom.Rectangle2D; 
import java.awt.image.BufferedImage; 
import java.net.URL; 

public class DnDTabbedPane extends JTabbedPane 
{ 
    private int LINEWIDTH = 3; 
    private String NAME  = "TabTransferData"; 
    private DataFlavor FLAVOR = new DataFlavor(DataFlavor. 
           javaJVMLocalObjectMimeType, NAME); 

    private GhostClassPane glassPaneGhost = new GhostClassPane(); 
    private boolean  isDrawRect  = false; 
    private Rectangle2D lineRect  = new Rectangle2D.Double(); 
    private Color   lineColor  = new Color(0, 100, 255); 
    private TabAcceptor acceptor  = null; 
    private ImageIcon  iconImageCancel = null; 
    private Container  myParent  = null; 

    protected DetailsTabbedPaneDialog tabbedDetailsDlg = null; 

    // constructor 
    public DnDTabbedPane(Container parentCont, String, 
               iconCancelWithPath, 
         final Plans plans, final GUIUtils guiUtils, 
         final String name) 
    { 
     super(); 
     myParent = parentCont; 

     final DragSourceListener dsl = new DragSourceListener() 
     { 
      @Override 
      public void dragEnter(DragSourceDragEvent e) 
      { 
       e.getDragSourceContext().setCursor(
            new Cursor(Cursor.HAND_CURSOR)); 
      } 

      @Override 
      public void dragExit(DragSourceEvent e) 
      { 
       e.getDragSourceContext().setCursor(
             new Cursor(Cursor.HAND_CURSOR)); 
       lineRect.setRect(0, 0, 0, 0); 
       isDrawRect = false; 
       glassPaneGhost.setPoint(new Point(-1000, -1000)); 
       glassPaneGhost.repaint(); 
      } 

      @Override 
      public void dragOver(DragSourceDragEvent e) 
      { 
       System.out.println("drag exit"); 
       TabTransferData data = getTabTransferData(e); 
       if (data == null) 
       { 
        e.getDragSourceContext().setCursor(
             new Cursor(Cursor.HAND_CURSOR)); 
       } 
      } 

      @Override 
      public void dragDropEnd(DragSourceDropEvent e) 
      { 
       isDrawRect = false; 
       lineRect.setRect(0, 0, 0, 0); 
       glassPaneGhost.setVisible(false); 
       glassPaneGhost.setImage(null); 

       // If drop failed, create new JDialog with JTabbedPane 
       if (!e.getDropSuccess()) 
       { 
        Point dropLocation = e.getLocation(); 

        // Create new JDialog with JTabbedPane 
        tabbedDetailsDlg = new DetailsTabbedPaneDialog(
          myParent, plans, guiUtils, name, 
          dropLocation.x, dropLocation.y, false); 

        // Transfer the tab to the new JTabbedPane 
        tabbedDetailsDlg.tabbedPane.convertTab(
          getTabTransferData(e), 
          getTargetTabIndex(dropLocation)); 
       } 

       if (1 > getTabCount()) 
       { 
        ((JDialog) myParent).dispose(); 
       } 
      } 

      public void dropActionChanged(DragSourceDragEvent e) 
      { 
      } 
     }; 

     final DragGestureListener dgl = new DragGestureListener() 
     { 
      @Override 
      public void dragGestureRecognized(DragGestureEvent e) 
      { 
       Point tabPt = e.getDragOrigin(); 
       int dragTabIndex = indexAtLocation(tabPt.x, tabPt.y); 
       if (0 <= dragTabIndex) 
       { 
        initGlassPane(e.getComponent(), e.getDragOrigin(), 
                 dragTabIndex); 
        try 
        { 
         e.startDrag(DragSource.DefaultMoveDrop, 
           new TabTransferable(dragTabIndex), dsl); 
        } 
        catch (InvalidDnDOperationException ex) 
        { 
         ex.printStackTrace(); 
        } 
       } 
      } 
     }; 

     new DropTarget(this, DnDConstants.ACTION_MOVE, 
       new CDropTargetListener(), true); 
     new DragSource().createDefaultDragGestureRecognizer(this, 
        DnDConstants.ACTION_MOVE, dgl); 
     acceptor = new TabAcceptor() 
     { 
      @Override 
      public boolean isDropAcceptable(DnDTabbedPane component, 
                  int index) 
      { 
       return true; 
      } 
     }; 

     URL iconURL = 
      getClass().getClassLoader().getResource(iconCancelWithPath); 
     if (null != iconURL) 
     { 
      iconImageCancel = new ImageIcon(iconURL); 
     } 
    } 

    @Override 
    public void addTab(String title, final Component component) 
    { 
     JLabel label = new JLabel(title); 
     label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 12)); 
     JPanel tab = new JPanel(new BorderLayout()); 
     tab.setOpaque(false); 
     tab.add(label, BorderLayout.WEST); 
     JButton buttonCancel = new JButton(iconImageCancel); 
     if (null != iconImageCancel) 
     { 
      tab.add(buttonCancel, BorderLayout.EAST); 
     } 
     super.addTab(title, component); 
     int idx = indexOfComponent(component); 
     setTabComponentAt(idx, tab); 
     setSelectedIndex(getTabCount() - 1); 
     buttonCancel.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent e) 
      { 
       // See if the dialog is empty 
       if (1 >= getTabCount()) 
       { 
        ((JDialog) myParent).dispose(); 
       } 
       else 
       { 
        remove(component); 
       } 
      } 
     }); 
    } 

    private TabTransferData getTabTransferData(DropTargetDropEvent 
                 event) 
    { 
     try 
     { 
      return (TabTransferData) 
         event.getTransferable().getTransferData(FLAVOR); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    private TabTransferData getTabTransferData(DropTargetDragEvent 
                  event) 
    { 
     try 
     { 
      return (TabTransferData) 
         event.getTransferable().getTransferData(FLAVOR); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 


    private TabTransferData getTabTransferData(DragSourceDropEvent 
                    event) 
    { 
     try 
     { 
      return (TabTransferData) event.getDragSourceContext() 
        .getTransferable().getTransferData(FLAVOR); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 


    private TabTransferData getTabTransferData(DragSourceDragEvent 
                 event) 
    { 
     try 
     { 
      return (TabTransferData) event.getDragSourceContext() 
        .getTransferable().getTransferData(FLAVOR); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 


    class TabTransferable implements Transferable 
    { 
     private TabTransferData data = null; 

     public TabTransferable(int tabIndex) 
     { 
      data = new TabTransferData(DnDTabbedPane.this, tabIndex); 
     } 

     public Object getTransferData(DataFlavor flavor) 
     { 
      return data; 
     } 

     public DataFlavor[] getTransferDataFlavors() 
     { 
      DataFlavor[] flavors = new DataFlavor[1]; 
      flavors[0] = FLAVOR; 
      return flavors; 
     } 

     public boolean isDataFlavorSupported(DataFlavor flavor) 
     { 
      return (flavor.getHumanPresentableName().equals(NAME)); 
     } 
    } 

    class TabTransferData 
    { 
     private DnDTabbedPane tabbedPane = null; 
     private int   tabIndex = -1; 

     public TabTransferData(DnDTabbedPane tabbedPane_in, int 
                  tabIndex_in) 
     { 
      tabbedPane = tabbedPane_in; 
      tabIndex = tabIndex_in; 
     } 

     public DnDTabbedPane getTabbedPane() 
     { 
      return tabbedPane; 
     } 

     public int getTabIndex() 
     { 
      return tabIndex; 
     } 
    } 

    private Point buildGhostLocation(Point pointLocation) 
    { 
     return SwingUtilities.convertPoint(this, new 
              Point(pointLocation), 
       glassPaneGhost); 
    } 

    class CDropTargetListener implements DropTargetListener 
    { 
     public void dragEnter(DropTargetDragEvent e) 
     { 
      e.acceptDrag(e.getDropAction()); 
     } 

     public void dragExit(DropTargetEvent e) 
     { 
      isDrawRect = false; 
     } 

     public void dropActionChanged(DropTargetDragEvent e) 
     { 
     } 

     public void dragOver(final DropTargetDragEvent e) 
     { 
      repaint();    
      glassPaneGhost.setPoint(buildGhostLocation(
               e.getLocation())); 
      glassPaneGhost.repaint(); 
     } 

     @Override 
     public void drop(DropTargetDropEvent e) 
     { 
      if (isDropAcceptable(e)) 
      { 
       convertTab(getTabTransferData(e), 
          getTargetTabIndex(e.getLocation())); 
       e.dropComplete(true); 
      } 
      else 
      { 
       e.dropComplete(false); 
      } 

      isDrawRect = false; 
      repaint(); 
     } 

     public boolean isDropAcceptable(DropTargetDropEvent e) 
     { 
      boolean bRet = false; 

      Transferable transferable = e.getTransferable(); 
      if (null != transferable) 
      { 
       DataFlavor[] flavor = e.getCurrentDataFlavors(); 
       if (transferable.isDataFlavorSupported(flavor[0])) 
       { 
        TabTransferData data = getTabTransferData(e); 

        if (null != data) 
        { 
         if (DnDTabbedPane.this == data.getTabbedPane()) 
         { 
          bRet = (data.getTabIndex() >= 0); 
         } 
         else if (acceptor != null) 
         { 
          bRet = acceptor. 
            isDropAcceptable(
             data.getTabbedPane(), 
             data.getTabIndex()); 
         } 
        } 
       } 
      } 
      return bRet; 
     } 
    } 

    private int getTargetTabIndex(Point point) 
    { 
     int idx = 0; 
     boolean isTopOrBottom = getTabPlacement() == JTabbedPane.Top 
                || 
           getTabPlacement() == JTabbedPane.BOTTOM; 

     if (0 != getTabCount()) 
     { 
      boolean bFound = false; 
      for (int i = 0; (i < getTabCount()) && (!bFound); i++) 
      { 
       Rectangle rect = getBoundsAt(i); 
       if (isTopOrBottom) 
       { 
        rect.setRect(rect.x - rect.width/2, rect.y, 
               rect.width, rect.height); 
       } 
       else 
       { 
        rect.setRect(rect.x, rect.y - rect.height/2, 
              rect.width, rect.height); 
       } 
       if (rect.contains(point)) 
       { 
        bFound = true; 
        idx = i; 
       } 
      } 
      if (!bFound) 
      { 
       Rectangle rect = getBoundsAt(getTabCount() - 1); 

       if (isTopOrBottom) 
       { 
        int x = rect.x + rect.width/2; 
        rect.setRect(x, rect.y, getWidth() - x, 
                rect.height); 
       } 
       else 
       { 
        int y = rect.y + rect.height/2; 
        rect.setRect(rect.x, y, rect.width, getHeight() - 
                    y); 
       } 

       idx = (rect.contains(point) ? getTabCount() : -1); 
      } 
     } 
     return idx; 
    } 


    private void convertTab(TabTransferData sourceTab, int targetIndex) 
    { 
     DnDTabbedPane source = sourceTab.getTabbedPane(); 
     int sourceIndex = sourceTab.getTabIndex(); 
     if (sourceIndex >= 0) 
     { 
      Component sourceComp = source.getComponentAt(sourceIndex); 
      String sourceTitle = source.getTitleAt(sourceIndex); 
      Component sourceTabComp = 
            source.getTabComponentAt(sourceIndex); 

      if (this != source) 
      { 
       // Not same tab plane 
       // Remove the source from the other tabbed plane 
       source.remove(sourceIndex); 
       if (targetIndex == getTabCount()) 
       { 
        // Add the source tab to the end 
        addTab(sourceTitle, sourceComp); 
        setTabComponentAt(getTabCount() - 1, sourceTabComp); 
       } 
       else 
       { 
        if (targetIndex < 0) 
        { 
         targetIndex = 0; 
        } 
        // Insert at the beginning 
        insertTab(sourceTitle, null, sourceComp, null, 
                 targetIndex); 
        setTabComponentAt(targetIndex, sourceTabComp); 
       } 
       setSelectedComponent(sourceComp); 
      } 
      else 
      { 
       // Same tab plane 
       if (targetIndex >= 0 && sourceIndex != targetIndex) 
       { 
        source.remove(sourceIndex); 
        if (targetIndex == getTabCount()) 
        { 
         addTab(sourceTitle, sourceComp); 
         setTabComponentAt(getTabCount() - 1, 
                 sourceTabComp); 
         setSelectedIndex(getTabCount() - 1); 
        } 
        else if (sourceIndex > targetIndex) 
        { 
         insertTab(sourceTitle, null, sourceComp, null, 
                 targetIndex); 
         setTabComponentAt(targetIndex, sourceTabComp); 
         setSelectedIndex(targetIndex); 
        } 
        else 
        { 
         insertTab(sourceTitle, null, sourceComp, null, 
                 targetIndex - 1); 
         setTabComponentAt(targetIndex - 1, 
                 sourceTabComp); 
         setSelectedIndex(targetIndex - 1); 
        } 
       } 
      } 
     } 
    } 

    private void initGlassPane(Component component, Point tabPt, int 
                 tabIndex) 
    { 
     getRootPane().setGlassPane(glassPaneGhost); 
     Rectangle rect = getBoundsAt(tabIndex); 
     BufferedImage image = 
        new BufferedImage(component.getWidth(), 
            component.getHeight(), 
            BufferedImage.TYPE_INT_ARGB); 
     component.paint(image.getGraphics()); 
     image = image.getSubimage(rect.x, rect.y, rect.width, 
                 rect.height); 
     glassPaneGhost.setImage(image); 
     glassPaneGhost.setPoint(buildGhostLocation(tabPt)); 
     glassPaneGhost.setVisible(true); 
    } 

    @Override 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     if (isDrawRect) 
     { 
      Graphics2D g2 = (Graphics2D) g; 
      g2.setPaint(lineColor); 
      g2.fill(lineRect); 
     } 
    } 

    public interface TabAcceptor 
    { 
     boolean isDropAcceptable(DnDTabbedPane component, int index); 
    } 

    class GhostClassPane extends JPanel 
    { 
     private Point location = new Point(0, 0); 
     private BufferedImage draggingGhost = null; 
     private AlphaComposite composite; 

     public GhostClassPane() 
     { 
      setOpaque(false); 
      composite = AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, 0.5f); 
     } 

     public void setImage(BufferedImage draggingGhost_in) 
     { 
      draggingGhost = draggingGhost_in; 
     } 

     public void setPoint(Point location_in) 
     { 
      location = location_in; 
     } 

     public void paintComponent(Graphics g) 
     { 
      if (null != draggingGhost) 
      { 
       Graphics2D g2 = (Graphics2D) g; 
       g2.setComposite(composite); 

       g2.drawImage(draggingGhost, (int) location.getX(), 
         (int) location.getY(), null); 
      } 
     } 
    } 
} 

답변

0

MouseInfo를 사용하여 화면에서 마우스를 찾습니다.

고스트 이미지가 첨부 된 상태에서 화면에서 움직이는 마우스 위치를 캡처하는 코드입니다.

class MickeyOnTheMove extends Thread 
{ 
    private Boolean   done   = false; 
    private MickeyOnTheMove mickey   = null; 
    private Point   currentPoint = null; 
    private GhostDialog  dlgGhost  = null; 
    private Point   prevPoint  = null; 
    private Point   ghostPoint  = null; 


    public MickeyOnTheMove(Image ghostImage) 
    { 
     prevPoint = new Point(MouseInfo.getPointerInfo().getLocation()); 
     currentPoint = new Point(MouseInfo.getPointerInfo().getLocation()); 
     ghostPoint = new Point((int)currentPoint.getX() + 10, 
           (int)currentPoint.getY() + 10); 

     dlgGhost = new GhostDialog(ghostImage); 
     dlgGhost.setLocation(ghostPoint); 
     dlgGhost.setVisible(true); 
     dlgGhost.setAlwaysOnTop(true); 
     dlgGhost.toFront(); 
    } 


    public void run() 
    { 
     try 
     { 
      done = false; 

      while (!done) 
      { 
       currentPoint = MouseInfo.getPointerInfo().getLocation(); 
       if ((prevPoint.getX() != currentPoint.getX()) || 
         (prevPoint.getY() != currentPoint.getY())) 
       { 
        ghostPoint = new Point((int)currentPoint.getX() + 10, 
          (int)currentPoint.getY() + 10); 
        if (null != dlgGhost) 
        { 
         dlgGhost.setLocation(ghostPoint); 
         dlgGhost.setAlwaysOnTop(true); 
         dlgGhost.toFront(); 
        } 

        prevPoint = currentPoint; 

        Thread.sleep(0); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
      done = true; 
     } 
    } 


    void setContextCursor(DragSourceContext context, int cursorType) 
    { 
     context.setCursor(new Cursor(cursorType)); 
    } 


    public void allDone() 
    { 
     done = true; 

     if (null != dlgGhost) 
     { 
      dlgGhost.setVisible(false); 
      dlgGhost.dispose(); 
      dlgGhost = null; 
     } 

     if (null != mickey) 
     { 
      mickey.interrupt(); 
     } 
    } 
} 
0

좋아, 난 그냥 그렇게 할 수있는 시간이 없기 때문에 나는 실제 코드를 제공 할 수없는,하지만 난 당신이 그것을 할 수있는 방법을 올바른 방향으로 힌트를 줄 수 :

을 당신이 옳은 것을 이해할 때 droppane에서 마우스를 가져갈 때마다 "유령 그림"이 마우스를 따라 가기를 원합니다.

당신은 가서 장식이없는 JFrame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
있는 간단한 JFrame의를 만든 다음의가 120x120 말을 프레임에 드롭 수 있도록, 크기를 조정하여 표시 할 이미지의 BufferedImage를 사용할 수 있습니다.

이제 JFrame.setBounds(x,y,w,h)을 마우스 커서에 붙여야합니다.

IIRC 당신은 Robot 클래스의 도움으로 마우스 위치를 얻음으로써 이것을 얻을 수 있습니다. 그런 다음 mouselistener를 사용하고 mouse1을 누른 채로이 "고스트 그림"과 함께 커서를 계속 따라갑니다. mousebutton 해제하기 경우, 드롭 창을 통해인지 날씨를 확인하고 당신을 위해 실제 코드를 커밋하지 위해, 내가 당신의 문제와 함께 당신을 도울 수있는 희망 미안

JFrame.setVisible(false); 

을 설정하지만, 내가 말했듯이 내 요즘은 드문 일이며 가능한 경우 커뮤니티를 돕고 싶습니다.

+0

@clockwOrk 마우스 수신기가 추가되는 대상은 무엇입니까? 전체 화면으로 드래그 할 수 있어야합니다. – Kit

+0

안녕하세요, 저는 당신이 java.awt.Robot에서 로봇을 추가하고 싶다고 생각합니다. 그것은 당신에게 화면상의 마우스의 절대 위치를 줄 수 있습니다. – clockw0rk