0

I have no clue to why is this flickering occurs. Also I noticed while dragging a panel the mouse pointer goes outside the panel.

// Java Program to demonstrate a simple  JLayeredPane
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.ImageIcon;

public class Z_LayeredPaneExample1 {
static JPanel[] panel;  
static Point initialClickPoint ;
static int N; 
static JFrame frame ;

public static void main(String[] args) {
    // Create a JFrame (the main window for the application).
    frame = new JFrame("JLayeredPane Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);

    // Create a JLayeredPane to manage the layering of components.
    JLayeredPane layeredPane = new JLayeredPane();
    frame.add(layeredPane); // Add the JLayeredPane to the JFrame.
    panel = new JPanel[3];

    // Create three colored panels to add to the layered pane.
    panel[0] = createColoredPanel(Color.RED, 100, 100, 200, 200);
    panel[1] = createColoredPanel(Color.GREEN, 150, 150, 200, 200);
    panel[2] = createColoredPanel(Color.BLUE, 200, 200, 200, 200);

    for ( int i = 0; i < 3; i++) {
        N=i ;

        // Add mouse listeners to enable drag and drop
        panel[N].addMouseListener(new MouseAdapter() {

            public void mouseReleased(MouseEvent e) {

                JPanel panel = (JPanel) e.getSource();
                int x = (int) (panel.getLocation().getX() + e.getX() -    
                        initialClickPoint.getX());
                int y = (int) (panel.getLocation().getY() + e.getY() -  
                          initialClickPoint.getY());
                panel.setLocation(x, y);

            }  
            public void mousePressed(MouseEvent e) {
                initialClickPoint = e.getPoint();
                JPanel panel = (JPanel) e.getSource();
                //panel.bringToFront();
            }
        });

         panel[N].addMouseMotionListener(new MouseMotionAdapter() {
            //@Override
            public void mouseDragged(MouseEvent e) {
                JPanel panel = (JPanel) e.getSource();
                int x = (int) (panel.getLocation().getX() + e.getX() - 
                initialClickPoint.getX());
                int y = (int) (panel.getLocation().getY() + e.getY() - 
                        initialClickPoint.getY());
                panel.setLocation(x, y);
        initialClickPoint = (panel.getLocation());
            }
        });
    }

    // Add the panels to the layered pane with different layer values.
    // The layers determine the stacking order of the panels.
    layeredPane.add(panel[0], JLayeredPane.DEFAULT_LAYER);
    layeredPane.add(panel[1], JLayeredPane.PALETTE_LAYER);
    layeredPane.add(panel[2], JLayeredPane.MODAL_LAYER);

    frame.setVisible(true); // Make the JFrame visible.
}

private static JPanel createColoredPanel(Color color, int x, int y, int width, int  
                                         height) {
    // Create a colored JPanel with specified color and position.
    JPanel panel = new JPanel();
    panel.setBackground(color);
    panel.setBounds(x, y, width, height);
    return panel;
}
}

I am lost as to what could be the fix. I am updating dragged panel location when dragged and when mouse released.

1
  • Hi James Z, thanks for editing my question. Please also help me in finding a fix for flickering images. Thanks Commented Jun 29 at 15:56

0

Browse other questions tagged or ask your own question.