Advertisement
boxglue

ImageRect (goes with BouncingImages2)

Apr 30th, 2020
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package bouncer2;
  2. import java.awt.Color;
  3. import java.awt.Point;
  4. import java.awt.Rectangle;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import javax.imageio.ImageIO;
  9.  
  10.  
  11. //NOTE: This class requires BouncingImages.java
  12.  
  13. /* This class combines an image with a rectangle
  14.  * which allows for easy movement and collision detection
  15.  */
  16. class ImageRect extends Rectangle{
  17.     //this gives it an x,y,width,height
  18.  
  19.     static int biggestDim = 0;
  20.    
  21.     BufferedImage image;
  22.     Color color = Color.RED;
  23.     //these are the speeds of movement
  24.     int vx=1;
  25.     int vy=1;
  26.     int lastx=-1;
  27.     int lasty=-1;
  28.  
  29.     Point ctr, tl, tr, bl, br, ml, mr,mt, mb;
  30.  
  31.     ImageRect(String filename) {
  32.        
  33.         vx = (int)(Math.random()*3+1);
  34.        
  35.         //load the image
  36.         try {
  37.             image = ImageIO.read(new File(filename));
  38.             width = image.getWidth(null);
  39.             height = image.getHeight(null);
  40.         } catch (IOException e) {
  41.             System.out.println("ERROR: image file \"" + filename + "\" not found");
  42.             //e.printStackTrace();
  43.             BouncingImages2.imagesLoaded = false;
  44.             width = 100 + (int)(Math.random()*100);
  45.             height= width - (int)(Math.random()*70);
  46.             color = Color.getHSBColor ((float) Math.random (), 1.0f, 1.0f);    // a quick way to get random colours
  47.             //System.exit(0);          
  48.         }
  49.        
  50.         //update the variable containing the biggest dimension
  51.         if (width > biggestDim) biggestDim = width;
  52.         if (height > biggestDim) biggestDim = height;
  53.  
  54.         calculatePoints();
  55.     }
  56.  
  57.     void calculatePoints() {
  58.         //Calculate points
  59.         //corners
  60.         tl = new Point(x,y);
  61.         tr = new Point(x+width,y);
  62.         bl = new Point(x,y+height);
  63.         br = new Point(x+width,y+height);
  64.         //center
  65.         ctr = new Point(x+width/2, y+ height/2);
  66.         //mid points of sides
  67.         ml = new Point(x, y+ height/2);
  68.         mr = new Point(x+width, y+height/2);
  69.         mt = new Point(x+width/2,y);
  70.         mb = new Point(x+width/2,y+height);
  71.     }
  72.  
  73.     void moveImage(){
  74.         lastx = x;
  75.         lasty = y;
  76.         x += vx;
  77.         y += vy;
  78.         if (x < 0 && vx < 0) {
  79.             x = 0;
  80.             vx = -vx;
  81.         }
  82.         if (y < 0 && vy < 0) {
  83.             y = 0;
  84.             vy = -vy;
  85.         }
  86.         if(x + width > BouncingImages2.WINW && vx > 0) {
  87.             x = BouncingImages2.WINW-width;
  88.             vx = -vx;
  89.         }
  90.         if(y + height > BouncingImages2.WINH && vy > 0) {
  91.             y = BouncingImages2.WINH-height;
  92.             vy = -vy;
  93.         }
  94.     }
  95.    
  96.     void undoMove() {
  97.         if (lastx > 0) {
  98.             x = lastx;
  99.             y = lasty;
  100.         }
  101.         lastx = lasty = -1;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement