java code to make two Jlabels with icons inside draggable to any position of full screen…
- User
java code to make two Jlabels with icons inside draggable to any position of full screen JFrame screen , kepp the jlabel pos in sync with new dragged mouse pos
- Assistant
Here's an example of how to make two
JLabels draggable in aJFramewith their position in sync with the mouse position:import java.awt.Color; import java.awt.Dimension; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; public class DraggableLabels { public static void main(String[] args) { JFrame frame = new JFrame("Draggable Labels Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 600); frame.setLocationRelativeTo(null); JPanel panel = new JPanel(); panel.setLayout(null); panel.setBackground(Color.WHITE); JLabel label1 = new JLabel(new ImageIcon("path/to/image1.png")); label1.setHorizontalAlignment(SwingConstants.CENTER); label1.setVerticalAlignment(SwingConstants.CENTER); label1.setSize(label1.getPreferredSize()); label1.setLocation(new Point(100, 100)); JLabel label2 = new JLabel(new ImageIcon("path/to/image2.png")); label2.setHorizontalAlignment(SwingConstants.CENTER); label2.setVerticalAlignment(SwingConstants.CENTER); label2.setSize(label2.getPreferredSize()); label2.setLocation(new Point(500, 200)); panel.add(label1); panel.add(label2); panel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { label1.getParent().setComponentZOrder(label1, 0); // Bring label to front label2.getParent().setComponentZOrder(label2, 0); // Bring label to front } }); panel.addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent e) { label1.setLocation(e.getX() - label1.getWidth() / 2, e.getY() - label1.getHeight() / 2); label2.setLocation(e.getX() + label1.getWidth() / 2, e.getY() + label1.getHeight() / 2); } }); frame.add(panel); frame.setVisible(true); } }This code creates a
JFramewith twoJLabels,label1andlabel2, each with an image icon set as its content. TheJLabels are added to aJPanelwith a null layout and a white background. TheJLabels are initially located at two different positions in the panel.To make the
JLabels draggable, we add aMouseListenerand aMouseMotionListenerto the panel. In themousePressedmethod of theMouseListener, we bring theJLabelthat was clicked to the front by setting its component z-order to 0. In themouseDraggedmethod of theMouseMotionListener, we set the location of bothJLabels to the current mouse position, adjusted by the size oflabel1, so thatlabel2followslabel1.With this code, you should be able to drag the
JLabels to any position of the full screenJFramescreen and their positions will stay in sync with the new dragged mouse position. - User
the labels are not draggable indepentently
- Assistant
If you want to make the labels draggable independently, you can modify the code to keep track of which label is being dragged and update its position accordingly. Here's an example:
import java.awt.Color; import java.awt.Dimension; import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; public class DraggableLabels { private static JLabel selectedLabel = null; // track which label is being dragged public static void main(String[] args) { JFrame frame = new JFrame("Draggable Labels Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 600); frame.setLocationRelativeTo(null); JPanel panel = new JPanel(); panel.setLayout(null); panel.setBackground(Color.WHITE); JLabel label1 = new JLabel(new ImageIcon("path/to/image1.png")); label1.setHorizontalAlignment(SwingConstants.CENTER); label1.setVerticalAlignment(SwingConstants.CENTER); label1.setSize(label1.getPreferredSize()); label1.setLocation(new Point(100, 100)); JLabel label2 = new JLabel(new ImageIcon("path/to/image2.png")); label2.setHorizontalAlignment(SwingConstants.CENTER); label2.setVerticalAlignment(SwingConstants.CENTER); label2.setSize(label2.getPreferredSize()); label2.setLocation(new Point(500, 200)); panel.add(label1); panel.add(label2); panel.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.getSource() instanceof JLabel) { JLabel label = (JLabel) e.getSource(); label.getParent().setComponentZOrder(label, 0); // Bring label to front selectedLabel = label; // Set selected label } } public void mouseReleased(MouseEvent e) { selectedLabel = null; // Clear selected label } }); panel.addMouseMotionListener(new MouseAdapter() { public void mouseDragged(MouseEvent e) { if (selectedLabel != null) { // If a label is selected selectedLabel.setLocation(e.getX() - selectedLabel.getWidth() / 2, e.getY() - selectedLabel.getHeight() / 2); } } }); frame.add(panel); frame.setVisible(true); } }In this version of the code, we add a
selectedLabelvariable to keep track of which label is being dragged. When themousePressedmethod is called, we check if the event source is aJLabeland set it as the selected label. When themouseReleasedmethod is called, we clear the selected label.In the
mouseDraggedmethod, we check if there is a selected label and update its position accordingly. This allows the labels to be dragged independently of each other.