Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bouncer2;
- import java.awt.Color;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- //NOTE: This class requires BouncingImages.java
- /* This class combines an image with a rectangle
- * which allows for easy movement and collision detection
- */
- class ImageRect extends Rectangle{
- //this gives it an x,y,width,height
- static int biggestDim = 0;
- BufferedImage image;
- Color color = Color.RED;
- //these are the speeds of movement
- int vx=1;
- int vy=1;
- int lastx=-1;
- int lasty=-1;
- Point ctr, tl, tr, bl, br, ml, mr,mt, mb;
- ImageRect(String filename) {
- vx = (int)(Math.random()*3+1);
- //load the image
- try {
- image = ImageIO.read(new File(filename));
- width = image.getWidth(null);
- height = image.getHeight(null);
- } catch (IOException e) {
- System.out.println("ERROR: image file \"" + filename + "\" not found");
- //e.printStackTrace();
- BouncingImages2.imagesLoaded = false;
- width = 100 + (int)(Math.random()*100);
- height= width - (int)(Math.random()*70);
- color = Color.getHSBColor ((float) Math.random (), 1.0f, 1.0f); // a quick way to get random colours
- //System.exit(0);
- }
- //update the variable containing the biggest dimension
- if (width > biggestDim) biggestDim = width;
- if (height > biggestDim) biggestDim = height;
- calculatePoints();
- }
- void calculatePoints() {
- //Calculate points
- //corners
- tl = new Point(x,y);
- tr = new Point(x+width,y);
- bl = new Point(x,y+height);
- br = new Point(x+width,y+height);
- //center
- ctr = new Point(x+width/2, y+ height/2);
- //mid points of sides
- ml = new Point(x, y+ height/2);
- mr = new Point(x+width, y+height/2);
- mt = new Point(x+width/2,y);
- mb = new Point(x+width/2,y+height);
- }
- void moveImage(){
- lastx = x;
- lasty = y;
- x += vx;
- y += vy;
- if (x < 0 && vx < 0) {
- x = 0;
- vx = -vx;
- }
- if (y < 0 && vy < 0) {
- y = 0;
- vy = -vy;
- }
- if(x + width > BouncingImages2.WINW && vx > 0) {
- x = BouncingImages2.WINW-width;
- vx = -vx;
- }
- if(y + height > BouncingImages2.WINH && vy > 0) {
- y = BouncingImages2.WINH-height;
- vy = -vy;
- }
- }
- void undoMove() {
- if (lastx > 0) {
- x = lastx;
- y = lasty;
- }
- lastx = lasty = -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement