Java-based panel function for selecting and dragging images [Based on the swing component], Drag and Drop swing

Source: Internet
Author: User

Java-based panel function for selecting and dragging images [Based on the swing component], Drag and Drop swing

This example describes the selectable and drag-and-drop panel functions implemented by Java. We will share this with you for your reference. The details are as follows:

I saw a post on the forum today, hoping to drag images in Swing like a map. This is the simplest implementation and provides 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 an image on the form. Usage: double-click the blank area of the form to open the image dialog box. After opening the image, you can drag the image on the form. */@ SuppressWarnings ("serial") public class DragingFrame extends JFrame {/*** constructor ** @ throws HeadlessException ??? */Public DragingFrame () throws HeadlessException {this. setdefaclocloseoperation (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 = new DragingFrame (); frame. setSize (4 00,300); frame. setLocation (300,300); frame. setResizable (false); frame. setTitle ("www.jb51.net double-click to open the image and drag it"); frame. setVisible (true) ;}}/*** can drag the image panel */@ SuppressWarnings ("serial") class ImagePanel extends JPanel {private DragStatus status = DragStatus. ready; // Drag and Drop status private Image image; // private Point imagePosition = new Point (0, 0) of the Image to be displayed ), // The current position of the image imageStartposition = new Point (0, 0 ), // The position of the image at the start of the secondary 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 () {// double-click the mouse to open the public void mouseClicked (MouseEvent e) {if (e. getClickCount () = 2) {openImage () ;}// when you press the mouse, the status is changed and the start position of the drag and drop operation is recorded. Public void mousePressed (MouseEvent e) {if (status = DragStatus. ready) {status = DragStatus. dragging; mouseStartposition = e. getPoint (); imageStartposition. setLocation (imagePosition. getLocation () ;}}// change the public void mouseReleased (MouseEvent e) {if (status = DragStatus. dragging) {status = DragStatus. ready ;}} public void mouseEntered (MouseEvent e) {} public void mouseExited (Mouse Event e) {}}); addMouseMotionListener (new MouseMotionListener () {// Java has a drag Event. In this Event, move the image position public void mouseDragged (MouseEvent e) {if (status = DragStatus. dragging) {moveImage (e. getPoint () ;}} public void mouseMoved (MouseEvent e) {}}) ;}/ *** move the image. In fact, drawing is performed in paintComponent (). Here, we only calculate the image position and call this method. ** @ Param point current mouse position */private void moveImage (Point point) {// the current position of the image is equal to the starting position of the image 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. getdefatooltoolkit (). getImage (file. getAbsolutePath (); if (image! = Null) {this. repaint () ;}}// create open file dialog box private JFileChooser createFileChooser () {JFileChooser chooser = new JFileChooser (); chooser. setDialogTitle ("select an image file... "); chooser. addChoosableFileFilter (new FileNameExtensionFilter ("common image formats", "jpg", "jpeg", "gif", "png"); chooser. showOpenDialog (this); return chooser;} @ Override protected void paintComponent (Graphics g) {super. paintComponent (g); if (I Mage! = Null) {g. drawImage (image, (int) imagePosition. getX (), (int) imagePosition. getY (), this) ;}} private enum DragStatus {Ready, Dragging }}

Running effect:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.