javafor-loopconditional-statementsstddraw

Java double for-loop and condition usng stdDraw to draw a picture, can not find the failure


I am learning java at home since university has been closed. recently I am trying to draw a picture using StdDraw as you see in the picture below, but I don't understand, why the red circle in the middle is missing, I hope you could help me, thank you!

one red circle in the middle is missing

import java.awt.*;

public class draw1 {

public static void main(String[] args) {
    int n = 8;
    int width = 300;
    int height = 300;
    int circle_x = width / n / 2;
    int circle_y = height / n / 2;
    int radius = 300 / n / 2;

    StdDraw.setCanvasSize(width, height);
    StdDraw.setXscale(0, 300);
    StdDraw.setYscale(0, 300);
    StdDraw.setPenRadius(0.002);
    StdDraw.setPenColor(Color.blue);

    for (int i = 1; i <= n; i++) {


        for (int j = 1; j <= n; j++) {
            if (i == 1 || i == n) {
                if (j == 1 || j == n) {
                    StdDraw.setPenColor(Color.blue);
                    StdDraw.setPenRadius(0.002);
                    StdDraw.circle(circle_x, circle_y, radius);
                } else if (j >= 2 && j < n) {
                    StdDraw.setPenColor(Color.red);
                    StdDraw.setPenRadius(0.006);
                    StdDraw.circle(circle_x, circle_y, radius);
                }


            } else  {
                if (j == 1 || j == n) {
                    StdDraw.setPenColor(Color.blue);
                    StdDraw.setPenRadius(0.002);
                    StdDraw.circle(circle_x, circle_y, radius);
                } else if (j >= 2 ) {
                    if (n % 2 == 0) {
                        if (j == n / 2) {
                            StdDraw.setPenColor(Color.orange);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x + radius, circle_y, radius);



                        } else if ( (i == n / 2 )  ){
                            StdDraw.setPenColor(Color.red);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x, circle_y + radius, radius);

                            System.out.println(i + "=i");

                        }
                    } else if (n % 2 != 0) {
                        if (j == n / 2 + 1) {
                            StdDraw.setPenColor(Color.orange);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x, circle_y, radius);
                        } else if (i == n / 2 + 1) {
                            StdDraw.setPenColor(Color.orange);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x, circle_y, radius);
                        }
                    }


                }
            }
            System.out.println(j);
                    circle_x = circle_x + (radius * 2);
                }
                circle_x = width / n / 2;
                circle_y = circle_y + width / n;


            }


        }

    }

Solution

  • I think this might be your problem.

                    if (n % 2 == 0) {
                        if (j == n / 2) {
                            StdDraw.setPenColor(Color.orange);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x + radius, circle_y, radius);
    
    
    
                        } else if ( (i == n / 2 )  ){    <---- HERE MIGHT BE YOUR PROBLEM
                            StdDraw.setPenColor(Color.red);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x, circle_y + radius, radius);
    
                            System.out.println(i + "=i");
    
                        }
    

    There is one case in which i and j are the same value. In your case it is 4. If i and j both equals to 4 the code will always go into the first if (j == n/2) and will not execute the else if... So basically the red circle you asked for will not be drawn.

    Btw: The n%2==0 check is practically useless because you are not changing the value of n (it is always 8). So your last code will never be executed:

                    } else if (n % 2 != 0) {
                        if (j == n / 2 + 1) {
                            StdDraw.setPenColor(Color.orange);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x, circle_y, radius);
                        } else if (i == n / 2 + 1) {
                            StdDraw.setPenColor(Color.orange);
                            StdDraw.setPenRadius(0.006);
                            StdDraw.circle(circle_x, circle_y, radius);
                        }
                    }
    

    No guarantee though but based on your code that might be the reason.