拖动鼠标移动JPanel

时间:2013-11-15 21:40:56

标签: java

如何制作,以便滚动添加到JPanel的图像?我让JPanel移动了mousedragged但是我不希望整个面板移动,只是滚动图像。

这是我的代码:

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class WorldMap extends JFrame implements MouseMotionListener {

JPanel panel = new JPanel();
JLabel map = new JLabel();
Image image = Toolkit.getDefaultToolkit().getImage(
        getClass().getResource("/map/map.jpg"));
ImageIcon icon;

public WorldMap() {
    addMouseMotionListener(this);
    icon = new ImageIcon(image);
    map.setIcon(icon);
    panel.add(map);
    add(panel);
    setTitle("World Map");
    setSize(800, 800);
    setResizable(false);
    setVisible(true);
}

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

public void mouseDragged(MouseEvent e) {
    e.translatePoint(e.getComponent().getLocation().x, e.getComponent()
            .getLocation().y);
    panel.setLocation(e.getX(), e.getY());
    repaint();
}

public void mouseMoved(MouseEvent e) {
}

}

1 个答案:

答案 0 :(得分:1)

你可以用Component来实现这一点,是的,但你需要在组件和它的父组件之间进行转换,以及接管布局管理器的职责。

一般来说,更容易“欺骗”并简单地在组件本身的不同偏移处绘制图像,例如

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
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.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DraggableImage {

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

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

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

    public class ImagePane extends JPanel {

        private BufferedImage img;

        private Point offset = new Point(0, 0);

        public ImagePane() {
            try {
                img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/MT015.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            MouseAdapter ma = new MouseAdapter() {

                private Point startPoint;

                @Override
                public void mousePressed(MouseEvent e) {
                    startPoint = e.getPoint();
                    startPoint.x -= offset.x;
                    startPoint.y -= offset.y;
                }

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

                @Override
                public void mouseDragged(MouseEvent e) {
                    Point p = e.getPoint();
                    int x = p.x - startPoint.x;
                    int y = p.y - startPoint.x;
                    offset = new Point(x, y);
                    repaint();
                }

            };

            addMouseListener(ma);
            addMouseMotionListener(ma);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                if (offset == null) {
                    offset = new Point(0, 0);
                }
                g2d.drawImage(img, offset.x, offset.y, this);
                g2d.dispose();
            }
        }

    }

}