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.