我对java中的图形很陌生,我正在尝试创建一个夹在另一个形状底部的形状。这是我想要实现的目标的示例:
形状底部的白线是在圆形边缘内剪裁的。 我目前的做法是这样的:
g2.setColor(gray);
Shape shape = getShape(); //round rectangle
g2.fill(shape);
Rectangle rect = new Rectangle(shape.getBounds().x, shape.getBounds().y, width, height - 3);
Area area = new Area(shape);
area.subtract(new Area(rect));
g2.setColor(white);
g2.fill(area);
我仍在尝试剪辑方法,但我似乎无法做到正确。当前的方法是否可以(性能方面,因为组件经常重新绘制)或者是否有更有效的方法?
最佳答案
我认为您关于使用剪辑方法的最初想法是正确的方法。这对我有用:
static void drawShapes(Graphics2D g, int width, int height,
Shape clipShape) {
g.setPaint(Color.BLACK);
g.fillRect(0, 0, width, height);
g.clip(clipShape);
int centerX = width / 2;
g.setPaint(new GradientPaint(
centerX, 0, Color.WHITE,
centerX, height, new Color(255, 204, 0)));
g.fillRect(0, 0, width, height);
g.setPaint(Color.WHITE);
int whiteRectHeight = height * 4 / 5;
g.fillRect(0, whiteRectHeight,
width, height - whiteRectHeight);
}
关于java - 2D 裁剪区域以形成形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19612294/