| 01 | /* |
| 02 | * File: Rainbow.java |
| 03 | * ------------------ |
| 04 | * This program displays a rainbow by adding consecutively |
| 05 | * smaller circles to the canvas using an array. |
| 06 | */ |
| 07 | |
| 08 | import java.awt.Color; |
| 09 | |
| 10 | import acm.graphics.GOval; |
| 11 | import acm.graphics.GRect; |
| 12 | import acm.program.*; |
| 13 | |
| 14 | public class Rainbow extends GraphicsProgram { |
| 15 | |
| 16 | public static final int SUN_RADIUS = 80; |
| 17 | public static final int BAND_WIDTH = 20; |
| 18 | public static final int RAINBOW_OVERHANG = 380; |
| 19 | public static final int RAINBOW_OFFSET = 150; |
| 20 | |
| 21 | public void run() { |
| 22 | |
| 23 | // Creating a sky with a GRect method |
| 24 | |
| 25 | drawSky (Color.CYAN); |
| 26 | |
| 27 | // Creating a sky with a GOval method |
| 28 | // drawCircle (220, 220, 510, Color.CYAN); |
| 29 | |
| 30 | Color[] colors = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA, Color.CYAN}; |
| 31 | |
| 32 | for (int i=0; i<colors.length; i++){ |
| 33 | |
| 34 | drawCircle (getWidth()/2, getHeight() + RAINBOW_OFFSET, ((getWidth()+RAINBOW_OVERHANG)/2)-(i*BAND_WIDTH), colors[i]); |
| 35 | |
| 36 | } |
| 37 | |
| 38 | // Creating a cute, little sun in the corner using the GOval method |
| 39 | |
| 40 | drawCircle (0, 0, SUN_RADIUS, Color.YELLOW); |
| 41 | |
| 42 | } |
| 43 | |
| 44 | private void drawCircle(int x, int y, int r, Color acolor) { |
| 45 | |
| 46 | GOval myCircle = new GOval(x-r, y-r, r*2, r*2); |
| 47 | myCircle.setFilled(true); |
| 48 | myCircle.setColor(acolor); |
| 49 | add (myCircle); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | private void drawSky(Color skycolor) { |
| 54 | |
| 55 | GRect mySky = new GRect(0, 0, getWidth(), getHeight()); |
| 56 | mySky.setFilled(true); |
| 57 | mySky.setColor(skycolor); |
| 58 | add (mySky); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.