Javatpoint Logo
Javatpoint Logo

ART in Java

Art in normal conversation means framed paintings; in that context, some people think of art as something only for intellectuals or artsy people. Still, art is for everyone and anyone who wants to experience it, and it's often in more places than we might realize. It may be songs we like, movies we watch, poems in the theatre and even a quilt that our grandmother made; all that and more is art.

Arts have been around for thousands of years, and through the ages, it is evolved in several different ways.

The reason for creating art varies from person to person, depending on many factors. Defining art is pretty tricky and has been debated throughout history; there is not only an agreed-upon definition. However, many believe that art is anything that stirs emotion in us.

Because of this, three different people can experience the same piece of art and have widely different reactions to it.

One might think it is the most beautiful thing they have ever seen. The second person might hate it, the third person might not feel much of anything, and none of them would be wrong.

Everyone is entitled to their preferences and feelings. When it comes to art, we know how we feel about our favorite song could be the same way someone else thinks about their famous sculpture, even though we might not like it. It can be valuable to consider why that person enjoys it so much, and we may learn something about them and ourselves.

The term "art" has been a label over the years. Many people have tried to classify what is what, which is different from what art is about. It is about our personal experience with it and the meaning that we draw from it. Everyone reacts to art differently and has the potential to grow. It allows us to tell stories, records, and history and tap into our emotions in a way that few other things can.

We can also draw the strings in 2D graphics.

Drawing with 2D Graphics by Strings

It is possible to draw a string as an image with the help of the Graphics2D class. It is achieved by calling the drawString() method.

Graphics2D is an abstract class, and we can create an instance by extending it and implementing the various methods associated with the Graphics class.

We know that it is a tedious task, and it is often done by creating a BufferedImage instance in Java and will retrieve the underlying Graphics instance from it.

By Replacing Image Matrix Indices with ASCII Characters

When we draw the strings, the Graphics2D class uses a small matrix-like technique where regions have shown the designed strings and are assigned a particular value while others provide the Zero value.

To replace the carved area, providing the desired ASCII characters, we need to check the values of the carved region as a single data point except for the Red, Green and Blue colour values.

When there is a need to have RGB colour represented as an integer, we set the image type to an integer mode:

The main idea is to replace the values assigned to non-zero indices of the image matrix, which have only the desired artistic character.

The matrix indices represent the zero value assigned with a single space character. The value of zero in equivalent integer mode is -16777216.

Art Generator in ASCII

Let us take a situation where we must make ASCII art of the string "JAVA".

First, we need to start by creating an empty image with the desired width and height and the type of the image set to integer mode, as we discussed above.

When using advanced 2D graphics in Java, we use our Graphics object to a Graphics2D instance. After doing all these, we set the desired parameters before invoking the drawString() method along with the string "JAVA".

In the above code, 13 and 26 are the represents, respectively, and the x and y coordinates for the point on the image from which printing should start.

We also had a 2Dgraphics and an underlying matrix containing two types of values that are discriminated, which are non-zero and zero indices.

Now, we will move to the 2-dimensional array or the matrix and replace all the values with the ASCII character known as "*" by a colon(:).

Let us write a program for the above-discussed pattern.


ART in Java

The above program output is shown as a block of asterisks(*) if we want to replace "*" with an integer value that is equal to -1664578 with an asterisk (*) and the rest all with ":" (colon). In this situation, we write a program to replace "asterisk" with an integer value.


In the output, we obtain an ASCII art corresponding to the string and an inverted carving.

What is ASCII?

ASCII means "American Standard Code for Information Interchange". They are alphanumeric characters and consist of seven bits. There are 128 characters; the problem is that they are only used to represent some characters.

Why do we Draw ASCII Art?

ASCII art has complex patterns, and realistic graphics require a lot of finesse. It can focus on the algorithms.

Development Strategy

There are some recommendations for managing complexity:

  1. Design the program that is thinking about steps or methods. We must write steps in English descriptions that are required for us, and we use these descriptions to decide the ways.
  2. Next is to create a table of patterns of characters like we use tables for writing "for" loops.


It is an English description of an algorithm.

For example, draw 12 wide by seven tall boxes of stars.

Psuedo-code algorithm


( #, 16=, #)

Top half

  1. |
  2. spaces (decreasing)
  3. <>
  4. dots(increasing)
  5. <>
  6. spaces(same as above)
  7. |

The bottom half(top half upside-down)


#, 16=,

Methods from Pseudocode


ART in Java

Scaling the Mirror

Let us modify our mirror program so that it can scale. The current mirror that is left is at the size of 4; the right is at size 3.

We want to structure the code to scale the figure by changing the code in just one place.

ART in Java

Limitations of Variables

Idea: Make a variable to represent the size. So, use the value of the variable in the methods.

Problem: A variable in one method cannot be seen in others.


The part of a program where a variable exists. From its declaration to the end of the {} braces:

A variable declared in a "for" loop exists only in that loop.

A variable declared in a method exists only in that method.

Scope Implications

The variables without overlapping scope can have the same name.

A variable cannot be declared twice or used out of its scope.

Class Constants

A fixed value is visible to the whole program, and a value can be set only at declaration and cannot be reassigned.


The name is usually in ALL_UPPER_CASE

ART in Java


public static final int DAYS_IN_WEEK = 7;

public static final double INTEREST_RATE = 3.5;

public static final int SSN = 658234569;


This article taught us how to create ASCII art in Java using an inbuilt 2D graphics library. We also showed the string by replacing the asterisk with an integer.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA