组合圆弧和矩形时出现小间隙 [英] Small gaps appear when combining arcs and rectangles
问题描述
我正在尝试通过一系列小的角弧和矩形来模拟一个角.
I'm trying simulate a corner by a series of small angular arcs and rectangles.
当它在开启抗锯齿的情况下渲染时,我得到如下图:
When it renders with anti-aliasing turned on, I get a picture like below:
当我关闭抗锯齿时,我得到以下图片:
When I turn anti-aliasing off, I get the following picture:
看起来好像我的计算差了一两个像素,但我在所有渲染中都使用了双精度,所以我什至不认为是这种情况.
It almost looks as if my computations are off by a pixel or two, but I'm using doubles for all of the rendering, so I don't even think that is the case.
类的代码在这里:
这不是什么大问题,因为线条几乎不会分散注意力或计算的严重错误,但如果可能的话,我希望能够修复它.
It's not a huge issue with it, as the lines are hardly a distraction or serious error of calculation, but it is something that I would like to be able to fix, if at all possible.
谢谢
推荐答案
实际上问题可能出在只使用 fill(Shape):它只填充内部.也可以调用 draw(Shape) 来修复形状不对齐的问题.
actually the problem might be using only fill(Shape): it only fills the interior. Calling draw(Shape) as well might fix the Shapes not lining up.
你为什么不把弯道画成一个单一的形状?
Why aren't you painting the bends as a single shape?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestDonut extends JPanel {
public TestDonut() {
setPreferredSize(new Dimension(100, 100));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Ellipse2D.Double gap = new Ellipse2D.Double(25, 25, 50, 50);
Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, 100, 100);
Shape quarter = new Rectangle(50, 50, 50, 50);
Area donut = new Area(circle);
donut.subtract(new Area(gap));
Area quarterDonut = new Area(quarter);
quarterDonut.subtract(donut);
Area bend = new Area(quarter);
bend.subtract(quarterDonut);
g.setColor(Color.RED);
((Graphics2D) g).fill(bend);
Path2D.Double bend2 = new Path2D.Double();
bend2.moveTo(0, 50);
bend2.quadTo(0, 0, 50, 0);
bend2.lineTo(50, 25);
bend2.quadTo(25, 25, 25, 50);
bend2.closePath();
g.setColor(Color.GREEN);
((Graphics2D) g).fill(bend2);
Arc2D.Double outerArc = new Arc2D.Double(0, 0, 100, 100, 0, 90, Arc2D.PIE);
Arc2D.Double innerArc = new Arc2D.Double(25, 25, 50, 50, 0, 90, Arc2D.PIE);
Area bend3 = new Area(outerArc);
bend3.subtract(new Area(innerArc));
g.setColor(Color.BLUE);
((Graphics2D) g).fill(bend3);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(new TestDonut());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
这篇关于组合圆弧和矩形时出现小间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!