0

I have created a desktop pane and internal frame for chess application. The outer window have Menu such as new game. It works pretty great. But the problem which I am facing is with mouse events in the internal frame. As my Internal frame is my chessboard with chess pieces. The mouse events were working when I was not using desktop pane with an internal frame just a simple frame approach for the game. When I switched my code to use desktop pane with internal frame my mouse events stops working.

I hope my problem is clear to everyone and hoping for a useful answer. here's the source code:

frame is the internal frame in which the chess game lies. The desktop pane should be above the frame and have menu bar for new game and quit.

 public void createFrame() {

    JInternalFrame frame = new JInternalFrame();

This is my chess board

JLayeredPane layeredPane;
JPanel chessBoard;
JLabel chessPiece;
int xAdjustment;
int yAdjustment;

Dimension boardSize = new Dimension(400, 400);
//FlowLayout flow=new FlowLayout();
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(this);
layeredPane.addMouseMotionListener(this);
chessBoard = new JPanel();
layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
chessBoard.setLayout(new GridLayout(8, 8));
chessBoard.setPreferredSize(boardSize);
chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
for (int i = 0; i < 64; i++) {
    JPanel square = new JPanel(new BorderLayout());
    chessBoard.add(square);
    int row = (i / 8) % 2;
    if (row == 0) {
        square.setBackground(i % 2 == 0 ? Color.LIGHT_GRAY : Color.RED);
    } else {
        square.setBackground(i % 2 == 0 ? Color.RED : Color.LIGHT_GRAY);
    }
}

Chessboard ended. These are my chess pices which contains images of pieces which are placed on the board to be moved

JLabel piece;

    piece = new JLabel( new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects

\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-

of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg") );



JPanel panel = (JPanel)chessBoard.getComponent(0);



panel.add(piece);


piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects

\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-

of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg"));

panel = (JPanel)chessBoard.getComponent(15);



panel.add(piece);

piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects

\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-

of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg"));

panel = (JPanel)chessBoard.getComponent(16);



panel.add(piece);



piece = new JLabel(new ImageIcon("C:\\Users\\Moemmer\\Documents\\NetBeansProjects

\\ChessGame\\src\\Images\\23797642-complete-wooden-chess-set-with-s-full-complement-

of-chess-pieces-in-both-colours-lined-up-in-rows-is - Copy.jpg"));



panel = (JPanel)chessBoard.getComponent(20);

panel.add(piece);          chess pieces code ended)


        frame.setVisible(true); 



        desktop.add(frame);


        frame.setBounds(20,20,410,450);



        frame.add(layeredPane);




        try {



            frame.setSelected(true);




        } catch (java.beans.PropertyVetoException e) {}




    }

    //Quit the application.


    public void quit() {



        System.exit(0);
    }
    @Override

These are the mouse events which worked well when the code was not converted to internal frame and used desktop pane...

public void mousePressed(MouseEvent e){



chessPiece = null;



Component c = chessBoard.findComponentAt(e.getX(), e.getY());



if (c instanceof JPanel)



return;


Point parentLocation = c.getParent().getLocation();



xAdjustment = parentLocation.x - e.getX();



yAdjustment = parentLocation.y - e.getY();



chessPiece = (JLabel)c;


chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);



chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());



layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);


}
    @Override


 public void mouseDragged(MouseEvent me) {



if (chessPiece == null) return;


chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);


}
//Drop the chess piece back onto the chess board
    @Override


    public void mouseReleased(MouseEvent e) {


if(chessPiece == null) return;


chessPiece.setVisible(false);


Component c = chessBoard.findComponentAt(e.getX(), e.getY());


if (c instanceof JLabel){


Container parent = c.getParent();




parent.remove(0);



parent.add( chessPiece );




}



else {



Container parent = (Container)c;



parent.add( chessPiece );



}



chessPiece.setVisible(true);



}



    @Override




    public void mouseClicked(MouseEvent e) {
}
    @Override


    public void mouseMoved(MouseEvent e) {


}
    @Override
    public void mouseEntered(MouseEvent e){
}
    @Override
    public void mouseExited(MouseEvent e) {
}
public void BackgroundImageJFrame()
{

}



Displaying the frame


private static void createAndShowGUI() {


        JFrame.setDefaultLookAndFeelDecorated(true);




        Game frame = new Game();


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);






        frame.setVisible(true);

    }

Main Method>>

    public static void main(String[] args) {




        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();

            }
        });


    }
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • no one wants to look at unformatted code – Paul Samsotha Jan 19 '14 at 19:16
  • See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513), as well as this [Q&A](http://stackoverflow.com/q/21142686/230513). – trashgod Jan 19 '14 at 20:01
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 3) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. 4) See also [Making a robust, resizable Chess GUI](http://stackoverflow.com/q/21142686/418556). – Andrew Thompson Jan 20 '14 at 01:37

0 Answers0