{"id":799,"date":"2011-09-02T12:45:00","date_gmt":"2011-09-02T12:45:00","guid":{"rendered":"http:\/\/projectsgeek.com\/developing-clock-in-java-using-multi-threading\/"},"modified":"2018-04-21T13:19:03","modified_gmt":"2018-04-21T07:49:03","slug":"developing-clock-in-java-using-multi-threading","status":"publish","type":"post","link":"https:\/\/projectsgeek.com\/2011\/09\/developing-clock-in-java-using-multi-threading.html","title":{"rendered":"Developing Clock In java using Multi threading"},"content":{"rendered":"<div dir=\"ltr\">\n<div><b>Developing Clock In java using Multi threading<\/b><\/div>\n<div><\/div>\n<div><a href=\"https:\/\/3.bp.blogspot.com\/-rLjOe1i4H6Q\/TmDP5NqpcoI\/AAAAAAAAAAk\/oyTVNsPvsaM\/s1600\/Capture3.PNG\" rel=\"nofollow noopener\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/3.bp.blogspot.com\/-rLjOe1i4H6Q\/TmDP5NqpcoI\/AAAAAAAAAAk\/oyTVNsPvsaM\/s320\/Capture3.PNG\" width=\"320\" height=\"190\" border=\"0\" alt=\"\"><\/a><\/div>\n<div><\/div>\n<div><\/div>\n<div><b> <\/b><br \/>\nimport java.util.*;<br \/>\nimport java.awt.*;<br \/>\nimport java.applet.*;<br \/>\nimport java.text.*;<br \/>\npublic class Clock extends Applet implements Runnable<br \/>\n{<br \/>\nThread timer;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The thread that displays clock<br \/>\nSimpleDateFormat formatter;\u00a0 \/\/ Formats the date displayed<br \/>\nDate currentDate;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Used to get date to display<br \/>\nColor handColor,numberColor;\u00a0\u00a0 \/\/ Color of main hands and dial\/\/ Color of second hand and numbers<br \/>\nint xcenter = 80, ycenter = 55; \/\/ Center position<br \/>\npublic void init()<br \/>\n{<br \/>\nformatter = new SimpleDateFormat (&#8220;EEE MMM dd hh:mm:ss yyyy&#8221;);<br \/>\ncurrentDate = new Date();<br \/>\nhandColor = Color.blue;<br \/>\nnumberColor = Color.darkGray;<br \/>\n}<br \/>\npublic void paint(Graphics g)<br \/>\n{<br \/>\nint xh, yh, xm, ym, xs, ys;<br \/>\nint s = 0, m = 10, h = 10;<br \/>\nString today;<br \/>\ncurrentDate = new Date();<br \/>\nformatter.applyPattern(&#8220;s&#8221;);<br \/>\ns = Integer.parseInt(formatter.format(currentDate));<br \/>\nformatter.applyPattern(&#8220;m&#8221;);<br \/>\nm = Integer.parseInt(formatter.format(currentDate));<br \/>\nformatter.applyPattern(&#8220;h&#8221;);<br \/>\nh = Integer.parseInt(formatter.format(currentDate));<br \/>\nxs = (int) (Math.cos(s * Math.PI \/ 30 &#8211; Math.PI \/ 2) * 45 + xcenter);<br \/>\nys = (int) (Math.sin(s * Math.PI \/ 30 &#8211; Math.PI \/ 2) * 45 + ycenter);<br \/>\nxm = (int) (Math.cos(m * Math.PI \/ 30 &#8211; Math.PI \/ 2) * 40 + xcenter);<br \/>\nym = (int) (Math.sin(m * Math.PI \/ 30 &#8211; Math.PI \/ 2) * 40 + ycenter);<br \/>\nxh = (int) (Math.cos((h*30 + m \/ 2) * Math.PI \/ 180 &#8211; Math.PI \/ 2) * 30 + xcenter);<br \/>\nyh = (int) (Math.sin((h*30 + m \/ 2) * Math.PI \/ 180 &#8211; Math.PI \/ 2) * 30 + ycenter);<br \/>\nformatter.applyPattern(&#8220;EEE MMM dd HH:mm:ss yyyy&#8221;);<br \/>\ntoday = formatter.format(currentDate);<br \/>\ng.setColor(numberColor);<br \/>\ng.drawString(today, 5, 125);<br \/>\ng.drawLine(xcenter, ycenter, xs, ys);<br \/>\ng.setColor(handColor);<br \/>\ng.drawLine(xcenter, ycenter-1, xm, ym);<br \/>\ng.drawLine(xcenter-1, ycenter, xm, ym);<br \/>\ng.drawLine(xcenter, ycenter-1, xh, yh);<br \/>\ng.drawLine(xcenter-1, ycenter, xh, yh);<br \/>\ng.setColor(handColor);<br \/>\ng.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);<br \/>\ng.setColor(numberColor);<br \/>\ng.drawString(&#8220;9&#8221;, xcenter-45, ycenter+3);<br \/>\ng.drawString(&#8220;3&#8221;, xcenter+40, ycenter+3);<br \/>\ng.drawString(&#8220;12&#8221;, xcenter-5, ycenter-37);<br \/>\ng.drawString(&#8220;6&#8221;, xcenter-3, ycenter+45);<br \/>\n}<br \/>\npublic void start()<br \/>\n{<br \/>\ntimer = new Thread(this);<br \/>\ntimer.start();<br \/>\n}<br \/>\npublic void stop()<br \/>\n{<br \/>\ntimer = null;<br \/>\n}<br \/>\npublic void run()<br \/>\n{<br \/>\nThread me = Thread.currentThread();<br \/>\nwhile (timer == me)<br \/>\n{<br \/>\ntry<br \/>\n{<br \/>\nThread.currentThread().sleep(100);<br \/>\n} catch (InterruptedException e) {}<br \/>\nrepaint();<br \/>\n}<br \/>\n}<br \/>\n}<\/div>\n<div><\/div>\n<div><a href=\"https:\/\/www.blogger.com\/goog_57348049\" rel=\"nofollow noopener\" target=\"_blank\">\u00a0<\/a><\/div>\n<div><a href=\"https:\/\/www.blogger.com\/%20%20%09%20https:\/\/www.ziddu.com\/download\/16262765\/MultiThreading_Clock.rar.html\" rel=\"nofollow noopener\" target=\"_blank\"><b>Download Code <\/b><\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Developing Clock In java using Multi threading import java.util.*; import java.awt.*; import java.applet.*; import java.text.*; public class Clock extends Applet implements Runnable { Thread timer;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The thread that displays clock SimpleDateFormat formatter;\u00a0 \/\/ Formats the date displayed Date currentDate;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Used to get date to display Color handColor,numberColor;\u00a0\u00a0 \/\/ Color of main hands and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[10],"tags":[],"class_list":{"0":"post-799","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-java-assignments","7":"entry"},"_links":{"self":[{"href":"https:\/\/projectsgeek.com\/wp-json\/wp\/v2\/posts\/799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/projectsgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/projectsgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/projectsgeek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/projectsgeek.com\/wp-json\/wp\/v2\/comments?post=799"}],"version-history":[{"count":0,"href":"https:\/\/projectsgeek.com\/wp-json\/wp\/v2\/posts\/799\/revisions"}],"wp:attachment":[{"href":"https:\/\/projectsgeek.com\/wp-json\/wp\/v2\/media?parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/projectsgeek.com\/wp-json\/wp\/v2\/categories?post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/projectsgeek.com\/wp-json\/wp\/v2\/tags?post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}