Java:使用drawArc绘制一个圆形螺旋

时间:2015-04-14 22:51:46

标签: java swing graphics draw

我正在进行java编程练习,我们必须使用drawArc方法绘制一个圆形螺旋,以便结果看起来类似于: enter image description here

我一直在研究这个问题,这是我到目前为止所做的:

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

我的代码给了我这个结果: enter image description here

我知道问题在于我对drawArc方法的参数,因为数字不正确,但我不知道如何使数字以循环方式进行。任何帮助表示赞赏。谢谢!

6 个答案:

答案 0 :(得分:6)

你的想法几乎是正确的。我做了一些修改。您需要反转角度以绘制螺旋的另一侧,并使用固定点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);
    }
}

答案 1 :(得分:5)

如果这是我的项目,是的,我会在循环中绘制弧形,但在循环内,我会尝试使弧形的边界框更大但仍然居中于同一位置。要做到这一点,我会将x和y递减一些常量,比如DELTA(我将其设置为== 1),并且我将宽度和高度增加2 * DELTA 。我也会保持我的arcAngle不变,而是在循环中改变我的startAngle:startAngle = startAngle - arcAngle;

例如,这个:

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class CircSpiral extends JPanel {
   private static final int DELTA = 1;
   private static final int ARC_ANGLE = 20;
   private static final int PREF_W = 300;
   private static final int PREF_H = PREF_W;
   private static final int LOOP_MAX = 400;

   public void paintComponent(Graphics g) {
      int x = PREF_W / 2;
      int y = PREF_H / 2;
      int width = 1;
      int height = 1;
      int startAngle = 0;
      int arcAngle = ARC_ANGLE;
      for (int i = 0; i < LOOP_MAX; i++) {
         g.drawArc(x, y, width, height, startAngle, arcAngle);
         x = x - DELTA;
         y = y - DELTA;
         width += 2 * DELTA;
         height += 2 * DELTA;
         startAngle = startAngle - arcAngle;
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

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

会导致:

enter image description here

答案 2 :(得分:2)

以下代码将输出此图像:

enter image description here

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

public class CircSpiral extends JPanel {
    public void paintComponent(Graphics g) {
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        int numIterations = 5;

        int arcWidth = 10;
        int arcGrowDelta = 30;

        for (int i = 0; i < numIterations; i++) {
            g.drawArc(centerX - arcWidth, centerY - arcWidth, 2 * arcWidth, 2 * arcWidth, 0, 180);
            arcWidth += arcGrowDelta;
            g.drawArc(centerX - arcWidth, centerY - arcWidth, 2 * arcWidth - arcGrowDelta, 2 * arcWidth, 180, 180);
        }
    }

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

这个想法非常简单,只需绘制一个圆圈的上半部分,就像这样:

enter image description here

然后将弧大小增加一个常数因子并绘制圆的下半部分但是使该圆的终点与上圆相匹配,因为它只是从底部圆宽度arcGrowDelta的基底:

enter image description here

重复一遍。

答案 3 :(得分:0)

这是我的解决方案:

package mainpack;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JPanel;

public class SpiralPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D)g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        int width = 10;
        int height = 10;
        int startAngle = 0;
        int arcAngle = 180;
        int x = (getWidth() - width) / 2;
        int y = (getHeight() - height) / 2;

        int i = 0;
        int t = 0;

        while (i < 36) {

            g2d.drawArc(x + t, y, width, height, startAngle, arcAngle);

            if (i % 2 == 0) {
                t -= 10;
            }
            y -= 5;
            width += 10;
            height += 10;
            startAngle += 180;

            i++;
        }
    }
}

答案 4 :(得分:0)

我是java的初学者,最后管理如何创建螺旋。

这是我的代码:

int lineLength = 20; //starting line length
int x = getWidth() / 2; //start drawing from center of JPanel
int y = getHeight() / 2; //start drawing from center of JPanel
for( int counter = 0; counter < 10; counter++ )
{
    g.drawArc( x, y, lineLength, lineLength, 0, 180 ); //draws top semicircle of equal width and height
    lineLength += 20; //increases arc diameter
    x -= 20; //moves x coordinate left

    g.drawArc( x, y - 10, lineLength, lineLength, 0, -180 ); //draws bottom semicircle; 'y - 10' joins the 2 semicircles
    lineLength += 20; //increases arc diameter
    y -= 20; //moves y coordinate up
}

答案 5 :(得分:0)

如果你愿意让一些好的旧三角学做这项工作,你可以使用它:

import java.awt. *;
import javax.swing. *;
import java.math.*;

public class Spiral extends JFrame {

public Spiral()
{
    // Set Window
    setTitle("Spirale");
    setSize(1500, 1500);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setVisible(true);
}

public void paint(Graphics g)
{
    super.paint(g);
    for(double i = 1; i < 50000; i++)
    {
        int locY = 600 - (int) (Math.cos((Math.PI*i)/1800)*i/50);
        int locX = 600 - (int) (Math.sin((Math.PI*i)/1800)*i/50);
        g.drawLine(locX, locY, locX, locY);
    }
}

public static void main(String[] args) {
    new Spiral();
}

}