Crispy Flowe
Crispy Flowe

Reputation: 7

Why java Antialiasing doesn't works?

I'm trying to create a game, and trying to make my text smoother. I have following rendering method, with antialiasing code:

    @Override
    public void paint(Graphics pen) {
        offscreen_canvas = this.createImage(WIDTH, HEIGHT);
        Graphics2D g2d = (Graphics2D) pen;

        // prevent flashing
        Graphics2D offscreen_pen = (Graphics2D) offscreen_canvas.getGraphics();

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // and I do something with graphics created
        game_obj.proc(offscreen_pen);
        g2d.drawImage(offscreen_canvas, 0, 0, getWidth(), getHeight(), null);
    }

But the quality of text is still very low, doesn't improve at all.

I try to use this:

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

But there's no difference, still doesn't works. this is the rendered text with code above two: second text

However, with the same code, when I trying to change antialiasing line to any of these following line (not both of them, any one of them):

g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

or

g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

Surprisingly, Text quality significantly improved, and I don't even need g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); at all. image: Improved text quality and its my expectation two: another one

But the side effect is, my game is also slow down significantly. My game object move 20 times slower than my original code.

Seems like nobody else have similar problem with me. I use this answer: https://stackoverflow.com/a/59431454, but I have no idea why this will happens at all.

So, why my antialias code doesn't works? what does KEY_RENDERING and KEY_INTERPOLATION do and why did they works? And why these two will slow down my game?

additionally: code of entire class if needed: https://qtext.io/o9nq

Upvotes: 0

Views: 102

Answers (0)

Related Questions