How to get the edge of the line drawn with Graphics in Java straight?

Asked

Viewed 45 times

3

I made a program that traces lines on the screen, but the line is very deformed (image 2) in certain links. I wanted to know if there is a way to leave straight (image 1) the edge of the line in whatever its inclination.

I’m using Graphics2D.

imagens

1 answer

2

Use the class RenderingHints:

private static final RenderingHints HINTS = new RenderingHints(null);
static {
    HINTS.put(KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY);
    HINTS.put(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON); 
    HINTS.put(KEY_COLOR_RENDERING, VALUE_COLOR_RENDER_QUALITY); 
    HINTS.put(KEY_INTERPOLATION, VALUE_INTERPOLATION_BICUBIC); 
    HINTS.put(KEY_STROKE_CONTROL, VALUE_STROKE_NORMALIZE);
}

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHints(HINTS);

    // Prossiga com seu desenho aqui.
}

All these keys and values are added with import static java.awt.RenderingHints.*. The most important thing is KEY_ANTIALIASING and the VALUE_ANTIALIAS_ON that activates anti-aliasing.

For more details, see the TCC I did in this area in 2007.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.