import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class PanelCenter implements KeyListener,MouseListener


    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;



    public class PanelCenter implements KeyListener,MouseListener
    {
         JButton l1,l2,aircraftcarrier;
         public boolean mouseClick = false;
         int size;
         int x1,y1,x2,y2;

        public PanelCenter(JFrame frame, final PanelTop pT, final PanelBottom pB, final PanelCell[][] bu1, PanelCell[][] CellP2, Boat bp1, Boat bp3, Boat bp5 )
        {

           super();


            JPanel pCenter = new JPanel(new GridLayout(1,3));
            frame.add(pCenter, BorderLayout.CENTER);

            int[][] cellUI = new int[10][10];

            JPanel pCenterLeft = new JPanel(new GridLayout(cellUI.length,cellUI.length,1,1));
            pCenterLeft.setBackground(Color.BLUE);
            pCenter.add(pCenterLeft);

            ImageIcon iconc = new ImageIcon("src\\sea.png");
            ImageIcon icond = new ImageIcon("src\\destroyer.png");

            for(int c = 0; c< cellUI.length; c++) 
            {
                int x = 65 + c;// 65 is equal to A in ASCII Character
                for (int i = 0; i< cellUI.length; i++)
                {

                    cellUI[c][i] = x;
                    String let = Character.toString ((char)cellUI[c][i]) + "" + (i+1);
                    bu1[c][i] = new PanelCell(iconc); 
                    bu1[c][i].setToolTipText(let); 
                    pCenterLeft.add(bu1[c][i]);

                    //Destroyer

                        bu1[c][i].addMouseListener(new java.awt.event.MouseAdapter()
                        {

                            @Override
                            public void mouseEntered(java.awt.event.MouseEvent event)
                            {

                                PanelCell bx = (PanelCell) event.getSource();
                                for (int i = 0; i < 10; i++)
                                    for (int j = 0; j < 10; j++)
                                    {
                                        if (bu1[i][j] == bx)
                                        {
                                            int size = bp3.getsize(); 
                                            int f = (bp3.getsize())-1;

                                bu1[i][j].setIcon(icond);

                                if ((j-f)>=0)
                                {
                                    bu1[i][j-f].setIcon(icond);
                                    bu1[i][j-f+1].setIcon(icond);
                                }
                                if ((i-f)>=0)
                                {
                                    bu1[i-f][j].setIcon(icond);
                                    bu1[i-f+1][j].setIcon(icond);
                                }                                       
                                        }
                                    }
                            }

                            @Override
                            public void mouseExited(java.awt.event.MouseEvent event)
                            {

                                PanelCell bx = (PanelCell) event.getSource();
                                for (int i = 0; i < 10; i++)
                                    for (int j = 0; j < 10; j++)

                                    {
                                        if (bu1[i][j] == bx)
                                        {
                                            int size = bp3.getsize(); 

                                             int f = (bp3.getsize())-1;
                                bu1[i][j].setIcon(iconc);

                                if ((j-f)>=0)
                                {
                                    bu1[i][j-f].setIcon(iconc);
                                    bu1[i][j-f+1].setIcon(iconc);
                                }
                                if ((i-f)>=0)
                                {
                                    bu1[i-f][j].setIcon(iconc);
                                    bu1[i-f+1][j].setIcon(iconc);
                                } 
                                }
                                }
                            }
                            @Override
                             public void mouseClicked(java.awt.event.MouseEvent event)
                            {
                                PanelCell bx = (PanelCell) event.getSource();
                                for (int i = 0; i < 10; i++)
                                    for (int j = 0; j < 10; j++)
                                    {
                                        if (bu1[i][j] == bx)
                                        {
                                            int size = bp3.getsize(); 
                                            int f = (bp3.getsize())-1;

                                bu1[i][j].setIcon(icond);

                                if ((j-f)>=0)
                                {
                                    bu1[i][j-f].setIcon(icond);
                                    bu1[i][j-f+1].setIcon(icond);
                                }
                                if ((i-f)>=0)
                                {
                                    bu1[i-f][j].setIcon(icond);
                                    bu1[i-f+1][j].setIcon(icond);
                                }                                       
                                        }
                                    }                           
                            }   
                            });                        
                        }    

                    }





            ImageIcon iconbs = new ImageIcon("src\\BS.jpg");
            JLabel pCenterMiddle = new JLabel(iconbs);
            pCenter.add(pCenterMiddle, iconbs);


            int[][] cellP2 = new int[10][10];//cellUI2

            JPanel pCenterRight = new JPanel(new GridLayout(CellP2.length,CellP2.length,1,1));
            pCenterRight.setBackground(Color.BLUE);
            pCenter.add(pCenterRight);

            ImageIcon iconr = new ImageIcon("src\\sea.png");

            for(int c = 0; c< CellP2.length; c++) 
            {
                int x2 = 65+c;
                for (int r = 0; r< CellP2.length; r++)
                {
                PanelCell buttonP2 = new PanelCell();
                cellP2[c][r] = x2;
                String let = Character.toString ((char)cellUI[c][r]) + "" + (r+1);
                JButton bu2 = new JButton(iconr);
                bu2.setToolTipText(let);
                bu2.setActionCommand("Hit");
                pCenterRight.add(bu2);
                }
            }
        }

        @Override
        public void keyTyped(KeyEvent ke)
        {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void keyPressed(KeyEvent ke)
        {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void keyReleased(KeyEvent ke)
        {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseClicked(MouseEvent me)
        {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mousePressed(MouseEvent me)
        {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseReleased(MouseEvent me)
        {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseEntered(MouseEvent me)
        {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseExited(MouseEvent me)
        {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }













    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;



    public class PanelCenter implements KeyListener,MouseListener
    {
         JButton l1,l2,aircraftcarrier;
         public boolean mouseClick = false;
         int size;
         int x1,y1,x2,y2;

        public PanelCenter(JFrame frame, final PanelTop pT, final PanelBottom pB, final PanelCell[][] bu1, PanelCell[][] CellP2, Boat bp1, Boat bp3, Boat bp5 )
        {

           super();


            JPanel pCenter = new JPanel(new GridLayout(1,3));
            frame.add(pCenter, BorderLayout.CENTER);

            int[][] cellUI = new int[10][10];

            JPanel pCenterLeft = new JPanel(new GridLayout(cellUI.length,cellUI.length,1,1));
            pCenterLeft.setBackground(Color.BLUE);
            pCenter.add(pCenterLeft);

            ImageIcon iconc = new ImageIcon("src\\sea.png");
            ImageIcon icond = new ImageIcon("src\\destroyer.png");

            for(int c = 0; c< cellUI.length; c++) 
            {
                int x = 65 + c;// 65 is equal to A in ASCII Character
                for (int i = 0; i< cellUI.length; i++)
                {

                    cellUI[c][i] = x;
                    String let = Character.toString ((char)cellUI[c][i]) + "" + (i+1);
                    bu1[c][i] = new PanelCell(iconc); 
                    bu1[c][i].setToolTipText(let); 
                    pCenterLeft.add(bu1[c][i]);

                    //Destroyer

                        bu1[c][i].addMouseListener(new java.awt.event.MouseAdapter()
                        {

                            @Override
                            public void mouseEntered(java.awt.event.MouseEvent event)
                            {

                                PanelCell bx = (PanelCell) event.getSource();
                                for (int i = 0; i < 10; i++)
                                    for (int j = 0; j < 10; j++)
                                    {
                                        if (bu1[i][j] == bx)
                                        {
                                            int size = bp3.getsize(); 
                                            int f = (bp3.getsize())-1;

                                bu1[i][j].setIcon(icond);

                                if ((j-f)>=0)
                                {
                                    bu1[i][j-f].setIcon(icond);
                                    bu1[i][j-f+1].setIcon(icond);
                                }
                                if ((i-f)>=0)
                                {
                                    bu1[i-f][j].setIcon(icond);
                                    bu1[i-f+1][j].setIcon(icond);
                                }                                       
                                        }
                                    }
                            }

                            @Override
                            public void mouseExited(java.awt.event.MouseEvent event)
                            {

                                PanelCell bx = (PanelCell) event.getSource();
                                for (int i = 0; i < 10; i++)
                                    for (int j = 0; j < 10; j++)

                                    {
                                        if (bu1[i][j] == bx)
                                        {
                                            int size = bp3.getsize(); 

                                             int f = (bp3.getsize())-1;
                                bu1[i][j].setIcon(iconc);

                                if ((j-f)>=0)
                                {
                                    bu1[i][j-f].setIcon(iconc);
                                    bu1[i][j-f+1].setIcon(iconc);
                                }
                                if ((i-f)>=0)
                                {
                                    bu1[i-f][j].setIcon(iconc);
                                    bu1[i-f+1][j].setIcon(iconc);
                                } 
                                }
                                }
                            }
                            @Override
                             public void mouseClicked(java.awt.event.MouseEvent event)
                            {
                                PanelCell bx = (PanelCell) event.getSource();
                                for (int i = 0; i < 10; i++)
                                    for (int j = 0; j < 10; j++)
                                    {
                                        if (bu1[i][j] == bx)
                                        {
                                            int size = bp3.getsize(); 
                                            int f = (bp3.getsize())-1;

                                bu1[i][j].setIcon(icond);

                                if ((j-f)>=0)
                                {
                                    bu1[i][j-f].setIcon(icond);
                                    bu1[i][j-f+1].setIcon(icond);
                                }
                                if ((i-f)>=0)
                                {
                                    bu1[i-f][j].setIcon(icond);
                                    bu1[i-f+1][j].setIcon(icond);
                                }                                       
                                        }
                                    }                           
                            }   
                            });                        
                        }    

                    }





            ImageIcon iconbs = new ImageIcon("src\\BS.jpg");
            JLabel pCenterMiddle = new JLabel(iconbs);
            pCenter.add(pCenterMiddle, iconbs);


            int[][] cellP2 = new int[10][10];//cellUI2

            JPanel pCenterRight = new JPanel(new GridLayout(CellP2.length,CellP2.length,1,1));
            pCenterRight.setBackground(Color.BLUE);
            pCenter.add(pCenterRight);

            ImageIcon iconr = new ImageIcon("src\\sea.png");

            for(int c = 0; c< CellP2.length; c++) 
            {
                int x2 = 65+c;
                for (int r = 0; r< CellP2.length; r++)
                {
                PanelCell buttonP2 = new PanelCell();
                cellP2[c][r] = x2;
                String let = Character.toString ((char)cellUI[c][r]) + "" + (r+1);
                JButton bu2 = new JButton(iconr);
                bu2.setToolTipText(let);
                bu2.setActionCommand("Hit");
                pCenterRight.add(bu2);
                }
            }
        }
    }












     JButton l1,l2,aircraftcarrier;
     public boolean mouseClick = false;
     int size;
     int x1,y1,x2,y2;

    public PanelCenter(JFrame frame, final PanelTop pT, final PanelBottom pB, final PanelCell[][] bu1, PanelCell[][] CellP2, Boat bp1, Boat bp3, Boat bp5 )
    {

       super();


        JPanel pCenter = new JPanel(new GridLayout(1,3));
        frame.add(pCenter, BorderLayout.CENTER);

        int[][] cellUI = new int[10][10];

        JPanel pCenterLeft = new JPanel(new GridLayout(cellUI.length,cellUI.length,1,1));
        pCenterLeft.setBackground(Color.BLUE);
        pCenter.add(pCenterLeft);

        ImageIcon iconc = new ImageIcon("src\\sea.png");
        ImageIcon icond = new ImageIcon("src\\destroyer.png");

        for(int c = 0; c< cellUI.length; c++) 
        {
            int x = 65 + c;// 65 is equal to A in ASCII Character
            for (int i = 0; i< cellUI.length; i++)
            {

                cellUI[c][i] = x;
                String let = Character.toString ((char)cellUI[c][i]) + "" + (i+1);
                bu1[c][i] = new PanelCell(iconc); 
                bu1[c][i].setToolTipText(let); 
                pCenterLeft.add(bu1[c][i]);

                //Destroyer

                    bu1[c][i].addMouseListener(new java.awt.event.MouseAdapter()
                    {

                        @Override
                        public void mouseEntered(java.awt.event.MouseEvent event)
                        {

                            PanelCell bx = (PanelCell) event.getSource();
                            for (int i = 0; i < 10; i++)
                                for (int j = 0; j < 10; j++)
                                {
                                    if (bu1[i][j] == bx)
                                    {
                                        int size = bp3.getsize(); 
                                        int f = (bp3.getsize())-1;

                            bu1[i][j].setIcon(icond);

                            if ((j-f)>=0)
                            {
                                bu1[i][j-f].setIcon(icond);
                                bu1[i][j-f+1].setIcon(icond);
                            }
                            if ((i-f)>=0)
                            {
                                bu1[i-f][j].setIcon(icond);
                                bu1[i-f+1][j].setIcon(icond);
                            }                                       
                                    }
                                }
                        }

                        @Override
                        public void mouseExited(java.awt.event.MouseEvent event)
                        {

                            PanelCell bx = (PanelCell) event.getSource();
                            for (int i = 0; i < 10; i++)
                                for (int j = 0; j < 10; j++)

                                {
                                    if (bu1[i][j] == bx)
                                    {
                                        int size = bp3.getsize(); 

                                         int f = (bp3.getsize())-1;
                            bu1[i][j].setIcon(iconc);

                            if ((j-f)>=0)
                            {
                                bu1[i][j-f].setIcon(iconc);
                                bu1[i][j-f+1].setIcon(iconc);
                            }
                            if ((i-f)>=0)
                            {
                                bu1[i-f][j].setIcon(iconc);
                                bu1[i-f+1][j].setIcon(iconc);
                            } 
                            }
                            }
                        }
                        @Override
                         public void mouseClicked(java.awt.event.MouseEvent event)
                        {
                            PanelCell bx = (PanelCell) event.getSource();
                            for (int i = 0; i < 10; i++)
                                for (int j = 0; j < 10; j++)
                                {
                                    if (bu1[i][j] == bx)
                                    {
                                        int size = bp3.getsize(); 
                                        int f = (bp3.getsize())-1;

                            bu1[i][j].setIcon(icond);

                            if ((j-f)>=0)
                            {
                                bu1[i][j-f].setIcon(icond);
                                bu1[i][j-f+1].setIcon(icond);
                            }
                            if ((i-f)>=0)
                            {
                                bu1[i-f][j].setIcon(icond);
                                bu1[i-f+1][j].setIcon(icond);
                            }                                       
                                    }
                                }                           
                        }   
                        });                        
                    }    

                }





        ImageIcon iconbs = new ImageIcon("src\\BS.jpg");
        JLabel pCenterMiddle = new JLabel(iconbs);
        pCenter.add(pCenterMiddle, iconbs);


        int[][] cellP2 = new int[10][10];//cellUI2

        JPanel pCenterRight = new JPanel(new GridLayout(CellP2.length,CellP2.length,1,1));
        pCenterRight.setBackground(Color.BLUE);
        pCenter.add(pCenterRight);

        ImageIcon iconr = new ImageIcon("src\\sea.png");

        for(int c = 0; c< CellP2.length; c++) 
        {
            int x2 = 65+c;
            for (int r = 0; r< CellP2.length; r++)
            {
            PanelCell buttonP2 = new PanelCell();
            cellP2[c][r] = x2;
            String let = Character.toString ((char)cellUI[c][r]) + "" + (r+1);
            JButton bu2 = new JButton(iconr);
            bu2.setToolTipText(let);
            bu2.setActionCommand("Hit");
            pCenterRight.add(bu2);
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent ke)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void keyPressed(KeyEvent ke)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void keyReleased(KeyEvent ke)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseClicked(MouseEvent me)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mousePressed(MouseEvent me)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseReleased(MouseEvent me)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseEntered(MouseEvent me)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseExited(MouseEvent me)
    {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

Recommended Answers

All 2 Replies

I am new guys to this web site and obviously the line is till 247. I would like to stop the mouseListener from loop as when i click with the mouse the hoover starts again.

You need to explain how your program works, what what is your expectation, and what is the program is doing outside your expectation... It is tidious for us to look at quite a long code to try to figure out first.

By the way, on Line 73 you did addMouseListener() and implemented the mouse event. You may need to start looking at that part first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.