我正在绘制带有径向渐变的小圆圈。示例结果(r
= 10 像素):
但是当我减小半径时,奇怪的事情开始发生。如果r
= 5px:
缩放以匹配 10px 图像:
如果 r = 2px(再次缩放),情况会变得更糟:
如您所见,渐变的中心始终偏离圆心(向下和向右)。
我用来生成上述图像的代码(SSCCE,可编译和可运行):
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.*;
import java.awt.geom.*;
public class Test {
public static void main(String[] args) throws Exception {
float r = 2;
BufferedImage img = new BufferedImage((int) r*4, (int) r*4, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = img.createGraphics();
RenderingHints rh = new RenderingHints(null);
rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
rh.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.addRenderingHints(rh);
g.setBackground(Color.WHITE);
g.clearRect(0, 0, (int) r*4, (int) r*4);
g.setPaint(new RadialGradientPaint(
r*2,r*2,r*2,
new float[] {0.1f,0.35f},
new Color[] {Color.RED, Color.GREEN}
));
g.fill(new Ellipse2D.Float(r,r,r*2,r*2));
g.dispose();
ImageIO.write(img, "png", new File("out.png"));
}
}
我尝试切换渲染提示,但结果没有改变。什么可能导致这个问题?
编辑:
0.5f
通过左上角偏移 RadialGradientPaint 得到正确的图像 (r=2):
“固定”代码:
g.setPaint(new RadialGradientPaint(
r*2-0.5f, r*2-0.5f, r*2,
new float[] {0.1f,0.35f},
new Color[] {Color.RED, Color.GREEN}
));