简体   繁体   中英

Can't get the right point for Gui Java Clock

There 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, - MS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM