This article describes the panel functions that can be selected and dragged and dropped pictures implemented by Java. Share it for your reference, as follows:
I saw a post on the forum today and hope to drag and drop pictures like dragging maps in Swing. Here is the simplest implementation, providing a basic idea.
import javax.swing.*;import javax.swing.filechooser.FileNameExtensionFilter;import java.awt.*;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.io.File;/** * Drag and drop the image on the form. How to use: Double-click the blank space in the form to open the picture dialog box. After opening the picture, you can drag and drop the picture on the form. */@SuppressWarnings("serial")public class DragingFrame extends JFrame { /** * Constructor* * @throws HeadlessException ? ? ? */ public DragingFrame() throws HeadlessException { this.setDefaultCloseOperation(EXIT_ON_CLOSE); getContentPane().setLayout(new BorderLayout()); getContentPane().add(new ImagePanel(), BorderLayout.CENTER); } // Program entry public static void main(String[] args) throws Exception { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); DragingFrame frame = new DragingFrame(); frame.setSize(400, 300); frame.setLocation(300, 300); frame.setResizable(false); frame.setTitle("www.VeVB.COM Double-click to open the picture, and then drag and drop"); frame.setVisible(true); }}/** * Panel that can drag and drop the picture*/@SuppressWarnings("serial")class ImagePanel extends JPanel { private DragStatus status = DragStatus.Ready; // Drag status private Image image; // Image to be displayed private Point imagePosition = new Point(0, 0), // The current position of the image imageStartposition = new Point(0, 0), // The position of the image at the start of each drag (that is, the position after the last drag) mouseStartposition; // The position of the mouse at the start of each drag ImagePanel() { addMouseListener(new MouseListener() { // Open the image when double-clicking the mouse public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { openImage(); } } // When the mouse is pressed, the status is changed and the drag start position is recorded. public void mousePressed(MouseEvent e) { if (status == DragStatus.Ready) { status = DragStatus.Dragging; mouseStartposition = e.getPoint(); imageStartposition.setLocation(imagePosition.getLocation()); } } // When the mouse is released, the status is changed. public void mouseReleased(MouseEvent e) { if (status == DragStatus.Dragging) { status = DragStatus.Ready; } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }); addMouseMotionListener(new MouseMotionListener() { // Java has a drag and drop event, move the image position in this event public void mouseDragged(MouseEvent e) { if (status == DragStatus.Dragging) { moveImage(e.getPoint()); } } public void mouseMoved(MouseEvent e) { } }); } /** * Move the picture. In fact, drawing work is done in paintComponent(), where it just calculates the image position and then calls the method. * * @param point Current mouse position*/ private void moveImage(Point point) { // The current position of the picture is equal to the starting position of the picture plus the offset of the mouse position. imagePosition.setLocation( imageStartposition.getX() + (point.getX() - mouseStartposition.getX()), imageStartposition.getY() + (point.getY() - mouseStartposition.getY()) ); repaint(); } // Open the image private void openImage() { System.out.println("Opening image..."); File file = createFileChooser().getSelectedFile(); if (file != null) { image = Toolkit.getDefaultToolkit().getImage(file.getAbsolutePath()); if (image != null) { this.repaint(); } } } // Create an open file dialog box private JFileChooser createFileChooser() { JFileChooser chooser = new JFileChooser(); chooser.setDialogTitle("Please select image file..."); chooser.addChoosableFilter(new FileNameExtensionFilter("Common image format", "jpg", "jpeg", "gif", "png")); chooser.showOpenDialog(this); return chooser; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (image != null) { g.drawImage(image, (int) imagePosition.getX(), (int) imagePosition.getY(), this); } } private enum DragStatus { Ready, Dragging }}Running effect:
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.