Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bouncer;
- 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 MyImage extends Rectangle{
- //this gives it an x,y,width,height
- BufferedImage image;
- //these are the speeds of movement
- int dx=1;
- int dy=1;
- MyImage(String filename) {
- //load the image
- try {
- image = ImageIO.read(new File(filename));
- } catch (IOException e) {
- System.out.println("ERROR: image file \"" + filename + "\" not found");
- //e.printStackTrace();
- System.exit(0);
- }
- //image = (BufferedImage) new ImageIcon(filename).getImage(); Does not work with BufferedImage
- width = image.getWidth(null);
- height = image.getHeight(null);
- }
- void moveImage(){
- x += dx;
- y += dy;
- if (x < 0 && dx < 0) {
- x = 0;
- dx = -dx;
- }
- if (y < 0 && dy < 0) {
- y = 0;
- dy = -dy;
- }
- if(x + width > BouncingImages.WINW && dx > 0) {
- x = BouncingImages.WINW-width;
- dx = -dx;
- }
- if(y + height > BouncingImages.WINH && dy > 0) {
- y = BouncingImages.WINH-height;
- dy = -dy;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement