1

my task is to create a 10x10 resizable chessboard through the paintComponent method.
My problem is that I get the first row right, but the next rows don't display and I simply don't know where my mistake is

GrafikPanel Class:

import java.awt.*;
import javax.swing.*;

public class GrafikPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int width = g.getClipBounds().width;
        int height = g.getClipBounds().height;

        int lines = 0;
        while(lines < 10){
            int posH = height;
            int posW = width/10;
            int squares = 0;
            while(squares < 10){
                if(squares%2 == 0){
                    if(lines%2 != 0){
                        g.setColor(Color.BLACK);
                    }
                    else {
                        g.setColor(Color.WHITE);
                    }
                    g.fillRect(width-posW, height-posH, width/10, height/10);
                }
                if(squares%2 != 0){
                    if(lines%2 != 0){
                        g.setColor(Color.WHITE);
                    }
                    else {
                        g.setColor(Color.BLACK);
                    }
                    g.fillRect(width-posW, height-posH, width/10, height/10);
                }
                posW += width/10;
                squares++;
            }
            posH -= height/10;
            lines++;
        }
    }
    @Override
    public Dimension getPreferredSize(){
        return new Dimension(500, 500);
    }
}

Auf1 Class:

import java.awt.*;
import javax.swing.*;

public class Auf1 extends JFrame {
    GrafikPanel panel;
    public Auf1(){
        setTitle("Schachbrett");

        panel = new GrafikPanel();
        add(panel);

        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args){
        new Auf1();
    }
}

My code is not formatted correctly, since I can't get used to the way one inputs code in here, sry for that. If someone could tell me where I have too look to fix my mistake, that'd be great.

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
Charles Nough
  • 353
  • 1
  • 3
  • 15

1 Answers1

3

I think you should just change the attitude of placing squares on the board. Your attitude is somehow confusing, because it tries to create an offset from the right side of the board, and every time in the loop you decrease the size and offset.

I used another way for calculating the places of the squares from left to right and now it became shorter and easier to understand:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

public class GrafikPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int width = g.getClipBounds().width;
        int height = g.getClipBounds().height;
        //
        int rowOffset = width/10;
        int colOffset = height/10;
        int squareWidth = width/10;
        //
        int lines = 0;
        while(lines < 8){

            int squares = 0;

            while(squares < 8){
                if(squares%2 == 0)
                {
                    g.setColor(lines%2 != 0 ? Color.BLACK : Color.WHITE);
                }
                else
                {
                    g.setColor(lines%2 != 0 ? Color.WHITE : Color.BLACK);
                }
                g.fillRect(rowOffset+(squares*squareWidth), colOffset+(lines*squareWidth), squareWidth, squareWidth);
                //
                squares++;
            }

            lines++;
        }
    }
    @Override
    public Dimension getPreferredSize(){
        return new Dimension(500, 500);
    }
}

Also I've change the number of rows and column from 10 to 8.

Hope this would be helpful,

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43
  • Your way of calculating makes more sense than mine, going to try it out that way as soon as I got time. Could you tell me what " ? Color.BLACK : Color.WHITE" this line is supposed to be? You substituted some of my ifs with it and it looks alot nicer, how is that operation called? In my case I'd also have to get the squares at the border of the window, hence I started off with 0 axis and than added/sub the height will have to try if that's possible with your method. – Charles Nough Oct 20 '15 at 13:13
  • 2
    @CharlesNough here is a quick and simple tutorial explaining the operation :) [click](http://www.cafeaulait.org/course/week2/43.html) – Phantomazi Oct 20 '15 at 13:33
  • @Charles Nough as Phantomazi said it is a a ternary operator that checks the condition before the ? and if it was true returns the statement in the left hand side of the : operator otherwise returns the right hands side. – STaefi Oct 20 '15 at 13:52
  • @Charles Nough: it is possible to start from the borders of the window. The solution is to set the `rowOffset = 0` and `colOffset = 0`. Also if you want that the squares fill all the surface, you should change the conditions `while(lines < 8)` to `while(lines < 10)` and `while(squares < 8)` to `while(squares < 10)` – STaefi Oct 20 '15 at 13:57