Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Polygon;
- import java.util.Random;
- //Jacob Baker
- //Lab-08
- //Star.java
- public class Star {
- private int centerX;
- private int centerY;
- private int outerRadius;
- private Color color;
- private boolean isMovingRight;
- private boolean isMovingDown;
- private int speed;
- private int panelWidth, panelHeight;
- public Star(int centerX, int centerY,int innerRadius, Color color) { //adjusted methods using Star() to divide out the 2 in "this.outerRadius = innerRadius*2"; too lazy to go through and edit it all out :p
- this.centerX = centerX;
- this.centerY = centerY;
- this.outerRadius = innerRadius*2;
- this.color = color;
- }
- public Star(int panelWidth, int panelHeight) {
- this.panelWidth = panelWidth;
- this.panelHeight = panelHeight;
- this.color = Color.yellow;
- Random rand = new Random();
- this.outerRadius = ((int) panelWidth/48) + 10;
- this.centerX = rand.nextInt(panelWidth - outerRadius);
- this.centerY = rand.nextInt(panelHeight - outerRadius);
- int r = rand.nextInt(2); // select 0 or 1 randomly
- if (r==0) {
- isMovingRight = true;
- } else {
- isMovingRight = false;
- }
- int d = rand.nextInt(2);
- if (d==0) {
- isMovingDown = true;
- } else {
- isMovingDown = false;
- }
- speed = rand.nextInt(3) + 1; // random number between 1 and 3;
- }
- //ANIMATION:
- public void moveHorizontal(int deltaX) {
- this.centerX += deltaX;
- }
- public void moveVertical(int deltaY) {
- this.centerY += deltaY;
- }
- public void moveRight() {
- this.moveHorizontal(speed);
- }
- public void moveLeft() {
- this.moveHorizontal(-speed);
- }
- public void moveDown() {
- this.moveVertical(speed);
- }
- public void moveUp() {
- this.moveVertical(-speed);
- }
- public void move() {
- if (isMovingRight && (getCenterX() > (this.panelWidth - this.outerRadius))) {
- //if the shape is moving right and I just hit the edge
- isMovingRight = false; // start moving left
- } else if (!isMovingRight && getCenterX() < 0) {
- //if the shape is moving left and just hit the edge
- isMovingRight = true;
- }
- //if we're not bumping into an edge yet
- if (isMovingRight) {
- moveRight();
- } else {
- moveLeft();
- }
- //now up and down
- if (isMovingDown && getCenterY() > (this.panelHeight - this.outerRadius)) {
- //if the shape is moving down and we hit the bottom
- isMovingDown = false; //start moving up
- } else if (!isMovingDown && getCenterY() < 0) {
- isMovingDown = true; //start moving down
- }
- if (isMovingDown) {
- moveDown();
- } else {
- moveUp();
- }
- }
- //GETTERS AND SETTERS:
- public int getCenterX() {
- return centerX;
- }
- public void setCenterX(int centerX) {
- this.centerX = centerX;
- }
- public int getCenterY() {
- return centerY;
- }
- public void setCenterY(int centerY) {
- this.centerY = centerY;
- }
- public int getOuterRadius() {
- return outerRadius;
- }
- public void setOuterRadius(int outerRadius) {
- this.outerRadius = outerRadius;
- }
- public Color getColor() {
- return color;
- }
- public void setColor(Color color) {
- this.color = color;
- }
- public void draw(int angleDegrees, Graphics page) {
- Polygon star = new Polygon();
- double angleRadians = (int) (angleDegrees*Math.PI / 180);
- double innerRadius = outerRadius/2;
- final double THIRTY_SIX_DEGREES = (36*Math.PI/180);
- for (int i = 0; i < 5; i++) {
- double x = centerX + (outerRadius*Math.cos(angleRadians));
- double y = centerY + (outerRadius*Math.sin(angleRadians));
- star.addPoint((int) x, (int) y);
- angleRadians += THIRTY_SIX_DEGREES;
- double inX = centerX + (innerRadius*Math.cos(angleRadians));
- double inY = centerY + (innerRadius*Math.sin(angleRadians));
- star.addPoint((int)inX, (int) inY);
- angleRadians += THIRTY_SIX_DEGREES;
- } //END MAIN LOOP
- page.setColor(color);
- page.fillPolygon(star);
- } // END DRAW
- } // END CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement