Java2D:如何将图像放置在具有正确旋转的中心周围

问题描述 投票:0回答:1

enter image description here

我无法以正确的旋转角度在中心周围放置图像。这是我正在使用的代码,可以在屏幕截图中看到输出。切片的数量可以由用户动态更改,因此我需要根据切片的数量计算新的旋转值,但我无法找到正确的方法。我可以随意为pictureAngle添加一个值来调整旋转,但它只适用于这个特定数量的切片。

注意:要绘制的第一张图片是右侧红色切片上的图片,在上方的屏幕截图中显示为直线。

double angle = (double) 360 / this.slices.length;

for (int i = 0; i < this.slices.length; i++) {

    double pictureAngle = Math.toRadians(i * (360 / this.slices.length));

    px[i] = this.centerX + (wheelSize/2.1) * Math.cos(Math.toRadians((i * angle) + angle / 2));
    py[i] = this.centerY + (wheelSize/2.1) * Math.sin(Math.toRadians((i * angle) + angle / 2));

    p.addPoint((int)px[i], (int)py[i]);

    AffineTransform backup1 = g2d.getTransform();
    AffineTransform trans = new AffineTransform();

    trans.rotate(pictureAngle, (int)px[i], (int)py[i]);

    g2d.transform( trans );

    g2d.drawImage(pictures[i], (int)px[i] - (pictures[i].getWidth() / 2), (int)py[i], null);

    g2d.setTransform( backup1 );

    g2d.drawLine((int)this.centerX, (int)this.centerY, (int)px[i], (int)py[i]);


}

如果我任意添加103度旋转它几乎可以工作,所以我想我需要一个正确的方法来根据切片的数量得到这个数字:

double pictureAngle = Math.toRadians(103 + i * (360 / this.slices.length));

enter image description here

java rotation affinetransform
1个回答
0
投票
        double angle = (double) 360 / nOfSlices;
        double startingAngle = 90 - angle / 2;

        for (int i = 0; i < nOfSlices; i++) {

            // this is the old code

            //rc2D arc = new Arc2D.Double(0, 0, wheelSize, wheelSize,
            //        i * angle,
            //        angle,
            //        Arc2D.PIE);

            Arc2D arc = new Arc2D.Double(0, 0, wheelSize, wheelSize,
                    (i * angle) - startingAngle,
                    angle,
                    Arc2D.PIE);

解决方案是在中间绘制第一个切片(我用黑色切片突出显示它 - 图片在下面)并将第一张图片放入其中0转,然后根据切片角度逐渐旋转图片。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.