0

我有可以移动图像的代码。一切正常。

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

问题:

  1. 我需要将图像从一个 JPanel拖放 到另一个 JPanel。
  2. 然后我需要将拖动的图像移动到当前面板

请给我一个示例代码好吗?

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{
    / ... / 
}
4

1 回答 1

2

可能有多种方法可以实现您想要的。您可以使用玻璃窗格或 JXLayer,或者您可以停止将两个面板视为单独的元素,而更像是它们只是进入大型虚拟空间的窗口。

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

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

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

在此处输入图像描述

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);
            }
        }
    }
}
于 2013-05-08T11:40:49.780 回答