i have tried to make a Drag and Drop for type of Puzzle. Its my school project and im the only one who is stuck with this trouble ...
it doesnt exchange me the Location of the Labels .. with MouseListener ...
package proveproject; import javax.swing.*; import java.awt.*;; import java.io.*; import java.awt.event.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.*; public class DPuzzle implements ActionListener{ JFrame dpFrame; JLabel []lbl; JLabel []eastLbl; JButton solve; JComponent a,b; int X1,Y1,X2,Y2; DPuzzle(){ dpFrame = new JFrame("PUZZLE"); dpFrame.setDefaultCloseOperation(dpFrame.EXIT_ON_CLOSE); dpFrame.setBounds(50,50,1200,500); dpFrame.setLayout(new BorderLayout()); JPanel centerPane = new JPanel(); centerPane.setLayout(new GridLayout(3,8,3,3)); JPanel southPane = new JPanel(); //Aggiungo le immagini ImageIcon []img; img = new ImageIcon[9]; img[0] = new ImageIcon("1.jpg"); img[1] = new ImageIcon("2.jpg"); img[2] = new ImageIcon("3.jpg"); img[3] = new ImageIcon("4.jpg"); img[4] = new ImageIcon("5.jpg"); img[5] = new ImageIcon("6.jpg"); img[6] = new ImageIcon("7.jpg"); img[7] = new ImageIcon("8.jpg"); img[8] = new ImageIcon("9.jpg"); //metto le immagini nelle label lbl = new JLabel[9]; for (int i = 0; i < 9; i++) { lbl[i]= new JLabel(""); lbl[i].addMouseMotionListener(new DragMouseAdapter()); lbl[i].addMouseListener(new DragMouseAdapter()); } lbl[0].setIcon(img[7]); lbl[1].setIcon(img[4]); lbl[2].setIcon(img[1]); lbl[3].setIcon(img[3]); lbl[4].setIcon(img[6]); lbl[5].setIcon(img[2]); lbl[6].setIcon(img[8]); lbl[7].setIcon(img[5]); lbl[8].setIcon(img[0]); //Finisco di mettere le label nel WestPane //Inizio mettere le immagini nelle Label East ImageIcon whiteImg = new ImageIcon("whiteImg.jpg"); eastLbl = new JLabel[9]; for (int i = 0; i < 9; i++) { eastLbl[i] = new JLabel(""); eastLbl[i].setIcon(whiteImg); eastLbl[i].addMouseMotionListener(new DragMouseAdapter()); eastLbl[i].addMouseListener(new DragMouseAdapter()); } //Fine Pannello Ovest //Creare pannello sud solve = new JButton("Fatto"); solve.addActionListener(this); southPane.add(solve); centerPane.add(lbl[0]); centerPane.add(lbl[1]); centerPane.add(lbl[2]); centerPane.add(new JLabel("")); centerPane.add(new JLabel("")); centerPane.add(eastLbl[0]); centerPane.add(eastLbl[1]); centerPane.add(eastLbl[2]); centerPane.add(lbl[3]); centerPane.add(lbl[4]); centerPane.add(lbl[5]); centerPane.add(new JLabel("")); centerPane.add(new JLabel("")); centerPane.add(eastLbl[3]); centerPane.add(eastLbl[4]); centerPane.add(eastLbl[5]); centerPane.add(lbl[6]); centerPane.add(lbl[7]); centerPane.add(lbl[8]); centerPane.add(new JLabel("")); centerPane.add(new JLabel("")); centerPane.add(eastLbl[6]); centerPane.add(eastLbl[7]); centerPane.add(eastLbl[8]); dpFrame.add(centerPane,BorderLayout.CENTER); dpFrame.add(southPane,BorderLayout.SOUTH); dpFrame.setVisible(true); } @Override public void actionPerformed(ActionEvent ae) { } //MouseListener //MouseMotionListener public class DragMouseAdapter implements MouseMotionListener,MouseListener{ @Override public void mouseDragged(MouseEvent me) { a =(JComponent)me.getSource(); X1 = a.getX(); Y1 = a.getY(); } @Override public void mouseMoved(MouseEvent me) { } @Override public void mouseClicked(MouseEvent me) { a.setLocation(X2, Y2); b.setLocation(X1, Y1); } @Override public void mousePressed(MouseEvent me) { } @Override public void mouseReleased(MouseEvent me) { b = (JComponent)me.getSource(); X2 = b.getX(); Y2 = b.getY(); } @Override public void mouseEntered(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); } @Override public void mouseExited(MouseEvent me) { throw new UnsupportedOperationException("Not supported yet."); } } public static void main(String[] args) { new DPuzzle(); } }
Is there anybody who can help me doing the exchange of the JLabels Location???