在自定义控件的皮肤中,我想绘制一个与控件大小相同的三角形,并让三角形随着框架大小的调整而增长。我有以下代码,但当我调整框架大小时,边界只会增加。如何让它正确调整大小?
private void update()
{
Bounds bounds = node.getBoundsInParent();
Path path = new Path();
path.getElements().add(
new MoveTo(
bounds.getWidth() / 2 + bounds.getMinX(),
bounds.getMinY()));
path.getElements().add(
new LineTo(bounds.getMaxX(), bounds.getMaxY()));
path.getElements().add(
new LineTo(bounds.getMinX(), bounds.getMaxY()));
path.setFill(Color.RED);
node.getChildren().setAll(path);
}
编辑:使用摇摆我会做以下事情。但我无法让它在 JavaFX 中工作。
public class Arrow extends JPanel
{
@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Dimension size = getSize();
Point top = new Point(size.width/2,0);
Point bottomRight = new Point(size.width, size.height);
Point bottomLeft = new Point(0, size.height);
GeneralPath path = new GeneralPath();
path.moveTo(top.x, top.y);
path.lineTo(bottomRight.x, bottomRight.y);
path.lineTo(bottomLeft.x, bottomLeft.y);
path.lineTo(top.x, top.y);
Graphics2D g2d = (Graphics2D)graphics.create();
g2d.setColor(Color.RED);
g2d.fill(path);
g2d.dispose();
}
}