Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Polygon;
  4. import java.util.Random;
  5.  
  6. //Jacob Baker
  7. //Lab-08
  8. //Star.java
  9.  
  10. public class Star {
  11.     private int centerX;
  12.     private int centerY;
  13.     private int outerRadius;
  14.     private Color color;
  15.    
  16.     private boolean isMovingRight;
  17.     private boolean isMovingDown;
  18.     private int speed;
  19.    
  20.     private int panelWidth, panelHeight;
  21.    
  22.     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
  23.         this.centerX = centerX;
  24.         this.centerY = centerY;
  25.         this.outerRadius = innerRadius*2;
  26.        
  27.         this.color = color;
  28.     }
  29.    
  30.    
  31.     public Star(int panelWidth, int panelHeight) {
  32.         this.panelWidth = panelWidth;
  33.         this.panelHeight = panelHeight;
  34.        
  35.         this.color = Color.yellow;
  36.        
  37.         Random rand = new Random();
  38.        
  39.         this.outerRadius = ((int) panelWidth/48) + 10;
  40.        
  41.         this.centerX = rand.nextInt(panelWidth - outerRadius);
  42.         this.centerY = rand.nextInt(panelHeight - outerRadius);
  43.        
  44.         int r = rand.nextInt(2); // select 0 or 1 randomly
  45.         if (r==0) {
  46.             isMovingRight = true;
  47.         } else {
  48.             isMovingRight = false;
  49.         }
  50.        
  51.         int d = rand.nextInt(2);
  52.         if (d==0) {
  53.             isMovingDown = true;
  54.         } else {
  55.             isMovingDown = false;
  56.         }
  57.        
  58.         speed = rand.nextInt(3) + 1; // random number between 1 and 3;
  59.     }
  60.    
  61.     //ANIMATION:
  62.    
  63.     public void moveHorizontal(int deltaX) {
  64.         this.centerX += deltaX;
  65.     }
  66.    
  67.     public void moveVertical(int deltaY) {
  68.         this.centerY += deltaY;
  69.     }
  70.    
  71.     public void moveRight() {
  72.         this.moveHorizontal(speed);
  73.     }
  74.     public void moveLeft() {
  75.         this.moveHorizontal(-speed);
  76.     }
  77.     public void moveDown() {
  78.         this.moveVertical(speed);
  79.     }
  80.     public void moveUp() {
  81.         this.moveVertical(-speed);
  82.     }
  83.    
  84.     public void move() {
  85.         if (isMovingRight && (getCenterX() > (this.panelWidth - this.outerRadius))) {
  86.             //if the shape is moving right and I just hit the edge
  87.             isMovingRight = false; // start moving left
  88.         } else if (!isMovingRight && getCenterX() < 0) {
  89.             //if the shape is moving left and just hit the edge
  90.             isMovingRight = true;
  91.         }
  92.        
  93.         //if we're not bumping into an edge yet
  94.         if (isMovingRight) {
  95.             moveRight();
  96.         } else {
  97.             moveLeft();
  98.         }
  99.        
  100.         //now up and down
  101.         if (isMovingDown && getCenterY() > (this.panelHeight - this.outerRadius)) {
  102.             //if the shape is moving down and we hit the bottom
  103.             isMovingDown = false; //start moving up
  104.         } else if (!isMovingDown && getCenterY() < 0) {
  105.             isMovingDown = true; //start moving down
  106.         }
  107.        
  108.         if (isMovingDown) {
  109.             moveDown();
  110.         } else {
  111.             moveUp();
  112.         }
  113.        
  114.     }
  115.    
  116.     //GETTERS AND SETTERS:
  117.    
  118.     public int getCenterX() {
  119.         return centerX;
  120.     }
  121.     public void setCenterX(int centerX) {
  122.         this.centerX = centerX;
  123.     }
  124.     public int getCenterY() {
  125.         return centerY;
  126.     }
  127.     public void setCenterY(int centerY) {
  128.         this.centerY = centerY;
  129.     }
  130.     public int getOuterRadius() {
  131.         return outerRadius;
  132.     }
  133.     public void setOuterRadius(int outerRadius) {
  134.         this.outerRadius = outerRadius;
  135.     }
  136.     public Color getColor() {
  137.         return color;
  138.     }
  139.     public void setColor(Color color) {
  140.         this.color = color;
  141.     }
  142.    
  143.    
  144.    
  145.     public void draw(int angleDegrees, Graphics page) {
  146.         Polygon star = new Polygon();
  147.         double angleRadians = (int) (angleDegrees*Math.PI / 180);
  148.         double innerRadius = outerRadius/2;
  149.        
  150.        
  151.         final double THIRTY_SIX_DEGREES = (36*Math.PI/180);
  152.        
  153.         for (int i = 0; i < 5; i++) {
  154.             double x = centerX + (outerRadius*Math.cos(angleRadians));
  155.             double y = centerY + (outerRadius*Math.sin(angleRadians));
  156.            
  157.             star.addPoint((int) x, (int) y);
  158.             angleRadians += THIRTY_SIX_DEGREES;
  159.            
  160.             double inX = centerX + (innerRadius*Math.cos(angleRadians));
  161.             double inY = centerY + (innerRadius*Math.sin(angleRadians));
  162.            
  163.             star.addPoint((int)inX, (int) inY);
  164.             angleRadians += THIRTY_SIX_DEGREES;
  165.  
  166.         } //END MAIN LOOP
  167.         page.setColor(color);
  168.  
  169.         page.fillPolygon(star);
  170.     } // END DRAW
  171.  
  172.  
  173. } // END CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement