How to make straight the edge of the line drawn with Graphics in Java?
I made a program that draws lines on the screen, but the line is very deformed (image 2) in certain links. I wanted to know if it is possible to leave straight (Image 1) the edge of the line in whatever its slope.
I'm using Graphics2D
.
3
Author: Victor Stafusa, 2018-03-29
1 answers
Use a 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 is KEY_ANTIALIASING
and VALUE_ANTIALIAS_ON
which activates anti-aliasing.
For more details, see the CBT I did in this area in 2007.
2
Author: Victor Stafusa, 2018-03-29 16:54:25