这是一个工作示例
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();
}
}
我正在使用与您相同的公式(也在此处发布)并且效果很好,如图所示