YauhenMardan

Untitled

Apr 8th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. import java.awt.Component;
  2. import java.awt.Dimension;
  3. import java.awt.EventQueue;
  4. import java.awt.Point;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.FileFilter;
  10. import java.io.IOException;
  11. import javax.imageio.ImageIO;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JLayeredPane;
  16. import javax.swing.UIManager;
  17. import javax.swing.UnsupportedLookAndFeelException;
  18.  
  19. public class TestDrag {
  20.  
  21.     public static void main(String[] args) {
  22.         new TestDrag();
  23.     }
  24.  
  25.     public TestDrag() {
  26.         EventQueue.invokeLater(new Runnable() {
  27.             @Override
  28.             public void run() {
  29.                 try {
  30.                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  31.                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  32.                     ex.printStackTrace();
  33.                 }
  34.  
  35.                 JFrame frame = new JFrame("Testing");
  36.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.                 frame.add(new TestPane());
  38.                 frame.pack();
  39.                 frame.setLocationRelativeTo(null);
  40.                 frame.setVisible(true);
  41.             }
  42.         });
  43.     }
  44.  
  45.     public class TestPane extends JLayeredPane {
  46.  
  47.         public TestPane() {
  48.             File[] images = new File("C:\\hold\\thumbnails").listFiles(new FileFilter() {
  49.                 @Override
  50.                 public boolean accept(File pathname) {
  51.                     String name = pathname.getName().toLowerCase();
  52.                     return name.endsWith(".png") ||
  53.                                     name.endsWith(".jpg") ||
  54.                                     name.endsWith(".bmp") ||
  55.                                     name.endsWith(".gif");
  56.                 }
  57.             });
  58.  
  59.             int x = 0;
  60.             int y = 0;
  61.             for (File imgFile : images) {
  62.  
  63.                 try {
  64.                     BufferedImage img = ImageIO.read(imgFile);
  65.                     JLabel label = new JLabel(new ImageIcon(img));
  66.                     label.setSize(label.getPreferredSize());
  67.                     label.setLocation(x, y);
  68.                     MouseHandler mh  = new MouseHandler();
  69.                     label.addMouseListener(mh);
  70.                     label.addMouseMotionListener(mh);
  71.                     add(label);
  72.                     x += 20;
  73.                     y += 20;
  74.                 } catch (IOException exp) {
  75.                     exp.printStackTrace();
  76.                 }
  77.  
  78.             }
  79.  
  80.         }
  81.  
  82.         @Override
  83.         public Dimension getPreferredSize() {
  84.             return new Dimension(800, 800);
  85.         }
  86.  
  87.         public class MouseHandler extends MouseAdapter {
  88.  
  89.             private Point offset;
  90.  
  91.             @Override
  92.             public void mousePressed(MouseEvent e) {
  93.                 JLabel label = (JLabel) e.getComponent();
  94.                 moveToFront(label);
  95.                 offset = e.getPoint();
  96.             }
  97.  
  98.             @Override
  99.             public void mouseDragged(MouseEvent e) {
  100.                 int x = e.getPoint().x - offset.x;
  101.                 int y = e.getPoint().y - offset.y;
  102.                 Component component = e.getComponent();
  103.                 Point location = component.getLocation();
  104.                 location.x += x;
  105.                 location.y += y;
  106.                 component.setLocation(location);
  107.             }
  108.  
  109.         }
  110.  
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment