{"id":847,"date":"2010-08-25T22:57:59","date_gmt":"2010-08-25T17:27:59","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=847"},"modified":"2010-08-25T22:57:59","modified_gmt":"2010-08-25T17:27:59","slug":"mouse-handling-in-applets-using-this","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/mouse-handling-in-applets-using-this\/","title":{"rendered":"Bouncing ball animation in Applet"},"content":{"rendered":"<p><a href=\"https:\/\/jitendrazaa.com\/blog\/programming\/java\/applet-lifecycle-of-applet\/\" target=\"_blank\">In Previous article, we have seen the life cycle of Applet.<\/a><\/p>\n<p>In this example, i will dem0nstrate that how we can animate ball and bounce it around the walls.<\/p>\n<p>Please give your feedback on this article, so that i can improve it in future.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport java.applet.Applet;\nimport java.awt.Color;\nimport java.awt.Graphics;\n\nclass Ball\n{\n  int x,y,radius,dx,dy;\n  Color BallColor;\n\n public Ball(int x,int y,int radius,int dx,int dy,Color bColor)\n {\n   this.x=x;\n   this.y=y;\n   this.radius=radius;\n   this.dx=dx;\n   this.dy=dy;\n   BallColor=bColor;\n }\n}\npublic class BouncingbALL extends Applet implements Runnable{\nBall redBall,blackBall;\n  public void init()\n  {\n    redBall=new Ball(80,80,20,2,4,Color.red);\n    blackBall=new Ball(40,70,20,4,2,Color.black);\n    Thread t=new Thread(this);\n    t.start();\n  }\n\n public void paint(Graphics g)\n {\n    g.setColor(redBall.BallColor);\n    g.fillOval(redBall.x, redBall.y, redBall.radius, redBall.radius);\n\n    g.setColor(blackBall.BallColor);\n    g.fillOval(blackBall.x, blackBall.y, blackBall.radius, blackBall.radius);\n }\n\n  public void run()\n  {\n    while(true)\n    {\n     try\n        {\n           displacementOperation(redBall);\n           displacementOperation(blackBall);\n           Thread.sleep(20);\n           repaint();\n        }\n    catch(Exception e){}\n    }\n  }\n\/\/This method checks the boundary condition of ball movement\npublic void displacementOperation(Ball ball)\n {\n  if(ball.y &gt;= 200 || ball.y &lt;= 0)\n  {\n    ball.dy=-ball.dy;\n  }\n\n  if(ball.x &gt;= 200 || ball.x &lt;= 0)\n  {\n    ball.dx=-ball.dx;\n  }\n\n   ball.y=ball.y-ball.dy;\n   ball.x=ball.x-ball.dx;\n  }\n}\n<\/pre>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/bOUNCINGBall.jpeg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-882\" title=\"Mouse handling in applets using this pointer method, Bouncing ball\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/08\/bOUNCINGBall.jpeg?resize=210%2C275&#038;ssl=1\" alt=\"Mouse handling in applets using this pointer method, Bouncing ball\" width=\"210\" height=\"275\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example to show Bouncing ball animation in Applet<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3],"tags":[41,92,329],"class_list":["post-847","post","type-post","status-publish","format-standard","hentry","category-java","tag-applet","tag-event-handling","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":999,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/bouncing-ball-applet-create-balls-on-mouse-click-of-random-color-and-random-size\/","url_meta":{"origin":847,"position":0},"title":"Bouncing ball applet &#8211; Create Balls on Mouse click of random color and random size","author":"Jitendra","date":"September 9, 2010","format":false,"excerpt":"Creating the balls on mouse click of random color, random size and random speed and bouncing around the wall of applet in JAVA.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Bouncing Ball applet - Create ball on Mouse click of random size and random color","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/Bouncing-Ball-applet.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1767,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/xml-tree-viewer-using-sax-parser-in-java-with-jtreejfilechooser-component-of-swing\/","url_meta":{"origin":847,"position":1},"title":"XML Tree Viewer using SAX Parser in JAVA with Jtree,JFileChooser component of Swing","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"XML Tree Viewer using SAX Parser in JAVA with Jtree,JFileChooser component of Swing, How to refresh Jtree when content changes , Expand or collapse JTree","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"XML Tree Viewer using SAX Parser in JAVA","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/XML-Tree-Viewer-using-SAX-Parser-in-JAVA.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":748,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/producer-consumer-problem\/","url_meta":{"origin":847,"position":2},"title":"Thread Synchronization","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"Describes Synchronization, Producer and Consumer Problem, wait(), notify(), InterThread Communication","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1825,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-thread-timertask\/","url_meta":{"origin":847,"position":3},"title":"Java Thread &#8211; Executor framework, Timer and TimerTask","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Tutorial and example of Executor framework, Timer and TimerTask over Thread","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Animation of ProgressBar in Swing Using TimerTask","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Animation-of-ProgressBar-Swing-Using-TimerTask.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":979,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/invoke-method-using-reflection-in-java\/","url_meta":{"origin":847,"position":4},"title":"Invoke method using reflection in JAVA","author":"Jitendra","date":"September 6, 2010","format":false,"excerpt":"Invoke method using reflection in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1984,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/compile-and-run-java-program-in-package-from-command-line\/","url_meta":{"origin":847,"position":5},"title":"Compile and run Java program in package from command line","author":"Jitendra","date":"April 14, 2011","format":false,"excerpt":"How to Compile JAVA Program in Package from command line","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Java Directory structure","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/Java-Directory-structure.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=847"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/847\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}