Results 1 to 3 of 3
- 07-02-2010, 07:30 PM #1
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 6
- Rep Power
- 0
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:
This is the main class, that uses the class that not use 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 the picture of the original chessboard: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 } }

Uploaded with ImageShack.us
This is the other class that extends JPanel
And finally this class Tablero(that uses cuadro)(The problem happens here)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; } }
This is the picture of the problem: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]); } } } } } }

I'll be very pleased for your help.And sorry for my not perfect english.
- 07-02-2010, 08:17 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 07-02-2010, 10:44 PM #3
Member
- Join Date
- Jul 2010
- Location
- Lima, Peru
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
GridLayout alignment problem
By q8inq8 in forum New To JavaReplies: 9Last Post: 03-16-2010, 07:54 PM -
problem with jpanel
By amith in forum AWT / SwingReplies: 2Last Post: 02-21-2010, 06:40 PM -
Jpanel painting problem
By kcakir in forum AWT / SwingReplies: 3Last Post: 04-15-2009, 10:21 PM -
Not another Jpanel Scrollbar problem!
By jiexx in forum AWT / SwingReplies: 3Last Post: 03-18-2009, 02:09 AM -
Problem with JPanel
By ibanez270dx in forum New To JavaReplies: 2Last Post: 11-09-2007, 05:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
o

Bookmarks