Brandon/InteractiveArt

From ggc

Jump to: navigation, search

(Incomplete)


001 package Brandon;
002 
003 import wiki.Wiki;
004 import fang.*;
005 import java.awt.*;
006 import java.awt.geom.*;
007 
008 /**
009  Class: ITEC2120
010  * Assignment: Interactive Art
011  * Author: Brandon Adams
012  * Due Date: 02/19/08
013  */
014 public class InteractiveArt extends GameLoop
015 {
016   /**Rectangle sprite that serves as a background*/
017   private RectangleSprite background;
018 
019   /**Text strings for the author and title information*/
020   private StringSprite title, author;
021 
022   /**Booleans for toggling the help screen, sprite action, and background changing*/
023   private boolean info, run, holdBG, up;
024 
025   /**Integer used in the fading process*/
026   private int counter, rgb, r, g, b;
027 
028   /**Starts the game by creating and initializing sprites, and setting other applet/object properties.*/
029   public void startGame()
030   {
031     makeShapes();
032     makeStrings();
033 
034     info = false;
035     run  = holdBG = up = true;
036 
037     //holdBG =false;
038 
039     counter = rgb = r = g = b = 0;
040 
041     canvas.addSprite(background, title, author);
042 
043     setHelpText("H = Show/hide Help <br><br> R = Restart <br><br> P = Pause/Unpause <br><br> S = Sound On/Off");
044 
045     scheduleAbsolute(new Hold()1);
046   }
047 
048   /**Creates and initializes geometric shapes*/
049   private void makeShapes()
050   {
051     background = new RectangleSprite(11);
052     background.setLocation(0.50.5);
053     background.setColor(Color.BLACK);
054   }
055 
056   /**Creates and initializes strings*/
057   private void makeStrings()
058   {
059     title = new StringSprite(" Title: Interactive Art");
060     title.setHeight(0.03);
061     title.setColor(Color.WHITE);
062     title.leftJustify();
063     title.topJustify();
064     title.setLocation(00);
065 
066     author = new StringSprite("Author: Brandon Adams ");
067     author.setHeight(0.03);
068     author.setColor(Color.WHITE);
069     author.rightJustify();
070     author.topJustify();
071     author.setLocation(10);
072   }
073 
074   /**Called 25 times per second to control applet's functions*/
075   public void advanceFrame(double timePassed)
076   {
077     if(run)
078     {
079       if(!holdBG)
080         fade(background, 10);
081 
082       title.setColor(new Color(rand(255), rand(255), rand(255)));
083       author.setColor(new Color(rand(255), rand(255), rand(255)));
084     }
085 
086     handleKeys(getPlayer().getKeyboard().getLastKey());
087   }
088 
089   /**Calls various control methods in response to certain key presses*/
090   private void handleKeys(char key)
091   {
092     switch (key)
093     {
094     case 'r': scheduleRelative(new Reset()1)break;
095     case 'p': run = !run; break;
096     case 's': toggleAudible()break;
097     case 'h': info = !info; help.setVisible(info)break;
098     }
099   }
100 
101   /**Toggles holdBG on and off every second to control background's color*/
102   private class Hold implements Alarm
103   {
104     public void alarm()
105     {
106       if(run)
107       {
108         holdBG = !holdBG;
109         rgb = rand(2);
110         = rand(255);
111         = rand(255);
112         = rand(255);
113         counter = 0;
114       }
115 
116       scheduleRelative(this1);
117     }
118   }
119 
120   /**Waits one second to restart after R is pressed*/
121   private class Reset implements Alarm
122   {
123     public void alarm()
124     {
125       startOver();
126     }
127   }
128 
129   /**Returns a random integer value between 0 and limit*/
130   private int rand(int limit)
131   {
132     int val = (int)Math.round(Math.random() * limit);
133     return val;
134   }
135 
136   /**Fades a sprite's color back and forth from black to white, at a multiplied rate*/
137   private void fade(Sprite sprite, int mult)
138   {
139     for(int = 0; i < mult; i++)
140     {
141       if(up)
142       {
143         if((counter + mult255)
144         {
145           counter = 255;
146           up = false;
147         }
148         else
149           counter++;
150       }
151       else
152       {
153         if((counter - mult0)
154         {
155           counter = 0;
156           up = true;
157         }
158         else
159           counter--;
160       }
161     }
162 
163     switch (rgb)
164     {
165     case 0: sprite.setColor(new Color(counter, g, b))break;
166     case 1: sprite.setColor(new Color(r, counter, b))break;
167     case 2: sprite.setColor(new Color(r, g, counter))break;
168     }
169   }
170 }


Download/View Brandon/InteractiveArt.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