Printing an empty square using buttonholes

I need to print an empty square and my code will print that instead and I have no idea why.

        ******
        *    *
        *    *
        *    *
        *    *
        ******

      

Can you explain why it is a rectangle and not a square, and how can I fix it?

This is my code:

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int m = 6;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= m; j++) {
                if (i == 1 || i == m)
                    System.out.print("*");
                else if (j == 1 || j == m)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }

      

+3


source to share


3 answers


You are typing characters into a text console. So if you print a 6 X 6 shape and each character is, for example, 5mm X 10mm, you get 30mm X 60mm on the screen.

You cannot control this from your Java program. It would be very difficult to know the current aspect ratio of your text console character (and the user can subsequently change the font at will) and you cannot change the font (from Java or platform independent).

Fortunately, all modern PC displays have nearly square pixels. So, choose a bitmap font with the same width and height for the characters. For example, in Windows cmd.exe, under the image of the Properties dialog box (accessed from the top left corner), one feature is demonstrated:

cmd.exe font selection dialog




If you need a larger font, you can choose a 10x20 font (width - half the height) and print extra space after each *

.

Or you can choose a 16x8 font (twice the width of the height) and then print an extra blank line between each line.

Otherwise you won't be able to do it in text mode, alias problems will make the non-integer aspect ratio really ugly. The only way to do this "right" is to make your "text console" a graphics window, where you draw your text drawing so that the character positions are independent of the font size.

+2


source


It is a square (6 rows and 6 columns). It is not your fault that the columns in your display are narrower than the rows.



+3


source


You must include a space in the Pint statement after *

.

The code below will work.

public class Test
{
public static void main(String[] args) {

        // TODO Auto-generated method stub
        int m = 6;
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= m; j++) {
                if (i == 1 || i == m)
                    System.out.print("* ");
                else if (j == 1 || j == m)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
}

      

+1


source







All Articles