开发者

Can't get the right point for Gui Java Clock

开发者 https://www.devze.com 2023-02-06 09:22 出处:网络
T开发者_如何学运维here is something wrong with my math and to be honest I\'m not exactly sure what I\'m doing as far as the math goes.

T开发者_如何学运维here is something wrong with my math and to be honest I'm not exactly sure what I'm doing as far as the math goes.

heres what the applicable code looks like:

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)));


Here is the math (assuming you got the hour, minute and second to display from Calendar)

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

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

//second
theta = 90 - 6*second;

Gives the angle in degrees, multiply by Math.PI/180 to convert to radians. End points of the hands aare at (r*cos(theta), -r*sin(theta)) from (centerX, centerY). Regards, - M.S.

0

精彩评论

暂无评论...
验证码 换一张
取 消