0

我是java新手,这是第一次使用方法。我一直在尝试创建一个 Epitrochoid,但它只输出一条直线。我的方法有问题吗?如果是这样,我正在寻求解决问题的任何提示/提示。

我已经在下面发布了相关代码。

  public class myEpitrochoid{

        public myEpitrochoid( ) {

            double a = 50;
            double b = 5;
            double k = 10;
            int num = 100;

            drawEpitrochoid( a, b, k, num);

        }

        private void drawEpitrochoid (double a, double b, double k, int num) {

          for ( int t=1 ; t<=num ; t++ ) {

            t = t/num;
            double x = (a+b)*cos(2*PI*t) - k * cos(2*PI*(a+b)*(t/b));
            double y = (a+b)*sin(2*PI*t) - k * sin(2*PI*(a+b)*(t/b));
            yertle.moveTo(x,y);

          }   

        }

}
4

1 回答 1

1

这是一个工作示例

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.beans.Transient;

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


@SuppressWarnings("serial")
public class EpitrochoidDemo extends JPanel {

    private Path2D.Double points = new Path2D.Double();
    private double t = 0.0;
    private double a = 50;
    private double b = 5;
    private double k = 1.7;
    private Ellipse2D.Double stationaryCircle;
    protected double num = 100.0;

    public EpitrochoidDemo() {
        setBackground(Color.black);
        stationaryCircle = new Ellipse2D.Double(400 - a, 400 - a,
                2 * a, 2 * a);
        nextPoint();

    }


    public void nextPoint() {
        double x = (a + b) * Math.cos(2 * Math.PI * t) - k
                * Math.cos(2 * Math.PI * (a + b) * (t / b));
        double y = (a + b) * Math.sin(2 * Math.PI * t) - k
                * Math.sin(2 * Math.PI * (a + b) * (t / b));
        x += 400;
        y += 400;

        // The Path2D needs an initial point
        if (t == 0.0)
            points.moveTo(x, y);
        // Draw a line from previous point to this point
        else
            points.lineTo(x, y);
        t += 0.001;
    }

    @Override
    @Transient
    public Dimension getPreferredSize() {
        return new Dimension(800, 800);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        // Pretty graphics
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        // Draw axis and stationary circle and timer in corner
        g2d.setColor(Color.white);
        g2d.drawString("t=" + t, 10, 10);
        g2d.draw(new Line2D.Double(0, 400, 800, 400));
        g2d.draw(new Line2D.Double(400, 0, 400, 800));
        g2d.draw(stationaryCircle);
        g2d.setColor(Color.red);
        g2d.draw(points);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        final EpitrochoidDemo e = new EpitrochoidDemo();

        frame.getContentPane().add(e);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        final Timer timer = new Timer(15, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                e.nextPoint();
                e.repaint();

                if (e.t >= e.num) {
                    ((Timer) arg0.getSource()).stop();
                }

            }
        });
        timer.start();
    }
}

我正在使用与您相同的公式(也在此处发布)并且效果很好,如图所示 在此处输入图像描述

于 2013-10-28T01:48:01.750 回答