0

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?

200 L.B
  • 145
  • 1
  • 1
  • 8
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Apr 19 '15 at 22:48

2 Answers2

4

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".

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • The creation of CaseBlanche and CaseNoire is just for illustrating the polymorphisme principal and we need it – 200 L.B Apr 20 '15 at 09:37
  • And what happened when you implemented my suggestion to get rid of all the instance variables? – camickr Apr 20 '15 at 15:11
  • I followed your suggestion and got rid of x an y but does not resolve my Issue. for your question I will do something that's against the specifications of the school project – 200 L.B Apr 20 '15 at 19:45
0

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);            
}
200 L.B
  • 145
  • 1
  • 1
  • 8
  • 1
    and your solution is to ignore the x/y variables, which is why they should NOT be defined in your class. You are not using them, so get rid of them. – camickr Apr 20 '15 at 19:56
  • I'm using them as I should get back the case based on col and line number – 200 L.B Apr 20 '15 at 20:11
  • 1
    You should NOT create variables x, y, width, height as I have already mentions. These are variable defined by the Component clase and are used to set the size and location of a component by the layout manager. If you want to know what component is at a row/column number then you can use the getLocation() method which will return a Point object giving the x/y values. You can then use the getSize() method which returns a Dimension object for the width/height values. – camickr Apr 20 '15 at 20:39
  • 1
    Tell you teacher it is wrong to redefine these variables and that the teacher should be teaching proper coding techniques so we don't have to spend so much time fixing bad habits in the forums. Overriding these variable will get you into problems in the future. The code only works because the variable have the exact meaning a the Component class. Since they do have the exact meaning there is no need to redefine them. – camickr Apr 20 '15 at 20:41