Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用单位圆三角在Java中绘制环形_Java_Loops_Graphics_Awt_Trigonometry - Fatal编程技术网

如何使用单位圆三角在Java中绘制环形

如何使用单位圆三角在Java中绘制环形,java,loops,graphics,awt,trigonometry,Java,Loops,Graphics,Awt,Trigonometry,我必须用Java中的线条(drawLine)画一个环,看起来应该像附件中的图片。我们提供了可以找到的类DrawingPanel 我用直线画了一个规则的圆圈,但我不确定如何得到圆环的形状。我是编程新手,这是我的第一篇博文,如果我错过了一些重要的东西,请道歉 这是我目前的代码: public static int panelSize = 400; public static void drawCircle() { double radius = 200; int x2

我必须用Java中的线条(
drawLine
)画一个环,看起来应该像附件中的图片。我们提供了可以找到的类
DrawingPanel

我用直线画了一个规则的圆圈,但我不确定如何得到圆环的形状。我是编程新手,这是我的第一篇博文,如果我错过了一些重要的东西,请道歉

这是我目前的代码:

public static int panelSize = 400;
    public static void drawCircle()
    {
    double radius = 200;
    int x2 = 200;
    int y2 = 200;

    DrawingPanel dp = new DrawingPanel(panelSize, panelSize);
    dp.setBackground(Color.CYAN);

    Graphics dpGraphics = dp.getGraphics(); 
    dpGraphics.setColor(Color.RED);

    for (int circle = 0; circle <= 360; circle++)
    {
        int x = (int)(x2 + Math.sin(circle * (Math.PI / 180)) * radius);
        int y = (int)(y2 + Math.cos (circle * (Math.PI / 180)) * radius);

        dpGraphics.drawLine(x, y, x2, y2);
    }
}
publicstaticintpanelsize=400;
公共静态无效drawCircle()
{
双半径=200;
int x2=200;
int y2=200;
DrawingPanel dp=新的DrawingPanel(面板尺寸,面板尺寸);
dp.立根背景(颜色为青色);
Graphics dpGraphics=dp.getGraphics();
dpGraphics.setColor(Color.RED);

对于(int circle=0;circle这样的图形可以通过从一个点到圆上更远的点画一条线,经过起点几次来绘制

这就是我想到的:

// Radius
int radius = 200;
// center of the circle
int centerX = 300, centerY = 300;

// The number of edges. Set to 5 for a pentagram
int mod = 136;
// The number of "points" to skip - set to 2 for a pentagram
int skip = 45;

// Precalculated multipier for sin/cos
double multi = skip * 2.0 * Math.PI / mod; 

// First point, calculated by hand
int x1 = centerX; // sin(0) = 0
int y1 = centerY + radius; // cos(0) == 1

for (int circle = 1; circle <= mod; circle++)
{
    // Calculate the end point of the line.
    int x2 = (int) (centerX + radius * Math.sin(circle * multi));
    int y2 = (int) (centerY + radius * Math.cos(circle * multi));
    dpGraphics.drawLine(x1, y1, x2, y2);
    // Next start point for the line is the current end point
    x1 = x2;
    y1 = y2;
}
//半径
int半径=200;
//圆心
int centerX=300,centerY=300;
//边数。五角星形设置为5
int mod=136;
//要跳过的“点数”-五角星形设置为2
int skip=45;
//用于正弦/余弦的预计算多载波
双重多重=跳过*2.0*Math.PI/mod;
//第一点,手工计算
int x1=centerX;//sin(0)=0
int y1=中心+半径;//cos(0)==1

对于(int circle=1;circle谢谢你,我不能超过你,因为我还没有15个声誉,但我真的很感谢你的帮助。