Problem drawing chess board with objects

Asked

Viewed 197 times

2

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

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 keep this output: Output

1 answer

1

The problem is in the method drawGrid. 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);
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.