Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

java - Move Over a JPanel With Mouse Dragged

How do I make it so I can scroll over an image that is added to my JPanel? I have the JPanel moving with mousedragged however I don't want the entire panel to move, just scroll across the image.

Here is my code:

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Could you achieve this with Components, yes, but you'd need to translate between the component and it's parent as well as take over the responsibility of the layout manager.

Generally speaking, it would be easier to "cheat" and simply draw the image at a different offset within the component itself, for example

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

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...