Java:使用drawArc绘制一个圆形螺旋
发布时间:2020-12-15 05:12:44 所属栏目:Java 来源:网络整理
导读:我正在进行 java编程练习,我们必须使用drawArc方法绘制一个圆形螺旋,以便结果看起来类似于: 我一直在研究这个问题,这是我到目前为止所做的: import java.awt.Graphics;import javax.swing.JPanel;import javax.swing.JFrame;public class CircSpiral exten
我正在进行
java编程练习,我们必须使用drawArc方法绘制一个圆形螺旋,以便结果看起来类似于:
我一直在研究这个问题,这是我到目前为止所做的: 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,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); } } 我的代码给了我这个结果: 我知道问题在于我对drawArc方法的参数,因为数字不正确,但我不知道如何使数字以循环方式进行.任何帮助表示赞赏.谢谢! 解决方法
你的想法几乎是对的.我做了一些修改.您需要反转角度以绘制螺旋的另一侧并使用固定点来启动角度.
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,-arcAngle); // x = x - 5; y = y - depth; width = width + 2 * depth; height = height + 2 * depth; g.drawArc(x,-arcAngle); } else { // g.drawArc(x + 10,arcAngle); x = x - 2 * depth; y = y - depth; width = width + 2 * depth; height = height + 2 * depth; g.drawArc(x,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); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |