hyunwoo park
hyunwoo park

Reputation: 1

Drawing with drawString doesn't look good

The result of drawString with the same font and size in java The appearance is different when drawn with the same font and size in a painter (or word). Drawing with drawstring in java doesn't look good.

Why does it look so different?

Is there a way to make it look nice like a word or a painter in java?

The top one is drawn in Java, the lower one is painted in painter.

Code:

    Font fontBold28 = new Font("Arial", Font.BOLD|Font.ITALIC, 28);

    g.setFont(fontBold28);
    g.setColor(Color.black);
    g.drawString("ABCDEFG", x, y);

Upvotes: 0

Views: 169

Answers (1)

VGR
VGR

Reputation: 44338

Antialiasing.

Text antialiasing, also known as font smoothing, is a rendering engine’s ability to blur the edges of character shapes, so the edges don’t look jagged.

It’s usually off by default in Java2D Graphics objects, because it causes text drawing to be slower, but in modern computing environments, that performance difference matters less than it used to. You probably won’t notice the difference, unless you are rendering text frequently.

You can turn on text antialiasing using the setRenderingHint method of Graphics2D:

Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

Font fontBold28 = new Font("Arial", Font.BOLD|Font.ITALIC, 28);

g.setFont(fontBold28);
g.setColor(Color.black);
g.drawString("ABCDEFG", x, y);

There are many behaviors of drawing that can be controlled by setting rendering hints. Read the RenderingHints documentation to see them all. The Java Tutorial also describes them.

Upvotes: 1

Related Questions