1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
package draganddrop.elb;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import draganddrop.librairie.GhostDropListener;
import draganddrop.librairie.GhostDropManager;
import draganddrop.librairie.GhostGlassPane;
import draganddrop.librairie.GhostMotionAdapter;
import draganddrop.librairie.GhostPictureAdapter;
public class DragnGhostElb extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel panelPrincipal;
private JPanel panelSources;
private JPanel panelCibles;
private GhostGlassPane glassPane;
private GhostPictureAdapter pictureAdapter;
private GhostDropListener listener1;
private GhostDropListener listener2;
// ================================================
// CONSTRUCTEUR
// ================================================
public DragnGhostElb() {
this.setSize(300, 200);
glassPane = new GhostGlassPane();
setGlassPane(glassPane);
afficher();
}
// ================================================
// AFFICHAGE
// ================================================
private void afficher() {
panelPrincipal = new JPanel(new BorderLayout());
setContentPane(panelPrincipal);
afficherCibles();
afficherSources();
}
private void afficherSources() {
panelSources = new JPanel();
BoxLayout layout = new BoxLayout(panelSources, BoxLayout.Y_AXIS);
panelSources.setLayout(layout);
JLabel source1 = new JLabel("source1");
JLabel source2 = new JLabel("source2");
JLabel source3 = new JLabel("source3");
BufferedImage img1 = null;
BufferedImage img2 = null;
BufferedImage img3 = null;
try {
img1 = ImageIO.read(new File("new_sale.png"));
img2 = ImageIO.read(new File("view_sale.png"));
img3 = ImageIO.read(new File("quit.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
source1.setIcon(new ImageIcon(img1));
source2.setIcon(new ImageIcon(img2));
source3.setIcon(new ImageIcon(img3));
source1.setBorder(new LineBorder(Color.BLACK));
source2.setBorder(new LineBorder(Color.BLACK));
source3.setBorder(new LineBorder(Color.BLACK));
panelSources.add(source1);
panelSources.add(source2);
panelSources.add(source3);
panelPrincipal.add(panelSources, BorderLayout.WEST);
// GESTION DU DRAG DE LA SOURCE
// SOURCE 1
source1.addMouseListener(pictureAdapter = new GhostPictureAdapter(glassPane, "source1", img1));
pictureAdapter.addGhostDropListener(listener1);
pictureAdapter.addGhostDropListener(listener2);
source1.addMouseMotionListener(new GhostMotionAdapter(glassPane));
// SOURCE 2
source2.addMouseListener(pictureAdapter = new GhostPictureAdapter(glassPane, "source2", img2));
pictureAdapter.addGhostDropListener(listener1);
pictureAdapter.addGhostDropListener(listener2);
source2.addMouseMotionListener(new GhostMotionAdapter(glassPane));
// SOURCE 3
source3.addMouseListener(pictureAdapter = new GhostPictureAdapter(glassPane, "source3", img3));
pictureAdapter.addGhostDropListener(listener1);
pictureAdapter.addGhostDropListener(listener2);
source3.addMouseMotionListener(new GhostMotionAdapter(glassPane));
}
private void afficherCibles() {
panelCibles = new JPanel();
BoxLayout layout = new BoxLayout(panelCibles, BoxLayout.Y_AXIS);
panelCibles.setLayout(layout);
JLabel cible1 = new JLabel("cible 1");
JLabel cible2 = new JLabel("cible 2");
cible1.setName(cible1.getText());
cible2.setName(cible2.getText());
cible1.setMinimumSize(new Dimension(50, 50));
cible2.setMinimumSize(new Dimension(50, 50));
cible1.setPreferredSize(new Dimension(50, 50));
cible2.setPreferredSize(new Dimension(50, 50));
cible1.setMaximumSize(new Dimension(50, 50));
cible2.setMaximumSize(new Dimension(50, 50));
cible1.setBorder(new LineBorder(Color.BLACK));
cible2.setBorder(new LineBorder(Color.BLACK));
panelCibles.add(cible1);
panelCibles.add(cible2);
panelPrincipal.add(panelCibles, BorderLayout.EAST);
// GESTION DU DROP DE LA CIBLE
listener1 = new GhostDropManager(cible1);
listener2 = new GhostDropManager(cible2);
}
// ================================================
// MAIN
// ================================================
public static void main(String[] args)
{
DragnGhostElb demo = new DragnGhostElb();
demo.setVisible(true);
}
} |
Partager