简体   繁体   English

如何在javaFX中定位Polygon

[英]How locate Polygon in javaFX

I try to design a custom button in javafx but I have some problem to locate a polygon : 我尝试在javafx中设计一个自定义按钮,但是在定位多边形时遇到了一些问题:

public class PlayButton extends Group{
public static final int PLAY = 0, PAUSE = 1;

private int state = PAUSE;
private Circle background;
private Polygon triangle;

public PlayButton(){
    background = new Circle(20);
    background.setStroke(MyApp.FIRST_COLOR);
    background.setStrokeWidth(2);
    background.setEffect(MyApp.DROP_SHADOW);
    background.setCursor(Cursor.HAND);
    background.setFill(MyApp.SECOND_COLOR_OPAQUE);
    this.getChildren().add(background);

    triangle = new Polygon();
    triangle.getPoints().addAll(new Double[]{10.0, 10.0, 40.0, 25.0, 10.0, 40.0});
    triangle.setFill(MyApp.FIRST_COLOR);
    this.getChildren().add(triangle);
}

public void setState(int state){
    this.state = state;
}
}

My polygon is outside of my background, but I don't understand why : 我的多边形不在背景中,但我不明白为什么:

我的按钮

The rectangle is a progress bar, no problem with him. 矩形是一个进度条,他没问题。
Thank you for your help 谢谢您的帮助

You're using absolute coordinates for the triangle, but you should probably use coordinates relative to the circle if you want them to always overlap. 您正在使用三角形的绝对坐标,但如果希望它们始终重叠,则可能应该使用相对于圆的坐标。 To get the top left of the enclosing rectangle of the circle I'd do: 为了得到圆的包围矩形的左上角,我要做的是:

float[] backgroundTopLeft = new float[] {
    background.getCenterX() - background.getRadius(),
    background.getCenterY() - background.getRadius()};

Then draw the triangle relative to that coordinate as the top left of the triangle. 然后相对于该坐标绘制三角形作为三角形的左上角。

After some test I think position of polygon's points are calculated from the center of my group, the radius of my circle in background is 20 then coordinate start at -20 and end at 20, then good coordinates are : 经过一些测试,我认为多边形的点的位置是从我组的中心计算出来的,背景中我的圆的半径是20,然后坐标从-20开始并在20结束,那么好的坐标是:

triangle.getPoints().addAll(new Double[]{-5.0, -10.0, 12.0, 0.0, -5.0, 10.0});

then it's look like that : 然后看起来像这样:
纽扣

Someone can tell me if I'm wrong or right ? 有人可以告诉我我是对还是错?
Else I hope this could help someone. 否则我希望这可以对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM