0

I am trying to create a chess board using the fillrect function in java.The code doesn't seem to be working and adds only the first statement in the frame.Even if I remove the for loop (that prints 64 squares ) and make only 2 add statements,it still prints only the first of them.Here is the code:

import javax.swing.* ;
import java.awt.* ;
public class ChessBoard extends JFrame {
    private int i;
    public ChessBoard (){
    setLayout(new GridLayout(8,8,0,0));
    // there are 64 squares 
    for(i=0; i<64 ;i++){

        if ((i % 2) == 0) //switches between black and white squares
            add(new DrawRect("WHITE"));
        else
            add(new DrawRect("BLACK"));

    }
}
}
class DrawRect extends JPanel{
    private String ngjyra = "BLACK";
    public DrawRect(String b) {
    ngjyra = b ;

    }

    @Override
    protected void paintComponent (Graphics g){
        super.paintComponent(g);
        if (ngjyra.equals("BLACK"))
            g.setColor(Color.BLACK);
        else 
                g.setColor(Color.WHITE);
        g.fillRect(getX(), getY(), getWidth(), getHeight()); 
           //add the square with the specified color

}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Lind
  • 233
  • 1
  • 5
  • 14
  • "adds only the first statement in the frame" what does it mean? Why can't you just call setBackground() for the cells' JPanels? – StanislavL Dec 08 '13 at 10:07
  • if I remove the loop,and make 2 add statements (consistion of a black and white Rect) it adds only the first statement to the frame.I am reading a book to learn java and its an exercise that asks for filledrect.And I would like to solve this particular problem. – Lind Dec 08 '13 at 10:08
  • Not related to your issue, but why can't `DrawRect` hold the `Color` directly? And if you absolutely need it this way, why a string, and not an enum or int? – JensG Dec 08 '13 at 10:12
  • A good idea.Thanks for your info on that one.An int would have been much simpler. – Lind Dec 08 '13 at 10:14
  • @Lind an `enum` would be much simpler. – Moritz Petersen Dec 08 '13 at 10:21
  • Color would be simplest. – JensG Dec 08 '13 at 12:21
  • See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Dec 08 '13 at 18:34
  • Thanks man,at the end of the semester I have to implement a chess game.I need to learn more,but those examples will help me . – Lind Dec 13 '13 at 17:05

2 Answers2

3

Your graphics uses relative coordinates with zero at the top left corner of the component, so the right way to draw rectangle is

g.fillRect(0, 0, getWidth(), getHeight());

Another issue that your colour assignment code is such that all black and all while cells make vertical stripes. Use instead the logic like

    for (int row = 0; row < 8; row++)
        for (int col = 0; col < 8; col++) {
            boolean white = (col % 2 == 0) == (row % 2 == 0);
            add(new DrawRect(white ? "WHITE" : "BLACK"));
        }
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • 1
    Thanks for your info.I had a logic error in the loop which assigned colors,and I didn't use fillrect correctly.Thanks – Lind Dec 08 '13 at 10:29
2

Your problem is with getX() and getY(), they return the same value for each of your DrawRect so they will be drawn one above the other. You could use setBackground instead:

class DrawRect extends JPanel {
   private Color ngjyra = Color.BLACK;

   public DrawRect(Color color) {
      ngjyra = color ;
      setBackground(ngjyra);
}

However you still have a mistake in your loop logic, as you will see if you try the code i posted above.

Blub
  • 3,762
  • 1
  • 13
  • 24
  • I don't think your idea is correct.When I add two statemens like add(new DrawRect("WHITE")); add(new DrawRect("BLACK")); it does not add a black square,but add's a white one.This removes the idea that they are drawn one above the other.The setBackground would be used if I used JPanels ... I am drawing rect. – Lind Dec 08 '13 at 10:17
  • Your DrawRect class is extending a JPanel. You could use setBackground on it to get the same effect without needing to worry about the paint method. – PakkuDon Dec 08 '13 at 10:20
  • As I said I want to draw rect.This is what the exercise asks.I must use the paint method. – Lind Dec 08 '13 at 10:21
  • 1
    Your assumption is wrong. In fact, both `DrawRect`s are added, but drawn on top of each other. It just *looks* like only one is drawn. – Moritz Petersen Dec 08 '13 at 10:22
  • Your class is a JPanel, so you can just use `setBackground`. – Blub Dec 08 '13 at 10:23
  • Edgar Boda your idea was right.I got it now.Thanks for your answer also.It seems I can not give 2 right answers ... :P – Lind Dec 08 '13 at 10:31
  • haha don't worry, after all Audrius' answer is more what you wanted anyway ;-) – Blub Dec 08 '13 at 10:31