How to Enable Anti Aliasing in Arbitrary Java Apps

How do you enable anti aliasing in arbitrary Java apps?

If you have access to the source, you can do this in the main method:

  // enable anti-aliased text:
System.setProperty("awt.useSystemAAFontSettings","on");

or, (and if you do not have access to the source, or if this is easier) you can simply pass the system properties above into the jvm by adding these options to the command line:

-Dawt.useSystemAAFontSettings=on

What's the proper way for a `BasicLookAndFeel` subclass to activate anti-aliasing in Java 11?

Anti-aliasing settings from the Look&Feel are picked up by JComponent.setUI().

In Java 8, it does UIManager.getDefaults().get(SwingUtilities2.AA_TEXT_PROPERTY_KEY).

In Java 9, it does UIManager.getDefaults().get(RenderingHints.KEY_TEXT_ANTIALIASING).

There is no gradual transition so that you can clean up the API in one step and transition to a newer Java in a separate step, you have to do both at the same time.

Enable anti-aliasing with lwjgl

Comments

Antialiasing can be achieved by creating a multisample framebuffer, either in a framebuffer object or through the default framebuffer when the LWJGL window is created for the first time.

If you are learning LWJGL and OpenGL then learn how to use VBOs, because glBegin, glVertex, etc. are removed from the core profile of OpenGL.

VBO Example

Here is a little example of a VBO storing Vertices and Texture Coordinates for two triangles and rendering it!

I assume that you know how to load and bind textures already.

Creating the VBO

This is the code where you create the actual Vertex and Texture Coordinate Buffer and store them onto the GPU.

int vertices = 6;

int vertex_size = 3; // X, Y, Z,
int texture_size = 2; // U, V,

FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, 1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex
vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex

vertex_data.put(new float[] { 1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex
vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex

FloatBuffer texture_data = BufferUtils.createFloatBuffer(vertices * texture_size);
texture_data.put(new float[] { 0f, 1f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate
texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate

texture_data.put(new float[] { 1f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate
texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate

vertex_data.flip();
texture_data.flip();

int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

int vbo_texture_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
glBufferData(GL_ARRAY_BUFFER, texture_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

Rendering the VBO

Then when you want to render the VBO, you need to do the following.

texture.bind();

glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);

glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
glTexCoordPointer(texture_size, GL_FLOAT, 0, 0l);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glDrawArrays(GL_TRIANGLES, 0, vertices); // The vertices is of course the max vertices count, in this case 6

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, 0);

texture.unbind();

Deleting the VBO

Then when you're done with the VBO and you don't need it anymore, you can delete it by doing the following.

glDeleteBuffers(vbo_vertex_handle);
glDeleteBuffers(vbo_texture_handle);

LWJGL/OpenGL - Static Imports

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

Info

You only have to create a VBO once (It is really bad to create one for each frame), so if you used VBO to store your terrain. Then if some changes happens then only at that point you want to update the VBO, else just keep it as it is. If you use the VBO for terrain and the terrain is really huge, then split the terrain into different chunks of terrain with it's own VBO.

Extra

If you have an Wavefront OBJ Model, and you want to render it multiple times, the best way to do that, would be to load the whole model to one VBO. Then you would do some Instancing to render it multiple times in multiple position, etc.

  • Instancing Tutorial 1
  • Instancing Tutorial 2

Update

A little example of a VBO storing Vertices and Colors for a Triangle and rendering it!

Creating the VBO.

This is the code where you create the actual Vertex and Color Buffer and bind them to the VBO.

int vertices = 3;

int vertex_size = 3; // X, Y, Z,
int color_size = 3; // R, G, B,

FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
vertex_data.put(new float[] { -1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, -1f, 0f, });
vertex_data.put(new float[] { 1f, 1f, 0f, });
vertex_data.flip();

FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * color_size);
color_data.put(new float[] { 1f, 0f, 0f, });
color_data.put(new float[] { 0f, 1f, 0f, });
color_data.put(new float[] { 0f, 0f, 1f, });
color_data.flip();

int vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

int vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

Rendering the VBO.

This is the code you need to call, to render the VBO.

glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);

glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(color_size, GL_FLOAT, 0, 0l);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLES, 0, vertices);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

The imports and the code for disposing/deleting the buffers are the same as the previous example!

Update 2 - Complete Example

Okay, so here is a complete example of a VBO containing Vertices and Colors. The example is 100% LWJGL only!

When I run the following code this is the desired result I get.

Test Image

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class VBOTest
{
public final static void main(String[] args)
{
int width = 1280;
int height = 720;

try
{
Display.setTitle("VBO Test");
Display.setDisplayMode(new DisplayMode(width, height));

Display.create();
}
catch (Exception ex)
{
ex.printStackTrace();

System.exit(0);
}

/*
* Initialize OpenGL States
*/

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0f, width, height, 0f, -1f, 1f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

/*
* Creating the Vertex & Color VBO
*/

final int VERTEX_SIZE = 3; // X, Y, Z,
final int COLOR_SIZE = 4; // R, G, B, A,

int vertices = 6;

int vbo_vertex_handle;
int vbo_color_handle;

FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * VERTEX_SIZE);

float half_width = 200f;
float half_height = 200f;

vertex_data.put(new float[] { -half_width, -half_height, 0f, });
vertex_data.put(new float[] { -half_width, half_height, 0f, });
vertex_data.put(new float[] { half_width, -half_height, 0f, });

vertex_data.put(new float[] { half_width, half_height, 0f, });
vertex_data.put(new float[] { half_width, -half_height, 0f, });
vertex_data.put(new float[] { -half_width, half_height, 0f, });

vertex_data.flip();

FloatBuffer color_data = BufferUtils.createFloatBuffer(vertices * COLOR_SIZE);

color_data.put(new float[] { 1f, 0f, 0f, 1f, });
color_data.put(new float[] { 1f, 0f, 1f, 1f, });
color_data.put(new float[] { 1f, 1f, 0f, 1f, });

color_data.put(new float[] { 0f, 1f, 0f, 1f, });
color_data.put(new float[] { 1f, 1f, 0f, 1f, });
color_data.put(new float[] { 1f, 0f, 1f, 1f, });

color_data.flip();

vbo_vertex_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

vbo_color_handle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glBufferData(GL_ARRAY_BUFFER, color_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

/*
* Main Rendering Loop
*/

boolean running = true;

while (running)
{
running = (!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE));

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glPushMatrix();
{
glTranslatef(width / 2f, height / 2f, 0f);

glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
glVertexPointer(VERTEX_SIZE, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, vbo_color_handle);
glColorPointer(COLOR_SIZE, GL_FLOAT, 0, 0l);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glDrawArrays(GL_TRIANGLES, 0, vertices);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
glPopMatrix();

glFlush();

Display.sync(60);
Display.update();
}

/*
* Dispose Elements
*/

glDeleteBuffers(vbo_vertex_handle);
glDeleteBuffers(vbo_color_handle);

Display.destroy();

System.exit(0);
}
}

Slick2d using anti-aliasing with Animations/Images

This looks suspiciously like artifacts due to GL_POLYGON_SMOOTH.

When you use that (deprecated) functionality, you are expected to draw all opaque geometry with blending enabled and the blend function: GL_SRC_ALPHA_SATURATE, GL_ONE. Failure to do so produces white outlines on most contours (aliased edges, basically).

Chapter 6 of the OpenGL Redbook states:


Now you need to blend overlapping edges appropriately. First, turn off the depth buffer so that you have control over how overlapping pixels are drawn. Then set the blending factors to GL_SRC_ALPHA_SATURATE (source) and GL_ONE (destination). With this specialized blending function, the final color is the sum of the destination color and the scaled source color; the scale factor is the smaller of either the incoming source alpha value or one minus the destination alpha value.


This means that for a pixel with a large alpha value, successive incoming pixels have little effect on the final color because one minus the destination alpha is almost zero. With this method, a pixel on the edge of a polygon might be blended eventually with the colors from another polygon that's drawn later. Finally, you need to sort all the polygons in your scene so that they're ordered from front to back before drawing them.

That is a lot of work that almost nobody does correctly when they try to enable GL_POLYGON_SMOOTH.

Java Applet: no antialiased font in browser (but in AppletViewer)

It should work by overriding the paint method like this for each component where you want to have anti-aliasing:

static void activateAntiAliasing(Graphics g) {
try {
Graphics2D g2d = (Graphics2D)g;

// for antialiasing geometric shapes
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON );

// for antialiasing text
g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON );

// to go for quality over speed
g2d.setRenderingHint( RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY );
}
catch(ClassCastException ignored) {}
}

@Override public void paint(final Graphics g) {
activateAntiAliasing(g);
super.paint(g);
}

Can font rendering quality be affected by programming, or is it entirely handled by the OS?

If you are using Java 6, there is a system property called awt.useSystemAAFontSettings you can use to control anti-aliasing. Possible values are:

"lcd"
ClearType style sub-pixel anti-aliasing.

"false"
Disable anti-aliasing.

"on"
Best contrast.

"gasp"
Standar anti-aliasing.

Beautiful (Anti-alias) Chinese Character Display

Anti-aliasing considered harmful: http://www.joelonsoftware.com/articles/fog0000000041.html

The point is, that beauty of characters is not necessarily the user interface goal. It is not everything. What you should look for, is readability of text. When your Chinese characters look not smooth, it may be exactly what helps human eye's control loop to know that it is in focus and stop blaming the eye muscules for blurriness. Really, don't fall in this pitfal.



Related Topics



Leave a reply



Submit