无法获得Gui Java Clock的正确点

时间:2011-01-20 23:09:11

标签: java clock

我的数学有问题,说实话,我不确定我在做什么,就算数而言。

下面是适用的代码:

seconds = (cal.get(Calendar.SECOND)+(cal.get(Calendar.MILLISECOND)/1000.0))-15;
                minutes = cal.get(Calendar.MINUTE)+(seconds/60);
  hours = ((cal.get(Calendar.HOUR))+(minutes/60))/12;
                //draw sec hand
                g2.drawLine(cx, cy, cx+(int)(Math.cos((Math.PI/30)*(seconds))*(r/2-25)), cy+(int)(Math.sin((Math.PI/30)*seconds)*(r/2-25)));
                //draw min hand
                g2.drawLine(cx, cy, cx+(int)(Math.cos((Math.PI/30)*(minutes)-(Math.PI/2))*(r/2-70)), cy+(int)(Math.sin((Math.PI/30)*minutes-(Math.PI/2))*(r/2-70)));
                //draw hour hand
                g2.drawLine(cx, cy, cx+(int)(Math.cos((Math.PI/12)*(hours))*(r/2-120)), cy+(int)(Math.sin((Math.PI/12)*hours)*(r/2-120)));

1 个答案:

答案 0 :(得分:0)

这是数学(假设您从日历中显示小时,分钟和秒)

// hour
theta = 90 - 30*hour - minute/2;

//minute
theta = 90 - 6*minute - second/10;

//second
theta = 90 - 6*second;

以度为单位给出角度,乘以Math.PI / 180转换为弧度。手的终点((r * cos(theta), - r * sin(theta))来自(centerX,centerY)。问候, - M.S。