{"id":963,"date":"2010-05-11T19:10:32","date_gmt":"2010-05-11T19:10:32","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=963"},"modified":"2011-01-14T13:59:22","modified_gmt":"2011-01-14T13:59:22","slug":"problem-2","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/11\/problem-2\/","title":{"rendered":"Problem 2"},"content":{"rendered":"<p><span id=\"zipfile\"><a href='http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/05\/Assignment2_Code.zip'><\/a><\/span>This problem 2 is &#8220;Rainbow Write a GraphicsProgram subclass that draws a rainbow&#8221;, the basics of the problem are &#8220;Starting at the top, the six arcs are red, orange, yellow, green, blue, and magenta, respectively; cyan makes a lovely colour for the sky. The top of the arc should not be off the screen. Each of the arcs in the rainbow should get clipped along the sides of the window,and not along the bottom. Your rainbow should be symmetric and nicely drawn, regardless of window size.&#8221;<\/p>\n<p>The way that I thought about this, is to basically use the GOval to create a circle that is placed in the centre width and slowly go down on the Y coordinates, the size of the GOval is going to be twice the size of the screen itself, thus will meet the problem above.  I setup a array of colours, that will then go through within a for loop to draw the GOval(circles) on the screen as below<\/p>\n<pre lang=\"java\">\r\nColor colours[] = new Color[7];\r\ncolours[0] = Color.RED;\r\ncolours[1] = Color.ORANGE;\r\ncolours[2] = Color.YELLOW;\r\ncolours[3] = Color.GREEN;\r\ncolours[4] = Color.BLUE;\r\ncolours[5] = Color.MAGENTA;\r\ncolours[6] = Color.CYAN;\r\n\r\nfor (int i =0; i < 7; i++)\r\n{\r\n\tGOval newCircle = new GOval(-getWidth()\/2,10+(i*10), getWidth()*2, getHeight()*2);\r\n\tnewCircle.setColor(colours[i]);\r\n\tnewCircle.setFillColor(colours[i]);\r\n\tnewCircle.setFilled(true);\r\n\tadd(newCircle);\r\n}\r\n<\/pre>\n<p>Here is my output of my Rainbow to the solution of the problem <\/p>\n<figure id=\"attachment_964\" aria-describedby=\"caption-attachment-964\" style=\"width: 764px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/05\/rainbow.jpeg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.codingfriends.com\/wp-content\/uploads\/2010\/05\/rainbow.jpeg\" alt=\"CS106A Assignment 2 Problem 2\" title=\"rainbow\" width=\"764\" height=\"250\" class=\"size-full wp-image-964\" \/><\/a><figcaption id=\"caption-attachment-964\" class=\"wp-caption-text\">CS106A Assignment 2 Problem 2<\/figcaption><\/figure>\n<p>I have included the full source code in the zip file above., but here is the full source code for this problem.<\/p>\n<pre lang=\"java\">\r\n\/*\r\n * File: Rainbow.java\r\n * ------------------\r\n * This program is a stub for the Rainbow problem, which displays\r\n * a rainbow by adding consecutively smaller circles to the canvas.\r\n *\/\r\n\r\nimport acm.graphics.*;\r\nimport acm.program.*;\r\nimport java.awt.*;\r\n\r\npublic class Rainbow extends GraphicsProgram {\r\n\r\n\tpublic void run() {\r\n\t\tColor colours[] = new Color[7];\r\n\t\tcolours[0] = Color.RED;\r\n\t\tcolours[1] = Color.ORANGE;\r\n\t\tcolours[2] = Color.YELLOW;\r\n\t\tcolours[3] = Color.GREEN;\r\n\t\tcolours[4] = Color.BLUE;\r\n\t\tcolours[5] = Color.MAGENTA;\r\n\t\tcolours[6] = Color.CYAN;\r\n\t\t\r\n\t\tGRect background = new GRect(0,0,getWidth(), getHeight());\r\n\t\tbackground.setColor(Color.CYAN);\r\n\t\tbackground.setFillColor(Color.CYAN);\r\n\t\tbackground.setFilled(true);\r\n\t\tadd(background);\r\n\t\t\r\n\t\tfor (int i =0; i < 7; i++)\r\n\t\t{\r\n\t\t\tGOval newCircle = new GOval(-getWidth()\/2,10+(i*10), getWidth()*2, getHeight()*2);\r\n\t\t\tnewCircle.setColor(colours[i]);\r\n\t\t\tnewCircle.setFillColor(colours[i]);\r\n\t\t\tnewCircle.setFilled(true);\r\n\t\t\tadd(newCircle);\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This problem 2 is &#8220;Rainbow Write a GraphicsProgram subclass that draws a rainbow&#8221;, the basics of the problem are &#8220;Starting at the top, the six arcs are red, orange, yellow, green, blue, and magenta, respectively; cyan makes a lovely colour for the sky. The top of the arc should not be off the screen. Each &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/11\/problem-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Problem 2<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-963","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=963"}],"version-history":[{"count":6,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/963\/revisions"}],"predecessor-version":[{"id":1337,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/963\/revisions\/1337"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}