FA/Assignment4 AllShapes

From ggc

Jump to: navigation, search

001 package FA;
002 
003 import fang.*;
004 import java.awt.*;
005 import java.awt.geom.*;
006 import wiki.Wiki;
007 import java.lang.Math.*;
008 
009 /**
010  * The program displays sets of dots arranged in the shape of a circle, 
011  * spiral, a rectangular grid and a sine wave.A;so, a flower is displayed by rotating
012  * reducing a series of ellipses place about a common center.
013  * @authorFrank Anderson
014  */
015 public class Assignment4_AllShapes extends Game
016 {
017   /**sets up the game*/
018 
019   private void Assignment4Circle()
020   {
021     /**
022        * Display a circle with dots of equal angular separation of 36 degrees.
023       * x- and y-coordinates where the center of the dots
024       * outlining the circle will be drawn. */
025     double xCoordinate;
026     double yCoordinate;
027 
028     // The center of the cirlce is at the center of the screen, (.5, .5).
029     double x_CircleCenter = .5;
030     double y_CircleCenter = .5;
031 
032     /* The circle whose circumference will be drawn has a radius of .35,
033      *thus the circle should occupy 2(.35) = 70% of the screen. */
034     double radius_of_circle = .35;
035 
036     // Select the number of dots to be located along the circumference.
037     int number_of_dots = 10;
038 
039     // Dots will begin at a selected initial angle. This case it is  0 degrees.
040     double initial_angle = 0;
041 
042     /* To have the dots placed at equal angular distances, the angle must
043      * must be increased the same percentange of the circle each time.  */
044     double angle_increment = 2*3.14/number_of_dots;
045     double angle_of_radius;
046 
047 
048     for (int = 1; i <= number_of_dots; i++)
049     {
050       /* A circle will be used to represent the dots. Its circle's size
051        * will occupy .05 of the screen.  */
052       OvalSprite oval = new OvalSprite(1,1);
053 
054       // Adjust the size of the dots to the number of individual dots to be shown.
055       oval.setSize(.5/number_of_dots);
056 
057       /* The angle that the radius makes with the horizontal will
058        * change for the location of each dot along the circumference.
059        * The angle changes by the same increment as it changes position. */
060       angle_of_radius = initial_angle + i*angle_increment;
061 
062       /* Convert the radius's polar coordinates to Cartesian coordinates
063        * using  x = r cos(angle) and  y = r sin(angle). */
064 
065       xCoordinate = radius_of_circle*Math.sin(angle_of_radius);
066       xCoordinate = xCoordinate + x_CircleCenter;
067 
068       yCoordinate = radius_of_circle*Math.cos(angle_of_radius);
069       yCoordinate = yCoordinate + y_CircleCenter;
070 
071       // Set the location of the center of the dot and display the dot.
072       oval.setLocation(xCoordinate, yCoordinate);
073       addSprite(oval);
074     }
075   }
076 
077   private void Assignment4SineWave()
078   {/**
079                                                                  *Determine the number of dots to be placed on the screen. */
080     int number_of_dots;
081     number_of_dots = 20;
082 
083     /*For each dot determine the x location and the y location
084     on the screen where the center of the sine curve  will be at (.5, .5).
085 
086     The y value will be the appropriate coordinate above or below 
087     the horizontal line across the middle of the screen.*/
088 
089     double xCoordinate;
090     double yCoordinate;
091     double adjust_ht;
092     adjust_ht=.4;
093 
094     /* Display number_of_dots   on the screen*/
095     for(int = 1; i<= number_of_dots; i++)
096     {
097       /*Determine the x- and y-coordinates for each point and plot that point*/
098 
099       OvalSprite oval = new OvalSprite(1,1);
100       oval.setSize(.03);
101       xCoordinate = (i-1)*(1.0/number_of_dots)+1.0/(2.0*number_of_dots);
102       yCoordinate = .5 - adjust_ht*(Math.sin((i-1)*2.0*3.14/number_of_dots));
103 
104       oval.setLocation(xCoordinate, yCoordinate);
105       addSprite(oval);
106     }
107   }
108 
109 
110   private void Assignment4Spiral()
111   {
112     /**
113       * Display a spiral with dots of equal angular separation of 36 degrees.
114       * The radius increases in equal increments starting at the center of the screen.
115        */
116 
117     double xCoordinate;
118     double yCoordinate;
119     double x_Spiral_Center = .5;
120     double y_Spiral_Center = .5;
121     double radius_of_spiral;
122     int number_of_dots = 40;
123     double initial_angle = 0.0;
124     double angle_increment = 470.0/number_of_dots;
125     double radius_increment =.4/number_of_dots
126                              ;
127     double angle_of_radius;
128     double initial_radius = 0;
129 
130 
131     for (int = 1; i <= number_of_dots; i++)
132     {
133       // create the dots to be displayed.
134       OvalSprite oval = new OvalSprite(1,1);
135       oval.setSize(.5/number_of_dots);
136 
137       /* Increase the angle of the radius by 360/number of dots
138       for each new dot location. */
139       angle_of_radius = initial_angle + (i-1)*angle_increment;
140 
141       /*Increase the radius of the spiral in equal lengths.*/
142       radius_of_spiral = initial_radius +(i-1)*radius_increment;
143 
144       /* Convert from polar to Cartesian coordinates.
145        * using x = r cos(angle)  and  y = r sin(angle) */
146       xCoordinate = radius_of_spiral*Math.sin(angle_of_radius*3.14/180);
147       xCoordinate = xCoordinate + x_Spiral_Center;
148 
149       yCoordinate = radius_of_spiral*Math.cos(angle_of_radius*3.14/180);
150       yCoordinate = yCoordinate + y_Spiral_Center;
151 
152       // Set the center of the dot and display it on the screen.
153       oval.setLocation(xCoordinate, yCoordinate);
154       addSprite(oval);
155     }
156   }
157 
158   private void Assignment4Grid()
159   {
160     /**
161       * This program will place 100 dots on the screen in a square grid
162       * with 10 rows and 10 columns.
163       */
164     int number_of_rows = 10;
165     int ovals_per_row = 10;
166 
167     double xCoordinate;
168     double yCoordinate;
169     for (int = 1; j <= number_of_rows; j++)
170     {
171       yCoordinate = (1.0/number_of_rows)*(j-1+ 1.0/(2*number_of_rows);
172       for (int = 1;  i <= ovals_per_row; i++)
173       {
174         OvalSprite oval = new OvalSprite(1,1);
175         oval.setSize(.02);
176         xCoordinate = 1.0/(2*ovals_per_row+ (1.0/ovals_per_row)*(i-1);
177         oval.setLocation(xCoordinate, yCoordinate);
178         addSprite(oval);
179       }
180     }
181   }
182 
183   private void Assignment4Rose()
184   {
185     /**
186       * This program displays a series of rose petals made of oval shapes that have been rotated equal amounts. 
187       * The number of petals is represented by the layers in the rose. Each petal is smaller in size than the previous layer.
188       * All petals are centered at the middle of the screen.
189       @author Frank Anderson & Derrick Dixon
190       */
191 
192     /* The number of petals on rose will be represented as layers. */
193     int layers = 10;
194     double initial_angle;
195     /* Each oval representing a layer of the rose will be rotated the same
196       number of degrees for each subsequent oval. */
197     initial_angle = 360.0/layers;
198     for(int i=1; i <= layers; i++)
199     {
200       OvalSprite petal=new OvalSprite (32);
201       petal.rotateDegrees(initial_angle + (i - 1)*360.0/layers);
202 
203       /* The size of the petal originally occupies  .90%
204       of the screen and is then decreased in size 90% for subsequent petals.*/
205       petal.setSize(.9 -(0.9*(i-1)/layers));
206 
207       /* The center of each oval representing a petal is at
208        * the center of the screen, (.5, .5). */
209       petal.setLocation(0.50.5);
210 
211       /* Alternate the colors of the petals between blue and yellow depending on
212       whether the petal number is odd or even. Blue for even numbered petals;
213       yellow for odd numbered petals. */
214 
215       if (% == 0)
216         petal.setColor(Palette.getColor("Blue"));
217       else
218         petal.setColor(Palette.getColor("Yellow"));
219 
220       // Display the petal of the rose.
221       addSprite(petal);
222     }
223   }
224 
225 
226 
227 
228 
229 
230   /**handle input and game events*/
231   public void advance()
232   {
233     /* Place directions on the screen. */
234 
235     StringSprite directions = new StringSprite"Depress: c, g, l, r, or  s.");
236     directions.setWidth(1);
237     directions.leftJustify();
238     directions.setHeight(.06);
239     directions.setLocation(0,.95);
240     directions.setColor(new Color(255,0,0));
241     addSprite(directions);
242 
243     /* Display graphic depending on character chosen. */
244     if (getKeyPressed() == 'c')
245     {
246       removeAllSprites();
247       Assignment4Circle();
248     }
249     if (getKeyPressed() == 'r')
250     {
251       removeAllSprites();
252       Assignment4Rose();
253     }
254     if (getKeyPressed() == 'l')
255     {
256       removeAllSprites();
257       Assignment4Spiral();
258     }
259     if (getKeyPressed() == 'g')
260     {
261       removeAllSprites();
262       Assignment4Grid();
263     }
264     if (getKeyPressed() == 's')
265     {
266       removeAllSprites();
267       Assignment4SineWave();
268     }
269   }
270 
271 }


Download/View FA/Assignment4_AllShapes.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter