import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class c1 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);
for(int i=1;i<=7;i++)
{
g2.setPaint(Color.blue);
g2.rotate((45*Math.PI)/180,210,300);
g2.fill(e);
}
}
public static void main(String args[])
{
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new c1());
frame.pack();
frame.setVisible(true);
}
}
在上面的程序中如何得到旋转后每个圆的圆心坐标?
------解决方案--------------------
换了种方式实现了你上面的功能
其中getPoint(Point2D.Double pc, Point2D.Double p, double r)这个方法
就是你要
- Java code
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); }}