急如何用SWT将10个按钮排列成圆形 把按钮横排,竖排好实现。如果把按钮排列成圆呢。。没啥好思路。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 能给点实例代码吗?对SWT不是很熟..谢谢! import java.awt.geom.Point2D;public class GeometryUtil { // 绕原点的旋转矩阵,绕任意点旋转,可以先移动到原点,旋转,然后再移回去 // cosθ -sinθ 0 // sinθ +conθ 0 // 0000 +0000 1 // x = r*cosα, y = r*sinα // x' = r*cos(α+θ) = r*cosα*cosθ - r*sinα*sinθ = x*cosθ - y*sinθ // y' = r*sin(α+θ) = r*sinα*cosθ + r*cosα*sinθ = x*sinθ + y*cosθ // (x, y)绕圆心旋转degree度 public static Point2D rotate(double x, double y, double degree) { return rotate(x, y, 0, 0, degree); } public static Point2D rotate(Point2D p, double degree) { return rotate(p.getX(), p.getY(), 0, 0, degree); } public static Point2D rotate(Point2D p, Point2D op, double degree) { return rotate(p.getX(), p.getY(), op.getX(), op.getY(), degree); } // (x, y)绕(ox, oy)旋转degree度 public static Point2D rotate(double x, double y, double ox, double oy, double degree) { x -= ox; y -= oy; double cos = Math.cos(Math.toRadians(degree)); double sin = Math.sin(Math.toRadians(degree)); double temp = x * cos - y * sin; y = x * sin + y * cos; x = temp; return new Point2D.Double(x + ox, y + oy); }}import java.awt.geom.Point2D;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;@SuppressWarnings("serial")public class CircleButtons extends JPanel { public CircleButtons() { JButton[] buttons = new JButton[10]; int radius = 100; int cx = 150; int cy = 150; int deltaAngle = 360 / buttons.length; setLayout(null); for (int i = 0; i < buttons.length; ++i) { buttons[i] = new JButton("" + i); Point2D p = GeometryUtil.rotate(cx + radius, cy, cx, cy, i * deltaAngle); // 计算按钮在圆周上的坐标 buttons[i].setBounds((int)p.getX(), (int)p.getY(), 50, 50); add(buttons[i]); } } private static void createGuiAndShow() { JFrame frame = new JFrame(""); frame.getContentPane().add(new CircleButtons()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setAlwaysOnTop(true); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createGuiAndShow(); } }); }} 完全看不懂,数学没学好,什么sin cos 的,唉,看来还得弄明白算法 和 公式 还有我在我的eclipse上运行了一下,有个错误,就主方法里面的run()方法错误,为什么呢? java 空字符串 JAVA压缩对象列表而不是压缩文件哪怕临时的文件也不行。这个如何做呢。 遍历问题!请教各位高手! JAVA运行出错,提示找不到驱动程序,请大家帮我看下 如何将这个作成一个可执行文件? java 中中文输入问题 用jsp收mail(pop3协议)! java正则表达式-reluctant 量词 jb7中如何单独编译一个java,就象编译单独一个helloworld的文件成class? java中如何进行com串行口事件侦听,。。。。高分 有挑战的一道试题 关于java代码的执行顺序
// 绕原点的旋转矩阵,绕任意点旋转,可以先移动到原点,旋转,然后再移回去
// cosθ -sinθ 0
// sinθ +conθ 0
// 0000 +0000 1
// x = r*cosα, y = r*sinα
// x' = r*cos(α+θ) = r*cosα*cosθ - r*sinα*sinθ = x*cosθ - y*sinθ
// y' = r*sin(α+θ) = r*sinα*cosθ + r*cosα*sinθ = x*sinθ + y*cosθ
// (x, y)绕圆心旋转degree度
public static Point2D rotate(double x, double y, double degree) {
return rotate(x, y, 0, 0, degree);
} public static Point2D rotate(Point2D p, double degree) {
return rotate(p.getX(), p.getY(), 0, 0, degree);
} public static Point2D rotate(Point2D p, Point2D op, double degree) {
return rotate(p.getX(), p.getY(), op.getX(), op.getY(), degree);
} // (x, y)绕(ox, oy)旋转degree度
public static Point2D rotate(double x, double y, double ox, double oy, double degree) {
x -= ox;
y -= oy; double cos = Math.cos(Math.toRadians(degree));
double sin = Math.sin(Math.toRadians(degree)); double temp = x * cos - y * sin;
y = x * sin + y * cos;
x = temp; return new Point2D.Double(x + ox, y + oy);
}
}
import java.awt.geom.Point2D;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;@SuppressWarnings("serial")
public class CircleButtons extends JPanel {
public CircleButtons() {
JButton[] buttons = new JButton[10];
int radius = 100;
int cx = 150;
int cy = 150;
int deltaAngle = 360 / buttons.length;
setLayout(null);
for (int i = 0; i < buttons.length; ++i) {
buttons[i] = new JButton("" + i);
Point2D p = GeometryUtil.rotate(cx + radius, cy, cx, cy, i * deltaAngle); // 计算按钮在圆周上的坐标
buttons[i].setBounds((int)p.getX(), (int)p.getY(), 50, 50);
add(buttons[i]);
}
} private static void createGuiAndShow() {
JFrame frame = new JFrame("");
frame.getContentPane().add(new CircleButtons()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createGuiAndShow();
}
});
}
}