将图像从一个面板移动到另一个面板

时间:2013-05-08 10:16:09

标签: java image swing drag-and-drop awt

我有代码,我可以移动图像。 一切都运作良好。

这里我在框架上只有一个ImagePanel(JPanel的子代)。

问题:

  1. 我需要图片从一个JPanel拖放到另一个JPanel。
  2. 然后我需要将拖动的图片移动到当前面板
  3. 你能给我一个示例代码吗?

    class ImagePanel extends JPanel {
        int x, y;
            BufferedImage image;
    
            ImagePanel() {
                    setBackground(Color.white);
                    setSize(450, 400);
                    addMouseMotionListener(new MouseMotionHandler());
    
                    Image img = getToolkit().getImage("C:\\2.png");
    
                    MediaTracker mt = new MediaTracker(this);
                    mt.addImage(img, 1);
                    try {
                            mt.waitForAll();
                    } catch (Exception e) {
                            System.out.println("Image not found.");
                    }
                    image = new BufferedImage(img.getWidth(this), img.getHeight(this),BufferedImage.TYPE_INT_ARGB);
                    Graphics2D g2 = image.createGraphics();
                    g2.drawImage(img, 0, 0, this);
            }
    
            public void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    Graphics2D g2D = (Graphics2D) g;
                    g2D.drawImage(image, x, y, this);
            }
    
            class MouseMotionHandler extends MouseMotionAdapter {
                    public void mouseDragged(MouseEvent e) {
                            x = e.getX();
                            y = e.getY();
                            repaint();
                    }
    
                    public void mouseMoved(MouseEvent e) {
    
                    }
            }
    }
    

    我需要使用以下设计来执行该代码。我需要添加一些布局的图像(我不需要使用点像素完成此操作)。或如何添加一些布局的图像?例如网格包布局。我不需要积分(x,y)。因为我还需要添加其他组件。

    public class DragAndDrop {
    
        private JFrame frame;        
            /* .. another component here .. */
    
        private JPanel leftPanel;  // here is my image
        public JPanel rightContentPanel; // destination of dragable image
    
    
        public static void main(String[] args) {              
            DragAndDrop window = new DragAndDrop();
    
        }
    
    
        public DragAndDrop() {
            initialize();
        }
    
    
        private void initialize() {
            frame = new JFrame();
            frame.getContentPane().setLayout(new BorderLayout(0, 0));
    
            leftPanel = new leftPanel();
            /* add components to left panel */
    
    
            rightContentPanel =  new rightPanel();
            /* add component to right panel */
    
    
    
    
            frame.getContentPane().add(rightContentPanel, BorderLayout.CENTER);
                    frame.getContentPane().add(leftPanel, BorderLayout.WEST);
    
                frame.setVisible(true);
                frame.setResizable(false);
        }
    } 
    
    class leftPanel extends JPanel {
        / ... /
    }
    
    class rightPanel extends JPanel{
        / ... / 
    }
    

1 个答案:

答案 0 :(得分:2)

可能有很多方法可以达到你想要的效果。您可以使用玻璃窗格或JXLayer,或者您可以停止将两个面板视为单独的元素,更像是将它们放入大型虚拟空间中。

此示例基本上将父组件视为两个图像窗格为窗口的“虚拟空间”。

它们共享相同的图像和图像位置详细信息。他们个人将图像位置(在虚拟坐标中)转换为局部坐标,并尽可能多地绘制图像上的图像...

鼠标控制由父级维护。这大大简化了流程,因为它可以同时通知两个面板

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class CrossImage {

    public static void main(String[] args) {
        new CrossImage();
    }

    public CrossImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;
        private ImagePane left;
        private ImagePane right;
        private Point imagePoint;

        public TestPane() {
            setBorder(new EmptyBorder(10, 10, 10, 10));
            setLayout(new GridLayout(0, 2, 10, 10));
            left = new ImagePane();
            right = new ImagePane();
            imagePoint = new Point(10, 10);
            left.setImageLocation(imagePoint);
            right.setImageLocation(imagePoint);
            try {
                img = ImageIO.read(new File("Background.jpg"));
                left.setImage(img);
                right.setImage(img);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            add(left);
            add(right);

            MouseAdapter mouseHandler = new MouseAdapter() {
                private Point delta;

                @Override
                public void mousePressed(MouseEvent e) {
                    Point origin = e.getPoint();
                    Rectangle bounds = new Rectangle(imagePoint, new Dimension(img.getWidth(), img.getHeight()));
                    if (bounds.contains(origin)) {
                        delta = new Point(origin.x - imagePoint.x, origin.y - imagePoint.y);
                    }
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    if (delta != null) {
                        imagePoint = e.getPoint();
                        imagePoint.translate(-delta.x, -delta.y);
                        left.setImageLocation(imagePoint);
                        right.setImageLocation(imagePoint);
                    }
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    delta = null;
                }
            };

            addMouseListener(mouseHandler);
            addMouseMotionListener(mouseHandler);
        }
    }

    public class ImagePane extends JPanel {

        private Image image;
        private Point imageLocation;

        public ImagePane() {
            setBorder(new LineBorder(Color.DARK_GRAY));
        }

        @Override
        public Dimension getPreferredSize() {
            return image == null ? super.getPreferredSize() : new Dimension(image.getWidth(this), image.getHeight(this));
        }

        public void setImage(Image image) {
            this.image = image;
            repaint();
        }

        public void setImageLocation(Point imageLocation) {
            this.imageLocation = imageLocation;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (image != null && imageLocation != null) {
                Point p = SwingUtilities.convertPoint(getParent(), imageLocation, this);
                g.drawImage(image, p.x, p.y, this);
            }
        }
    }
}