从一个面板移动图像到另一个 [英] move image from one panel to another

查看:295
本文介绍了从一个面板移动图像到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code,我可以移动图像。
一切正常。

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

问题:


  1. 我需要拖动从一个JPanel的下降图片到另一个JPanel的。

  2. 然后,我需要的移动拖图像当前面板

你能给我一个例子code,好吗?

 类ImagePanel继承JPanel {
    INT X,Y;
        BufferedImage的图像;        ImagePanel(){
                的setBackground(Color.white);
                的setSize(450,400);
                addMouseMotionListener(新MouseMotionHandler());                图片IMG =中的getToolkit()的getImage(C:\\\\ 2.png);                媒体跟踪MT =新媒体跟踪(本);
                mt.addImage(IMG,1);
                尝试{
                        mt.waitForAll();
                }赶上(例外五){
                        的System.out.println(没有找到图片。);
                }
                图像=新的BufferedImage(img.getWidth(本),img.getHeight(本),BufferedImage.TYPE_INT_ARGB);
                G2的Graphics2D = image.createGraphics();
                g2.drawImage(IMG,0,0,这一点);
        }        公共无效的paintComponent(图形G){
                super.paintComponent方法(G);
                Graphics2D的G2D =(Graphics2D的)克;
                g2D.drawImage(图像,X,Y,这一点);
        }        类MouseMotionHandler扩展MouseMotionAdapter {
                公共无效的mouseDragged(的MouseEvent E){
                        X = e.getX();
                        Y = e.getY();
                        重绘();
                }                公共无效的mouseMoved(的MouseEvent E){                }
        }
}

我需要做的code,这个下面的设计。我需要一些布局(我不需要用点像素做到了这一点),增加图像。或如何与一些布局添加图片?例如网格包布局。我不需要点(X,Y)。因为我需要添加其他组件了。

 公共类DragAndDrop {    私人的JFrame框架;
        / * ..这里的另一个组件。* /    私人的JPanel leftPanel; //这里是我的形象
    公众的JPanel rightContentPanel; // dragable图像的目的地
    公共静态无效的主要(字串[] args){
        DragAndDrop窗口=新DragAndDrop();    }
    公共DragAndDrop(){
        初始化();
    }
    私人无效初始化(){
        帧=新的JFrame();
        frame.getContentPane()的setLayout(新的BorderLayout(0,0))。        leftPanel =新leftPanel();
        / *组件添加到左侧面板* /
        rightContentPanel =新rightPanel();
        / *添加组件右侧面板* /
        frame.getContentPane()加(rightContentPanel,BorderLayout.CENTER)。
                frame.getContentPane()加(leftPanel,BorderLayout.WEST)。            frame.setVisible(真);
            frame.setResizable(假);
    }
}类leftPanel继承JPanel {
    / ...... /
}类rightPanel继承JPanel {
    / ...... /
}


解决方案

有可能是任何数量的方式来实现你想要什么。您可以使用玻璃板或JXLayer或者你可以停止治疗两个面板为单独的元素,而更像他们只是窗户成一个大的虚拟空间。

本实例主要治疗父组件的虚拟空间到其中的两个图像窗格是窗口。

他们都共享相同的图像和图像位置的详细信息。它们,个别的图像位置(在虚拟坐标)转换为本地坐标和绘制尽可能多的图像作为将出现在他们...

鼠标控制由父保持。这极大地简化了工艺,因为它可以同时通知同时面板

 进口java.awt.BorderLayout中;
进口java.awt.Color中;
进口java.awt.Dimension中;
进口java.awt.EventQueue中;
进口java.awt.Graphics;
进口java.awt.GridLayout中;
进口java.awt.Image中;
进口java.awt.Point中;
进口java.awt.Rectangle中;
进口java.awt.event.MouseAdapter;
进口java.awt.event.MouseEvent中;
进口java.awt.image.BufferedImage中;
进口的java.io.File;
进口java.io.IOException异常;
进口javax.imageio.ImageIO中;
进口javax.swing.JFrame中;
进口javax.swing.JPanel中;
进口javax.swing.SwingUtilities中;
进口javax.swing.UIManager中;
进口javax.swing.UnsupportedLookAndFeelException;
进口javax.swing.border.EmptyBorder中;
进口javax.swing.border.LineBorder中;公共类CrossImage {    公共静态无效的主要(字串[] args){
        新CrossImage();
    }    公共CrossImage(){
        EventQueue.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                尝试{
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }赶上(ClassNotFoundException的| InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException前){
                }                JFrame的帧=新的JFrame(测试);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(新的BorderLayout());
                frame.add(新TestPane());
                frame.pack();
                frame.setLocationRelativeTo(NULL);
                frame.setVisible(真);
            }
        });
    }    公共类TestPane继承JPanel {        私人的BufferedImage IMG;
        私人ImagePane离开;
        私人ImagePane权;
        私人点imagePoint;        公共TestPane(){
            setBorder(新EmptyBorder(10,10,10,10));
            的setLayout(新的GridLayout(0,2,10,10));
            左=新ImagePane();
            右=新ImagePane();
            imagePoint =新点(10,10);
            left.setImageLocation(imagePoint);
            right.setImageLocation(imagePoint);
            尝试{
                IMG = ImageIO.read(新文件(Background.jpg));
                left.setImage(IMG);
                right.setImage(IMG);
            }赶上(IOException异常前){
                ex.printStackTrace();
            }
            加(左);
            加(右);            MouseAdapter mouseHandler =新MouseAdapter(){
                私人点三角洲;                @覆盖
                公共无效鼠标pressed(的MouseEvent E){
                    原点= e.getPoint();
                    矩形范围=新的Rectangle(imagePoint,新的尺寸(img.getWidth(),img.getHeight()));
                    如果(bounds.contains(来源)){
                        三角洲=新的点(origin.x - imagePoint.x,origin.y - imagePoint.y);
                    }
                }                @覆盖
                公共无效的mouseDragged(的MouseEvent E){
                    如果(△!= NULL){
                        imagePoint = e.getPoint();
                        imagePoint.translate(-delta.x,-delta.y);
                        left.setImageLocation(imagePoint);
                        right.setImageLocation(imagePoint);
                    }
                }                @覆盖
                公共无效的mouseReleased(的MouseEvent E){
                    三角洲= NULL;
                }
            };            addMouseListener将(mouseHandler);
            addMouseMotionListener(mouseHandler);
        }
    }    公共类ImagePane继承JPanel {        私人形象画像;
        私人点imageLocation;        公共ImagePane(){
            setBorder(新LineBorder(Color.DARK_GRAY));
        }        @覆盖
        公共尺寸的get preferredSize(){
            返回形象== NULL? super.get preferredSize():新的Dimension(image.getWidth(本),image.getHeight(本));
        }        公共无效setImage(形象画像){
            this.image =图像;
            重绘();
        }        公共无效setImageLocation(点imageLocation){
            this.imageLocation = imageLocation;
            重绘();
        }        @覆盖
        保护无效paintComponent(图形G){
            super.paintComponent方法(G);
            如果(图像=零和放大器;!&安培;!imageLocation = NULL){
                点P = SwingUtilities.convertPoint(的getParent(),imageLocation,这一点);
                g.drawImage(图像,p.x,p.y,这一点);
            }
        }
    }
}

I have Code where I can move image. Everything works well.

here I have only one ImagePanel (children of JPanel) on the frame.

Questions:

  1. I need to drag and drop image from one JPanel to another JPanel.
  2. Then I need to move dragged image to current panel.

Can you give me an example code, please?

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) {

                }
        }
}

I need to do that code, with this following design. I need to add image with some layout (I don't need to done this with Point pixels). or how to add image with some layout? for example Grid bag layout. I don't need Points (x,y). because I need to add another components too.

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

解决方案

There's probably any number of ways to achieve what you want. You could use the glass pane or JXLayer or you could stop treating the two panels as separate elements and more like they were just windows into a large virtual space.

This example basically treats the parent component as the "virtual space" into which the two image panes are windows.

They both share the same image and image location details. They, individual, convert the image location (which is in virtual coordinates) to local coordinates and draw as much of the image as would appear on them...

Mouse control is maintained by the parent. This greatly simplifies the process, as it can notify both the panels simultaneously

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);
            }
        }
    }
}

这篇关于从一个面板移动图像到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆