Java:使用drawArc绘制圆形螺旋 [英] Java: Draw a circular spiral using drawArc

查看:318
本文介绍了Java:使用drawArc绘制圆形螺旋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行Java编程练习,我们必须使用drawArc方法绘制圆形螺旋,以便结果类似于以下内容:

I'm working on a java programming exercise where we have to draw a circular spiral using the drawArc method so that the result looks similar to this:

我已经为此工作了一段时间,这是我到目前为止的工作:

I've been working on this for a while and this is what I have so far:

import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class CircSpiral extends JPanel {
   public void paintComponent(Graphics g) {
      int x = 100;
      int y = 120;
      int width = 40;
      int height = 60;
      int startAngle = 20;
      int arcAngle = 80;
      for (int i = 0; i < 5; i++) {
         g.drawArc(x, y, width, height, startAngle, arcAngle);
         g.drawArc(x + 10, y + 10, width, height, startAngle + 10, arcAngle);
         x = x + 5;
         y = y + 5;
         startAngle = startAngle - 10;
         arcAngle = arcAngle + 10;
      }
   }

   public static void main(String[] args) {
      CircSpiral panel = new CircSpiral();
      JFrame application = new JFrame();
      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      application.add(panel);
      application.setSize(300, 300);
      application.setVisible(true);
   }
}

我的代码给出了以下结果:

My code gives me this result:

我知道问题出在在我的drawArc方法参数中,因为数字不正确,但是我不知道如何使数字循环。任何帮助表示赞赏。谢谢!

I know the problem lies in my arguments for the drawArc method because the numbers aren't right, but I don't know how to go about making the numbers go in a circular manner. Any help is appreciated. Thank you!

推荐答案

您的想法几乎是正确的。我做了一些修改。您需要反角绘制螺旋的另一侧,并使用固定点 startAngle

Your idea is almost right. I did some modifications. You need to inverse the angle to draw the other side of the spiral and use fixed point to startAngle.

import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class CircSpiral extends JPanel {

    public void paintComponent(Graphics g) {
        int x = getSize().width / 2 - 10;
        int y = getSize().height/ 2 - 10;
        int width = 20;
        int height = 20;
        int startAngle = 0;
        int arcAngle = 180;
        int depth = 10;
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                //   g.drawArc(x + 10, y + 10, width, height, startAngle + 10, -arcAngle);
                //  x = x - 5;
                y = y - depth;
                width = width + 2 * depth;
                height = height + 2 * depth;
                g.drawArc(x, y, width, height, startAngle, -arcAngle);
            } else {
                //  g.drawArc(x + 10, y + 10, width, height, startAngle + 10, arcAngle);
                x = x - 2 * depth;
                y = y - depth;
                width = width + 2 * depth;
                height = height + 2 * depth;
                g.drawArc(x, y, width, height, startAngle, arcAngle);
            }
        }
    }

    public static void main(String[] args) {
        CircSpiral panel = new CircSpiral();
        JFrame application = new JFrame();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);
        application.setSize(300, 300);
        application.setVisible(true);
    }
}

这篇关于Java:使用drawArc绘制圆形螺旋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆