如何在Wheel游戏中正确放置球的位置?

时间:2019-05-26 08:21:24

标签: java swing

我在轮盘游戏中有一个固定的插槽数。从数字00到数字1(顺时针)开始。在我的Collection数组中,头寸计数为0到37。目前,我已经成功地将球的位置放在1度的数字中间。但是当我尝试传递数字位置时,我无法为每个位置计算正确的值。

我已经尝试弄乱计算结果了,它是球位于数字中间还是超出范围!

    public class WheelDisplay extends JPanel implements ConstantVariable {

    private Image image;
    private ImageObserver imageObserver;
    private float degrees = 1;
    private int post = 0;

    public WheelDisplay() {
        ImageIcon icon = new ImageIcon(IMAGE_LOCATION);
        image = icon.getImage();
        imageObserver = icon.getImageObserver();
    }

    /** Credit to stackoverflow forum : https://stackoverflow.com/questions/25923480/simple-circle-rotation-simulate-motion **/
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        int circleDiameter = Math.min(getWidth(), getHeight());
        double circleRadius = circleDiameter / 2;
        int offSetX = (getWidth() - circleDiameter) / 2;
        int offSetY = (getHeight() - circleDiameter) / 2;

        g2d.drawImage(image, offSetX, offSetY, circleDiameter, circleDiameter, imageObserver);

        g2d.setColor(Color.BLUE);
        int ballDiameter = (int) (circleDiameter * 0.02);
        int ballRadius =  ballDiameter / 2;

        Point p = getPointOnCircle(this.degrees, circleRadius * 0.9, circleRadius);

        int valueX = offSetX + p.x - ballRadius;
        int valueY = offSetY + p.y - ballRadius;
        g2d.fillOval(valueX, valueY, ballDiameter, ballDiameter);

        g2d.dispose();
    }

    private Point getPointOnCircle(float degress, double circleRadius, double innerCircleRadius) {

        //The calculation that allow ball sit in the middle of each number when spin
        double rads =  ((Math.PI  * degress) / 38);

        // Calculate the outter point of the line
        int xCordinate = Math.round((float) (innerCircleRadius + Math.cos(rads) * circleRadius));
        int yCordinate = Math.round((float) (innerCircleRadius + Math.sin(rads) * circleRadius));

        return new Point(xCordinate, yCordinate);
    }

    public void setDegree(int x) {
        this.degrees += 2;
        this.post = x; // The number of position coming from Collection (0-37)
    }
}

球应该位于轮盘图像中每个38个数字的中间。

希望有人可以指出我做错了哪一部分!

我正在使用的图像是这个。

My wheel Image without passing in number position

0 个答案:

没有答案