如何将数字加到1到12,我只是创建一个圆圈

时间:2019-03-26 18:22:01

标签: java

import java.awt.Color;

import acm.graphics.GOval;
import acm.program.GraphicsProgram;

public class Clock extends GraphicsProgram {
    private static final long serialVersionUID = 1L;

    public void run() {
        GOval tofig = createFilledCircle(getWidth()/2, getHeight()/2, 200, Color.black);
        add(tofig);
        GOval lala = createFilledCircle(getWidth()/2, getHeight()/2, 180, Color.white);
        add(lala);
    }


    private GOval createFilledCircle(double x, double y, double r, Color color) {
        GOval circle = new GOval(x - r, y - r, 2 * r, 2 * r);
        circle.setFilled(true);
        circle.setColor(color);
        return circle;
    }

    // Ignore this;
    public static void main(String[] args) {
        new Clock().start();
    }

}

3 个答案:

答案 0 :(得分:1)

这是您正在尝试做的三角部分的代码,您和我已经一起努力过:

public class DrawCircle {

    static final double twopi = Math.PI * 2;
    static final double fudge = 0.000001;

    private static void drawHourLabels(double center_x, double center_y, double radius) {

        int steps = 12;
        for (double angle = 0.0; angle < (twopi - fudge); angle += twopi/steps) {
            double x_offset = Math.sin(angle) * radius;
            double y_offset = Math.cos(angle) * radius;
            double x = center_x + x_offset;
            double y = center_y + y_offset;

            // Here you'd do the actual drawing of each hour label at the coordinates x,y.  We'll just print them for now.
            System.out.println(String.format("%f %f", x, y));
        }
    }

    public static void main(String... args) {
//        drawHourLabels(getWidth()/2, getHeight()/2, 220); // <-- you'd do something like this in your "run" method.

        // Draw clock labels around circle with center at 400x200 of radius 220
        drawHourLabels(400, 600, 220);
    }
}

之所以使用'fudge'值,是因为浮点算术不是十分精确。到我们将12个浮点值加起来达到2 * Math.PI时,我们可能有点过高或不足。我们希望确保在循环结束时不再处理12:00位置,因为我们计算出的值略小于2 *Math.PI。因此,我们添加了一个很小的“忽悠系数”,但可以保证它大于我们积累的任何浮点误差。

答案 1 :(得分:0)

看看我的这个简单例子。

/**
 * Draw text elements from 1 to 12 inside a circle
 * @return a group of text elements from 1 to 12
 */
public strictfp static Group drawText() {
    //a list for storing text numbers
    List<Text> numbersInClock = new ArrayList<>();
    //create a group of shapes
    Group group = new Group();
    //set it to x,y positions
    group.setLayoutX(150);
    group.setLayoutY(150);
    //numbers corresponding to angle calculations
    int[] numbers = {3,4,5,6,7,8,9,10,11,12,1,2};
    double[] angles = {0, 0.166666666667, 0.333333333334,0.5, 0.666666666667, 0.833333333334, 1, 1.166666666667, 1.33333333334, 1.5, 1.666666666667, 1.83333333334};
    int i = 0;
    for (double angle : angles) {    
        //Calculated formula for x,y positions of each number within a circle
        //(x,y) = (rcos(angle),rsin(angle))  
        int r = 90; // length of radius, a bit shorter to put it inside a circle
        //calculate x and y positions based on formula for numbers within a circle
        double x = r * Math.cos(angle*Math.PI);
        double y = r * Math.sin(angle*Math.PI);
        //create a text element consiting a coressponding number
        Text text = new Text(x, y, String.valueOf(numbers[i++]));
        //add it to a list
        numbersInClock.add(text);
    }
    //add all text elements to a group
    group.getChildren().addAll(numbersInClock);
    //return a group
    return group;

}

可以从此处获取完整代码:https://github.com/MomirSarac/JavaFX-Circle-Clock-Image

答案 2 :(得分:-1)

您可以使用sin和cos知道在哪里设置文本。 Sine和Cosine是两个数学运算符,它们以弧度取一个角度,并给出介于-1和1之间的数字,以知道如何乘以获取坐标

you can learn more here