200 L.B
200 L.B

Reputation: 155

Issue in Painting JComponents added to JComponent

I have an Issue painting jcomponent

//class where the rectangle should drawed

public class Board extends JComponent
{
    private Case[][] cases= new Case[10][10];  

    public Plateau() {
        super();
        this.setLayout(new GridLayout(10,10));
        this.setSize(getPreferredSize());

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if ((i + j) % 2 == 0) {

                  cases[i][j] = new WhiteCase(j * Case.LONGUEUR, i * Case.LONGUEUR, Case.LONGUEUR, Case.LONGUEUR);
                } else {
                    cases[i][j] = new BlackCase(j * Case.LONGUEUR, i * Case.LONGUEUR, Case.LONGUEUR, Case.LONGUEUR);

                }                          
                add(cases[i][j]);
            }
        }
        repaint(); 

    }   
    public Dimension getPreferredSize() {
          return new Dimension(600, 600);
        }
}

//class Base for rectangle

public abstract class   Case extends JComponent  {


    protected static final int LONGUEUR=60;

    protected  int x,y,width,height;                
    protected abstract void paintComponent(Graphics g);
    public Dimension getPreferredSize() {             return new Dimension(LONGUEUR, LONGUEUR);
        }
}

///black Case

public class BlackCase extends Case
{

    private Piece piece; 
    private static final long serialVersionUID = 1L;
    public CaseNoire(int x, int y,int width,int height)
    {

        this.x=x;
        this.y=y;  
        this.width = width;   
        this.height= height;
    }
    public Dimension getPreferredSize() {
          return new Dimension(LONGUEUR, LONGUEUR);
        }
@Override
protected void paintComponent(Graphics g)
{   

    g.setColor(Color.darkGray);      
    g.fillRect(x, y,width,height);                  
}
}


public class CaseWhite extends Case {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public CaseBlanche(int x, int y,int width,int height)
    {

        this.x=x;
        this.y=y;  
        this.width = width;   
        this.height= height;


    }

    @Override
    public void paintComponent(Graphics g)
    {

        g.setColor(Color.white);      
        g.fillRect(x, y,width,height);
        g.setColor(Color.BLACK);
        g.drawString("X= "+x , 10, 10);
    }
    public Dimension getPreferredSize() {
          return new Dimension(LONGUEUR, LONGUEUR);
        }

}

//Main  class
public class CheckersGame extends JFrame {

    private static final long serialVersionUID = 1L;

    public static void main(String[] args )
    {
     CheckersGame  checkers= new CheckersGame();  
    } 
    public  CheckersGame()    
    {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Jeu de Dames");                     
        JPanel panelPrincipalDame = new JPanel(new GridBagLayout());  

        Board board = new Board();
        GridBagConstraints c  =  new GridBagConstraints(); 
        c.fill= GridBagConstraints.NONE;  
        c.gridx =0; 
        c.gridy = 0 ; 
        c.gridheight= 2; 
        c.gridwidth= 2;  

        panelPrincipalDame.add(plateau,c);  
  setSize(800, 700);
        setContentPane(panelPrincipalDame);     

        ![//setVisible(true);][1]
        setResizable(false);        
    }    
}

The result of this code is ( Note X+ 0 etc.. is just for debug purpose ) enter image description here

But what I want is this enter image description here

Please why I get only one rectangle?

Upvotes: 0

Views: 86

Answers (2)

200 L.B
200 L.B

Reputation: 155

I resolved it I just set the X=0 and Y= 0 in the paintComponent()

protected void paintComponent(Graphics g)
{   

    g.setColor(Color.darkGray);      
    g.fillRect(0, 0,width,height);            
}

Upvotes: 0

camickr
camickr

Reputation: 324088

So much for listening to my suggestion to NOT create "CaseNoire" and "CaseBlanch" classes I gave in your last question: paintComponent does not paint correctly from two weeks ago. These classes are not needed. What happens if you ever want to give the user the flexibility to choose the colors of the squares. Your game logic should never be based on the class name or anything like that. So get rid of the classes and use the built in Swing features to color the background of the component.

I think the problem is because you created variables "x, y, width, height" in the Case class. I believe these variables already defined in the Component class, to represent the size/location of the component.

Get rid of the variables, you don't need to manage the size/location of each component because the GridLayout will do this for you.

Again, look at the example code I gave you that shows how to create a "ChessBoard".

Upvotes: 4

Related Questions