日期:2014-05-20 浏览次数:20914 次
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Test74 extends JPanel {
Ellipse2D.Double e = new Ellipse2D.Double(210, 100, 30, 30);
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.red);
g2.fill(e);
Point2D.Double pc = new Point2D.Double(210, 300);
Point2D.Double p = new Point2D.Double(210 + 15, 100 + 15);
for(int i = 1; i<8; i++) {
p = getPoint(pc, p, Math.PI/4);
g2.fill(new Ellipse2D.Double(p.x - 15, p.y - 15, 30, 30));
}
}
//得到p围绕pc旋转r(弧度)后的点
public Point2D.Double getPoint(Point2D.Double pc, Point2D.Double p,
double r) {
double a = Math.atan2(pc.y - p.y, p.x - pc.x);
double raduis = pc.distance(p);
Point2D.Double result = new Point2D.Double();
result.x = pc.x + raduis * Math.cos(a + r);
result.y = pc.y - raduis * Math.sin(a + r);
return result;
}
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Test74());
frame.setSize(500, 600);
frame.setVisible(true);
}
}