{"id":999,"date":"2010-09-09T11:43:11","date_gmt":"2010-09-09T06:13:11","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=999"},"modified":"2010-09-09T11:43:11","modified_gmt":"2010-09-09T06:13:11","slug":"bouncing-ball-applet-create-balls-on-mouse-click-of-random-color-and-random-size","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/bouncing-ball-applet-create-balls-on-mouse-click-of-random-color-and-random-size\/","title":{"rendered":"Bouncing ball applet &#8211; Create Balls on Mouse click of random color and random size"},"content":{"rendered":"<p><a href=\"https:\/\/jitendrazaa.com\/blog\/programming\/java\/mouse-handling-in-applets-using-this\/\" target=\"_blank\">In previous example, we have seen that how to animate two balls around the applet border.<\/a><\/p>\n<p>In this section, we will create ball of random size, random speed and random color on mouse click.<\/p>\n<p>To create a random number we have used <strong>Math.random().<\/strong><\/p>\n<p>To create a random number between 1 and 256, we have to multiply it by 256. <strong>256 * Math.random().<\/strong><\/p>\n<p>On every mouse click we have added the object of ball in List and every time in run method and paint method, we have iterate over the list and applied the logic<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/Bouncing-Ball-applet.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1000\" title=\"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=206%2C276&#038;ssl=1\" alt=\"Bouncing Ball applet - Create ball on Mouse click of random size and random color\" width=\"206\" height=\"276\" \/><\/a><!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.shivasoft;\n\nimport java.applet.Applet;\nimport java.awt.Color;\nimport java.awt.Graphics;\nimport java.awt.event.MouseEvent;\nimport java.awt.event.MouseListener;\nimport java.util.ArrayList;\nimport java.util.List;\n\nclass Ball\n{\nint x,y,radius,dx,dy;\nColor Ballcolor;\n\/**\n*\n* @param starting x position\n* @param starting y position\n* @param radius\n* @param x displacement\n* @param y displacement\n* @param ball Color\n*\/\npublic Ball(int x,int y,int radius,int dx, int dy, Color bColor)\n{\nthis.x = x;\nthis.y = y;\nthis.radius = radius;\nthis.dx = dx;\nthis.dy = dy;\nBallcolor = bColor;\n}\n}\n\npublic class BouncingBall extends Applet implements Runnable,MouseListener {\n\nList bCollection;\npublic void init()\n{\nbCollection = new ArrayList();\n\nThread t = new Thread(this);\nt.start();\naddMouseListener(this);\n}\npublic void paint(Graphics g)\n{\nfor(Ball b : bCollection)\n{\ng.setColor(b.Ballcolor);\ng.fillOval(b.x, b.y, b.radius, b.radius);\n}\n}\npublic void run()\n{\nwhile(true)\/\/infinite loop\n{\ntry\n{\nfor(Ball b : bCollection)\n{\ndisplacementOperation(b);\n}\nThread.sleep(20);\nrepaint();\n}catch(Exception e)\n{\n\n}\n}\n}\npublic void displacementOperation(Ball ball)\n{\nif(ball.y &amp;lt;= 0 || ball.y &amp;gt;= 200)\n{\nball.dy = -ball.dy;\n}\nif(ball.x &amp;lt;= 0 || ball.x &amp;gt;= 200)\n{\nball.dx = -ball.dx;\n}\n\nball.y = ball.y - ball.dy;\nball.x = ball.x - ball.dx;\n}\n@Override\npublic void mouseClicked(MouseEvent evt) {\nint r,g,b;\n\nr = (int)(255 * Math.random());\ng = (int)(255 * Math.random());\nb = (int)(255 * Math.random());\n\nColor c = new Color(r,g,b);\n\nint radius = (int)(20*Math.random());\n\nint dx = (int)(5*Math.random());\nint dy = (int)(5*Math.random());\n\nBall ba = new Ball(evt.getX(),evt.getY(),radius,dx, dy, c);\nbCollection.add(ba);\n\n}\n@Override\npublic void mouseEntered(MouseEvent evt) {\n\n}\n@Override\npublic void mouseExited(MouseEvent evt) {\n}\n@Override\npublic void mousePressed(MouseEvent evt) {\n}\n@Override\npublic void mouseReleased(MouseEvent evt) {\n}\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Creating the balls on mouse click of random color, random size and random speed and bouncing around the wall of applet in JAVA. <\/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":[329],"class_list":["post-999","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":847,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/mouse-handling-in-applets-using-this\/","url_meta":{"origin":999,"position":0},"title":"Bouncing ball animation in Applet","author":"Jitendra","date":"August 25, 2010","format":false,"excerpt":"Example to show Bouncing ball animation in Applet","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"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=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":895,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/multiplepage_load\/","url_meta":{"origin":999,"position":1},"title":"Page loads multiple times in Rad Window","author":"Jitendra","date":"August 27, 2010","format":false,"excerpt":"Solution of Multiple time page load problem of Telerik Rad Window.","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1774,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/difference-between-sax-and-dom-parsers\/","url_meta":{"origin":999,"position":2},"title":"Difference between SAX and DOM Parsers","author":"Jitendra","date":"March 22, 2011","format":false,"excerpt":"What is the difference between SAX And DOM Parsers","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":3025,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-11\/","url_meta":{"origin":999,"position":3},"title":"Salesforce Interview Questions \u2013 Part 11","author":"Jitendra","date":"August 19, 2012","format":false,"excerpt":"101. How to force lead assignment rule via Apex while updating or adding the Lead? Ans : To enforce Assignment Rules in Apex you will need to perform following steps: Instantiate the \"Database.DMLOptions\"\u009d class. Set the \"useDefaultRule\"\u009d property of \"assignmentRuleHeader\"\u009d to True. Finally call a native method on your Lead\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Pagination in SOQL using keyword Offset","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Pagination-in-SOQL-using-keyword-Offset.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":6285,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/free-tambola-game-number-generator\/","url_meta":{"origin":999,"position":4},"title":"Housie \/ Tambola \/ Bingo Game Number Generator","author":"Jitendra","date":"July 25, 2017","format":false,"excerpt":"Online free Housie \/ Tambola \/ Bingo Game. Simple to use and play with Bingo Number descriptions","rel":"","context":"In &quot;Others&quot;","block_context":{"text":"Others","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/others\/"},"img":{"alt_text":"Free online Tambola Game","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Tambola.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Tambola.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Tambola.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Tambola.gif?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Tambola.gif?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":3355,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/jquery-based-raffle-in-salesforce\/","url_meta":{"origin":999,"position":5},"title":"JQuery Based Raffle in Salesforce","author":"Jitendra","date":"June 29, 2013","format":false,"excerpt":"Recently i run into very interesting scenario where i had to randomly take name of few contacts. There are many ways to do it but I thought why not to implement this functionality in Salesforce with help of some Javascript? So i came up with this - JQuery based Raffle\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"JQuery Based Raffle in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-Based-Raffle-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-Based-Raffle-in-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/06\/JQuery-Based-Raffle-in-Salesforce.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/999","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=999"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/999\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}