简体   繁体   English

如何在Java中绘制正方形?

[英]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. 用户将按顺序输入网格上正方形的左下角的大小(在边的长度中),x坐标和y坐标作为要编程的命令行参数。

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] . 换句话说,这三个值将在程序开始时存储在String数组args中,分别为值args[0], args[1], and args[2] You can use the command Integer.parseInt() to convert the String into an int. 您可以使用命令Integer.parseInt()将String转换为int。 Assume that the input parameters are valid ints. 假设输入参数是有效的整数。

For example , in the Interactions pane of DrJava: 例如 ,在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). 在2 5 1 1上运行Que s ti意味着我们要绘制一个边长为5的正方形,该正方形的左下角位于位置(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. 默认情况下,程序应将正方形打印在15x15的网格上,但是如果正方形不合适,则必须相应地扩展网格。

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. 建立一个正方形始终适合15x15网格的解决方案。

  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. 如何扩展程序以使其适用于较大的正方形或偏移太大而无法适合15x15网格的正方形。

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. 首先,我们必须获取CLI上给出的arg,然后将其转换为经过清理的int。

    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. 要绘制正方形,我们必须自上而下,因为我们使用的是CLI。 Alternatively one could fill a large char[][] and print it out at the end - top to bottom :-). 或者,可以填充一个大的char[][]并在末尾将其打印出来-从上到​​下:-)。 The whole '-1' in parts of the code is basically due to the fact that arrays and such are 0-based. 部分代码中的整个“ -1”基本上是由于数组等基于0的事实。

    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. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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