Draw in console (overview)
YAJC become Java programmerIn the previous post (https://t.me/yajc_java/16) we've created an application which prints to console randomly colored blocks. Let's go through the source code and see what it does.
ConsoleColors.java
This class is quite simple. It is used as a container for special constants which could be used multiple times in the project. We just group and name them to ConsoleColors "space".
public class ConsoleColors {
define a new class with public visibility (you can use this class from any other class)
public static final String RESET = "\033[0m"; public static final String RED_BACKGROUND = "\033[41m"; public static final String GREEN_BACKGROUND = "\033[42m"; public static final String YELLOW_BACKGROUND = "\033[43m";
define 4 static fields of the class which do not require you to create an object (instance of a class, you can think about class as a template for objects). Each field represents a sequence of characters (string) which enables special output mode in console.
Draw.java
Draw class has the main logic and is an entrypoint for the program execution.
import java.util.Arrays; import java.util.List; import java.util.Random;
This construction specifies which other classes we would like to use in out project with short names. Without an import one should refer to a class by the whole name like java.util.Arrays. Instead you can add import for this class and use it as Arrays in your class.
public class Draw {
Like in ConsoleColors we just define out class
public static void main(String[] args) {
This is the main function - entrypoint for our program
List<String> colors = Arrays.asList(
ConsoleColors.YELLOW_BACKGROUND,
ConsoleColors.GREEN_BACKGROUND,
ConsoleColors.RED_BACKGROUND
);
We pack all the colors we would like to use into a collection of items. They have a strict order and we can access each of them by an index.
For instance if we would like to access ConsoleColors.RED_BACKGROUND then we need to write colors[2]. Pay attention indices start with 0.
Random random = new Random();
Because we would like to choose a random color we need to generate a random index. This class gives this functionality out of box. We're lucky it exists as internals of this are extremely hard.
for (int y = 0; y < 20; y++) {
// action
}
This is an example of a repeatable action. Instead of writing some code multiple times we can introduce this loop with common part.
for (int y = 0; y < 20; y++) {
// A action
for (int x = 0; x < 40; x++) {
// B action
int randomColorIndex = random.nextInt(colors.size());
String color = colors.get(randomColorIndex);
System.out.print(color + "\u2007");
}
System.out.println(ConsoleColors.RESET);
}
We can even put a loop inside another loop 😊
This construction gives us opportunity to repeat one action (let's name it A) 20 times and another one (let's name it B) 40 times. A action includes B action repeated 40 times and additional System.out.println(ConsoleColors.RESET); which forces output to go to the next line (this happens because we use println method, there is print method which does not create a new line)
In B action we generate a new random number from 0 to colors.size() exclusive (because the last index of colors is colors.size() - 1, remember indices start with 0). Then we take the color and output it (without a new line) to the console.
Here "\u2007" represents a space symbol. It's a bit wider than " " whitespace symbol and output looks better.
If you have any question, please welcome to the group-chat. See you 👋