Results 1 to 3 of 3
  1. #1
    Jon_Xel is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    6
    Rep Power
    0

    Thumbs up chessboard problem with JPanel and GridLayout

    Well, mi english is not very well, but I'm gonna try, i'm trying to make a simple videogame using swing, at the beginning when I use a two dimensional array of JPanel placing in a cointainer using GridLayout mi program shows the chessboard correctly , but then when I try to use a class that inherits from JPanel instead of using the original JPanel, the program shows only one square of JPanels superimposed.

    This is the original class that not uses extended JPanel:
    Java Code:
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class tablero1 extends JFrame
    {
        private Container contenedor;
        private JPanel[][] Cuadro;
        public tablero1()
        {
            contenedor=getContentPane();
            contenedor.setLayout(new GridLayout(8,8));
            setSize(600,600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pintarCuadros();
        }
        public void pintarCuadros()
        {
            Cuadro=new JPanel[8][8];
            for(int x=0;x<8;x++)
            {
                for(int y=0;y<8;y++)
                {
                    if(x%2==0)
                    {
                        if(y%2!=0)
                        {
                        Cuadro[x][y]=new JPanel();
                        Cuadro[x][y].setBackground(Color.BLACK);
                        contenedor.add(Cuadro[x][y]);
     
                        }
                        else
                        {
                        Cuadro[x][y]=new JPanel();
                        Cuadro[x][y].setBackground(Color.WHITE);
                        contenedor.add(Cuadro[x][y]);
     
                        }
     
                    }
                    else
                    {
                        if(y%2==0)
                        {
                        Cuadro[x][y]=new JPanel();
                        Cuadro[x][y].setBackground(Color.BLACK);
                        contenedor.add(Cuadro[x][y]);
     
                        }
                        else
                        {
                        Cuadro[x][y]=new JPanel();
                        Cuadro[x][y].setBackground(Color.WHITE);
                        contenedor.add(Cuadro[x][y]);
     
                        }
                    }
                }
            }
        }
    }
    This is the main class, that uses the class that not use extended JPanel.
    Java Code:
     
    public class test
    {
        public static void main(String[] args)
        {
            tablero1 gui= new tablero1();//U have to change tablero1 by Tablero
            gui.setVisible(true);            //the class that uses extended JPanel
        }                                        
    }
    This the picture of the original chessboard:


    Uploaded with ImageShack.us

    This is the other class that extends JPanel
    Java Code:
     
    import javax.swing.*;
    import java.awt.*;
    public class cuadro extends JPanel
    {
        private int Y;
        private int X;
        boolean ocupado;
        private int pieza;
        public cuadro(int x,int y)
        {
            this.setLayout(null);
            X=x;
            Y=y;
        }
        public int getX()
        {
            return X;
        }
        public int getY()
        {
            return Y;
        }
        public void setX(int x)
        {
            X=x;
        }
        public void setY(int y)
        {
            Y=y;
        }
    }
    And finally this class Tablero(that uses cuadro)(The problem happens here)
    Java Code:
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    public class Tablero extends JFrame
    {
        private Container contenedor;
        private cuadro[][] Cuadro;
        public Tablero()
        {
            this.setLayout(null);
            contenedor=getContentPane();
            contenedor.setLayout(new GridLayout(8,8));
            setSize(600,600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pintarCuadros();
        }
        public void pintarCuadros()
        {
            Cuadro=new cuadro[8][8];
            for(int x=0;x<8;x++)
            {
                for(int y=0;y<8;y++)
                {
                    if(x%2==0)
                    {
                        if(y%2!=0)
                        {
                        Cuadro[x][y]=new cuadro(x,y);
                        Cuadro[x][y].setBackground(Color.BLACK);
                        contenedor.add(Cuadro[x][y]);
     
                        }
                        else
                        {
                        Cuadro[x][y]=new cuadro(x,y);
                        Cuadro[x][y].setBackground(Color.WHITE);
                        contenedor.add(Cuadro[x][y]);
     
                        }
     
                    }
                    else
                    {
                        if(y%2==0)
                        {
                        Cuadro[x][y]=new cuadro(x,y);
                        Cuadro[x][y].setBackground(Color.BLACK);
                        contenedor.add(Cuadro[x][y]);
     
                        }
                        else
                        {
                        Cuadro[x][y]=new cuadro(x,y);
                        Cuadro[x][y].setBackground(Color.WHITE);
                        contenedor.add(Cuadro[x][y]);
     
                        }
                    }
                }
            }
        }
    }
    This is the picture of the problem:



    I'll be very pleased for your help.And sorry for my not perfect english.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default

    getX() and getY() are methods of the JComponent class.

    I'm sure you don't intend to override these methods so use different method names.

  3. #3
    Jon_Xel is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    6
    Rep Power
    0

    Default

    Oh that was great, i haven`t notice that enormouse mistake, now the program shows the chessboard correctly, thanks...:)o

Similar Threads

  1. GridLayout alignment problem
    By q8inq8 in forum New To Java
    Replies: 9
    Last Post: 03-16-2010, 07:54 PM
  2. problem with jpanel
    By amith in forum AWT / Swing
    Replies: 2
    Last Post: 02-21-2010, 06:40 PM
  3. Jpanel painting problem
    By kcakir in forum AWT / Swing
    Replies: 3
    Last Post: 04-15-2009, 10:21 PM
  4. Not another Jpanel Scrollbar problem!
    By jiexx in forum AWT / Swing
    Replies: 3
    Last Post: 03-18-2009, 02:09 AM
  5. Problem with JPanel
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-09-2007, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •