Problem in drawing chessboard with objects

2

An exercise asks you to use basic objects of type Rectangle , Polygon , Elipse ... I used cycles for but only part of the board appears correct. I leave the code below:

import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.scene.shape.*;
import javafx.embed.swing.*;
import javafx.application.*;
import javafx.scene.text.*;
import java.util.*;
import javafx.scene.paint.Color;

public class PequenosProgramas  {  
   private Pane pane;
private void start(Stage primaryStage) 
{
    primaryStage.setOnCloseRequest(
        e -> Platform.runLater( () -> {Platform.exit(); System.exit(0);} ) );

    // WRITE YOUR CODE HERE
    // TODO    

    // https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Pane.html
    this.pane = new Pane();
    this.pane.setPrefSize(600, 600);
    primaryStage.setScene(new Scene(this.pane, Color.BLACK));
    primaryStage.show();

    //this.drawRow(10);   TIRAR COMENTARIO PARA EXECUTAR A FUNÇAO "drawRow".
    this.drawGrid(8);
} // END start

/**
 * Add shape to pane 
 */
public void addShape(Shape shape)
{
    Platform.runLater(() -> this.pane.getChildren().add(shape));
}

/** execute this method to start the program
 * executing the code in method start(Stage primaryStage) 
 */
public static void start()
{
    PequenosProgramas drawingApp = new PequenosProgramas();
    drawingApp.launch();
}

public void launch()
{
    // Initialises JavaFX:
    new JFXPanel();
    // Makes sure JavaFX doesn't exit when first window is closed:
    Platform.setImplicitExit(false);
    // Runs initialisation on the JavaFX thread:
    Platform.runLater(() -> start(new Stage()));
}

public PequenosProgramas() 
{
    super();
}

// private void drawRow (int nSquares)
// {
// int i = 0;
// int positionX = 5;
// while(i<10)
// {
// positionX= positionX +30;
// Rectangle square = new Rectangle(positionX,10,20,20);
// if(i%2 == 0)
// {
// square.setStrokeWidth(1);
// square.setStroke(Color.BLACK);
// square.setFill(Color.WHITE);
// }else{
// square.setStrokeWidth(0);
// square.setStroke(Color.BLACK);
// square.setFill(Color.BLACK);
// }
// pane.getChildren().add(square);
// i++;
// }
// }

private void drawGrid(int nRowsAndColumns)
{
    int side = 50;
    for(int i = 100;i<400;i+=100)
    {
        for(int j =100;j<400;j+=100)
        {

            Rectangle chessColumn =new Rectangle(i,j,side,side);
            chessColumn.setStrokeWidth(1);
            chessColumn.setStroke(Color.BLACK);
            chessColumn.setFill(Color.WHITE);
            pane.getChildren().add(chessColumn);

        }
    }

    for(int k =150;k<=450;k+=100)
    {
        for(int l=150;l<=450;l+=100)
        {
     Rectangle chessLigne = new Rectangle(k,l,side,side);
     chessLigne.setStrokeWidth(1);
     chessLigne.setStroke(Color.BLACK);
     chessLigne.setFill(Color.WHITE);
     pane.getChildren().add(chessLigne);
    }
    }

}

} // END class World

And get this output:

    
asked by anonymous 11.12.2017 / 19:52

1 answer

1

The problem is in the drawGrid method. The stop condition of the first loop is wrong. You are using a < when it should be a < =.

private void drawGrid(int nRowsAndColumns) {
    int side = 50;
    for (int i = 100; i <= 400; i += 100) {
        for (int j = 100; j <= 400; j += 100) {

            Rectangle chessColumn = new Rectangle(i, j, side, side);
            chessColumn.setStrokeWidth(1);
            chessColumn.setStroke(Color.BLACK);
            chessColumn.setFill(Color.WHITE);
            pane.getChildren().add(chessColumn);

        }
    }

    for (int k = 150; k <= 450; k += 100) {
        for (int l = 150; l <= 450; l += 100) {
            Rectangle chessLigne = new Rectangle(k, l, side, side);
            chessLigne.setStrokeWidth(1);
            chessLigne.setStroke(Color.BLACK);
            chessLigne.setFill(Color.WHITE);
            pane.getChildren().add(chessLigne);
        }
    }
}
    
13.12.2017 / 02:42