Skip to content

Instantly share code, notes, and snippets.

Created March 13, 2013 14:32
Show Gist options
  • Save anonymous/5152693 to your computer and use it in GitHub Desktop.
Save anonymous/5152693 to your computer and use it in GitHub Desktop.
Help me Assignment 2 pro 1
import java.awt.Color;
public class MovingRectangle {
/**
* method: showAnimation
* This method shows an animation in which a rectangle moves around, inside and close to
* the edges of an open window.
* Eveytime the rectangle changes the moving direction, its color is changed
* to the next color in the specified array. If there is no more color in the array,
* go back to the first color.
*
* The method must check for valid parameters, that is:
* - the rectangle must fit inside the window.
* - the color array must contain at least 1 color.
*
* @param windowHeight: the height of the window
* @param windowWidth: the width of the window
* @param recHeight: the height of the rectangle
* @param recWidth: the width of the rectangle
* @param colors: the array of Colors
*/
public static void showAnimation(int windowHeight, int windowWidth,
int recHeight, int recWidth, Color[] colors) {
// 1. Check for valid parameters
// ------------------------------
// Write your code here to check for valid parameters
// if the parameters are not valid, just print out an error message and
// exit.
// 2. Start showing the animation
// You are supposed to replace the code inside the while loop.
// The code here is just to show you how to draw and erase a rectagle
// by calling method retangle().
// Please follow the requirements in the assignments to show the animation
// correctly.
how to check the parameter ??
while (Mosaic.isOpen()) {
// Draw the rectangle in color c, pause so the user can see it, then
// redraw the same rectangle in black, effectively erasing the
// the colored rectangle.
for (int i = 1; i<5;i++){Color c = Color[i];} //i try ramdom color by using array but it is not work
// color of the rectangle
for (int i = 1;i<=300;i=i+10){
rectangle(0, i, recHeight, recWidth, c);
Mosaic.delay(50);
rectangle(0, i, recHeight, recWidth, Color.BLACK);
Mosaic.delay(50);}
Color d= Color.BLUE;
for (int i = 1;i<=290;i=i+10){
rectangle(i, 290, recHeight, recWidth, d);
Mosaic.delay(50);
rectangle(i, 290, recHeight, recWidth, Color.BLACK);
Mosaic.delay(50);}
for (int i = 300;i>=0;i=i-10){
rectangle(290, i, recHeight, recWidth, c);
Mosaic.delay(50);
rectangle(290, i, recHeight, recWidth, Color.BLACK);
Mosaic.delay(50);}
for (int i = 300;i>=0;i=i-10){
rectangle(i, 0, recHeight, recWidth, c);
Mosaic.delay(50);
rectangle(i, 0, recHeight, recWidth, Color.BLACK);
Mosaic.delay(50);}
// Now, adjust the parameters to get ready to draw the next rectangle
// ....
} // end while()
} // end showAnimation()
/**
* Draws the outline of a rectangle in the window by setting
* the color of each little square on that rectangle;
* @param top is the starting row of the rectangle
* @param left is the starting column
* @param height is the number of rows in the rectangle
* @param width is the number of columns
* @param r red component of the color
* @param g green component of the color
* @param b blue component of the color
*/
static void rectangle(int top, int left, int height, int width, Color c) {
// Your job is to write the code for this method
// The code below just colors the corners of the rectangle.
// You have to draw the whole outline of it.
Mosaic.setColor(top, left, c); // top left corner
for (int i = 0;i<=8;i++)
{
Mosaic.setColor(top,left+i,c);
}
Mosaic.setColor(top, left + width - 1, c); // top right corner }
for (int i = 0;i<=8;i++)
{
Mosaic.setColor(top+i,left+width-1,c);
}
Mosaic.setColor(top + height - 1, left, c); // bottom left corner
for (int i = 0;i<=8;i++)
{
Mosaic.setColor(top + height - 1, left+i, c);
}
Mosaic.setColor(top + height - 1, left+width-1, c);
for(int i =0; i<=8;i++){
Mosaic.setColor(top + height - 1-i, left, c); // bottom right corner
}
} // end rectangle()
} // end class MovingSquare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment