Skip to content

Instantly share code, notes, and snippets.

@darthhomme
Created April 23, 2017 01:27
Show Gist options
  • Save darthhomme/b380f05119d3902e611c19036cdb748f to your computer and use it in GitHub Desktop.
Save darthhomme/b380f05119d3902e611c19036cdb748f to your computer and use it in GitHub Desktop.
might help you, Marc
package shapes;
import java.awt.Graphics;
import java.awt.Color;
/**
* @author - Jeong Min Kim u0264416
* @version - 3/14/2017
* @pre_condition you need 6 int values
* @post_condition you get back an object that is initially 200 pixels in width and 100 in height. It will move on a timer
*/
public class Rectangle
{
int RECT_WIDTH = 200;
int RECT_HEIGHT = 100;
int x = Shapes.WIN_WIDTH/2-100;
int y = Shapes.WIN_HEIGHT/2-50;
int xVelocity=4;
int yVelocity=4;
Color rectangleColor = Color.CYAN;
public void paint(Graphics g)
{
g.setColor(rectangleColor);
g.fillRect(x, y, RECT_WIDTH, RECT_HEIGHT);
}
public void update()
{
// we are giving directions to our rectangle object
// we want our object to move down and to the left by incremements of 4 pixels.
x = x + xVelocity;
y = y + yVelocity;
// if x is less than 0 or x is greather than the height of our JPanel, then
// the velocity xVelocity will be subtracted by 4
if(x<0 || x > Shapes.WIN_HEIGHT)
{
xVelocity=-xVelocity;
}
if(y<0 || y> Shapes.WIN_HEIGHT)
{
yVelocity=-yVelocity;
}
}
// this method will when called choose the color of the object at random.
public void changeColorOnMouseEvent()
{
Color colorsArray[] = new Color[]{Color.CYAN, Color.MAGENTA, Color.BLACK, Color.YELLOW, Color.DARK_GRAY, Color.RED, Color.BLUE, Color.GREEN, Color.ORANGE, Color.PINK};
int value =(int)(Math.random()*colorsArray.length);
rectangleColor = colorsArray[value];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment