Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在拖放过程中使重影图像移动到选项卡式窗格对话框之外?_Java_Swing_Awt - Fatal编程技术网

Java 如何在拖放过程中使重影图像移动到选项卡式窗格对话框之外?

Java 如何在拖放过程中使重影图像移动到选项卡式窗格对话框之外?,java,swing,awt,Java,Swing,Awt,我有一个JDialog和一个JPanel,其中包含一个选项卡式窗格。使用拖放,我希望重影图像能够移动到JDialog之外。光标可以在整个屏幕上移动,因此如何使重影图像跟随它 package PlannerManager; import GUIUtils; import javax.swing.*; import java.awt.*; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transfera

我有一个
JDialog
和一个
JPanel
,其中包含一个选项卡式窗格。使用拖放,我希望重影图像能够移动到
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);
            }
        }
    }
}
packageplannermanager;
进口指南;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.datatransfer.DataFlavor;
导入java.awt.datatransfer.transfer;
导入java.awt.dnd.*;
导入java.awt.event.*;
导入java.awt.geom.Rectangle2D;
导入java.awt.image.buffereImage;
导入java.net.URL;
公共类DnDTabbedPane扩展了JTabbedPane
{
私有int线宽=3;
私有字符串NAME=“TabTransferData”;
私有DataFlavor=新DataFlavor(DataFlavor)。
javaJVMLocalObjectMimeType,NAME);
私有GhostClassPane glassPaneGhost=新GhostClassPane();
私有布尔值isDrawRect=false;
private Rectangle2D lineRect=新的Rectangle2D.Double();
专用颜色lineColor=新颜色(0、100、255);
私有选项卡acceptor acceptor=null;
private ImageIcon iconImageCancel=null;
私有容器myParent=null;
受保护的detailThrappedPaneDialog选项卡detailsDlg=null;
//建造师
公共DnDTabbedPane(容器parentCont,字符串,
iconCancelWithPath,
最终计划,最终指南,
最终字符串名)
{
超级();
myParent=parentCont;
最终DRAGSOURCELISTER dsl=新DRAGSOURCELISTER()
{
@凌驾
公共无效拖缆器(拖缆器)
{
e、 getDragSourceContext().setCursor(
新光标(光标。指针);
}
@凌驾
公共无效dragExit(DRAGSOURCEE)
{
e、 getDragSourceContext().setCursor(
新光标(光标。指针);
setRect(0,0,0,0);
isDrawRect=false;
glassPaneGhost.设定点(新点(-1000,-1000));
glassPaneGhost.repaint();
}
@凌驾
公共无效dragOver(DRAGSOURCEDRAGE)
{
System.out.println(“拖动退出”);
TabTransferData=getTabTransferData(e);
如果(数据==null)
{
e、 getDragSourceContext().setCursor(
新光标(光标。指针);
}
}
@凌驾
公共无效dragDropEnd(DragSourceDropEvent e)
{
isDrawRect=false;
setRect(0,0,0,0);
glassPaneGhost.setVisible(false);
glassPaneGhost.setImage(空);
//如果删除失败,请使用JTabbedPane创建新的JDialog
如果(!e.getDropSuccess())
{
点位置=e.getLocation();
//使用JTabbedPane创建新的JDialog
tabbedDetailsDlg=新的detailsthorizedpanedialog(
我的父项、计划、指南、名称、,
dropLocation.x,dropLocation.y,false);
//将选项卡转移到新的JTabbedPane
tabbedDetailsDlg.tabbedPane.convertTab(
getTabTransferData(e),
getTargetTabIndex(液滴定位);
}
如果(1>getTabCount())
{
((JDialog)myParent.dispose();
}
}
公共作废dropActionChanged(DRAGSOURCEDRAGE事件)
{
}
};
最终DragGestureListener dgl=新DragGestureListener()
{
@凌驾
公共无效DragestureRecognited(DragestureEvent e)
{
点tabPt=e.getDragOrigin();
int dragTabIndex=指数化位置(表x、表y);
如果(0=getTabCount())
{
((JDialog)myParent.dispose();
}
其他的
{
移除(组件);
}
}
});
}
专用选项卡TransferData getTabTransferData(DropTargetDropEvent
(活动)
{
尝试
{
返回(TabTransferData)
event.getTransferable().getTransferData(FLAVOR);
}
捕获(例外e)
{
e、 printStackTrace();
}
返回null;
}
专用选项卡TransferData getTabTransferData(DropTargetDrageEvent
(活动)
{
尝试
{
返回(TabTransferData)
event.getTransferable().getTransferData(FLAVOR);
}
捕获(例外e)
{
e、 printStackTrace();
}
返回null;
}
专用选项卡TransferData getTabTransferData(DragSourcePropertEvent
(活动)
{
尝试
{
return(TabTransferData)事件。getDragSourceContext()
.getTransferable().getTransferData(味道);
}
捕获(例外e)
{
e、 printStackTrace();
}
返回null;
}
专用选项卡TransferData getTabTransferData(DragSourceDragEvent
(活动)
{
尝试
{
返回(TabTransferData)
JFrame.setVisible(false);
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();
        }
    }
}