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:

SSHS3.jpg

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:

KGVsR.jpg

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!

解决方案

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值