简体   繁体   中英

How to draw a square in java?

Here is what I have to do:

You are to write a program that draws a square composed of # signs onto a grid. The user will enter the size (in the length of the sides), the x-coordinate, and the y-coordinate of the bottom-left corner of the square on the grid as command-line parameters to program, in that order.

In other words, the three values will be stored in the String array args at the start of the program, as the values args[0], args[1], and args[2] . You can use the command Integer.parseInt() to convert the String into an int. Assume that the input parameters are valid ints.

For example , in the Interactions pane of DrJava:

run Que s ti on 2 5 1 1 means that we want to draw a square with side length 5 whose bottom-left corner is at position (1, 1). All distances in this question are in number of characters. By default, the program should print the square onto a 15x15 grid, but if the square wouldn't fit, the grid has to be extended accordingly.

I'm trying to build my program in three steps.

  1. write the code to display the axes properly.

  2. build a solution where the square always fits into the 15x15 grid.

  3. How to extend the program to make it work for larger squares or for squares that are too much shifted to fit into the 15x15 grid.

Here are my codes so far:

import java.util.Scanner;

public class drawS {
    public static void main(String[] args) {
        Scanner ask = new Scanner(System.in);

        int length = ask.nextInt();
        int i;
        int r = 15;
        int xmax = 15;
        int ymax = 15;

        drowLine(length);

        System.out.println();

        if (i == 0 && j == 0) {
            System.out.print("+");
        }
        if (i == 0 && j <= ymax) {
            System.out.print("|");
        }
        if (j == ymax) {
            System.out.print("^");
        }
        for (int j = ymax; j >= 0; j--) {
            for (int i = 0; i <= xmax; i++) {
                for (i = 0; i < length - 2; i++) {
                    drowEmptyLine(length);
                    System.out.println();
                }
                drowLine(length);
            }

            public static void drowLine ( int n){
                for (int i = 0; i < n; i++) {
                    System.out.print("");
                }
            }

            public static void drowEmptyLine ( int n){
                System.out.print("");
                for (int i = 0; i < n - 2; i++) {
                    System.out.print("# ");
                }
                System.out.print('#');
            }
        }
    }
}

Doing a little grave digging apparently :-)

To start with we have to grab the args given on the CLI and turn them to sanitized ints.

    public static void main(String[] args) {
        try {
            int size = Integer.parseInt(args[0]);
            int x = Integer.parseInt(args[1]);
            int y = Integer.parseInt(args[2]);

            if (size < 1 || size > 30) {
                System.out.println("Setting size to 5");
                size = 5;
            }

            if (x < 0 || x > 30) {
                System.out.println("Setting x to 2");
                x = 2;
            }

            if (y < 0 || y > 30) {
                System.out.println("Setting y to 2");
                y = 2;
            }

            int height = Math.max(y + size + 1, 15);
            int width = Math.max(x + size + 1, 15);
            drawSquare(height, width, size, x, y);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

To draw the square we have to go top to bottom because we are using the CLI. Alternatively one could fill a large char[][] and print it out at the end - top to bottom :-). The whole '-1' in parts of the code is basically due to the fact that arrays and such are 0-based.

    static void drawSquare(int height, int width, int size, int x, int y) {
        for (int line = height -1 ; line >= 0; line--) {
            drawline(line, size, x, y, width);
        }
    }

Lines can be categorized in either empty lines, those representing the top and bottom of the square, and those representing the square's content.

    static void drawline(int line, int size, int x, int y, int width) {
        if (line > y + size - 1 || line < y) {
            drawEmptyLine(width);
        } else if (line == y + size - 1 || line == y) {
            drawBorder(x, size, width);
        } else {
            drawContent(x, size, width);
        }
    }

Empty lines are easy:

    static void drawEmptyLine(int width) {
        draw(emptyLine(width));
    }

    static char[] emptyLine(int width) {
        char[] dots = new char[width];
        Arrays.fill(dots, 0, width, '.');
        return dots;
    }

    static void draw(char[] chars) {
        StringBuffer sb = new StringBuffer(chars.length);
        sb.append(chars);
        System.out.println(sb);
    }

The border and content lines simply replace the dots in an empty line by some hashes:

    static void drawContent(int x, int size, int width) {
        char[] dots = emptyLine(width);
        dots[x] = '#';
        dots[x + size - 1] = '#';
        draw(dots);
    }

    static void drawBorder(int x, int size, int width) {
        char[] dots = emptyLine(width);
        Arrays.fill(dots, x, x + size, '#');
        draw(dots);
    }

That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM