I wrote a program using some custom rendering and had to display a rectangle with a frame. I decided to just call graphics2D.fillRect (), switch to the border color and call graphics2D.drawRect (). However, despite the fact that these calls are repeated with the same coordinates and sizes, fillRect () does not always fill the entire area contained in drawRect when the color I draw is translucent (has alpha). In addition, the area colored by fillRect () is sometimes outside the area containing drawRect (). Why do these two methods paint things in different places with different colors?
Here is an example of a problem demonstration. A mouse click in the window will switch between drawing an alpha fill and without. Note that there is a white line at the bottom of the rectangle when drawing with alpha, but this line of pixels is missing when drawing without alpha.
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.AffineTransform; import javax.swing.JFrame; import javax.swing.JPanel; public class ColorWithAlpha extends JPanel { private boolean hasAlpha = true; private static final long serialVersionUID = 1L; public static void main(String[] args) {
source share