Question

I'm trying to get the length and width of an image to create a collision detection area for a basic game. I hope in the end to move it onto android, so I'm avoiding use of the BufferedImage class (which android doesn't seem to have). Instead, I want to read a .png from an assets folder within my Eclipse project, and hand it to a sprite class. All sprites will inherit from ASprite(), which contains a HitBox() class:

import java.awt.Image;
import java.awt.image.ImageObserver;


public class HitBox {

    private int TopLeftX;
    private int TopLeftY;
    private int Width;
    private int Length;

    public void createManually(int topx, int topy, int width, int length){
        TopLeftX = topx;
        TopLeftY = topy;
        Width = width;
        Length = length;
    }

    public void createFromImage(Image i, int topx, int topy){
          //TODO...
    }

   //Various getting and setting functions, & testIntersect(HitBox h1)

}

For Sprites that I want to have a custom collision area, I can manually pass in the diameters for the HitBox(). For the rest, I want to be able to take in an image and an X and Y position for the top left corner, and extract the height and width of the image into variables to define the hitbox. I think it will involve using ImageObserver, but I'm having some trouble understanding how to do it. I hope someone can help.

Was it helpful?

Solution

I suggest you to write an interface.Lets call it ImageInterface. Now in image interface you can have all basic image related stuff like getWitdh(), getHeight(), getPixelValue(x,y),draw() etc. Use that interface in all of your project. And implement it seperately for cros platforms. You can use BufferedImage for java and Bitmap for Android.

OTHER TIPS

I'm not sure I'm understanding exactly what you want, but if you are say using Bitmap objects to hold your images, you can simply get the image W/H like this:

// Load an image from the drawable folder.
Bitmap tempBitmap = BitmapFactory.decodeResource(myContext.getResources(), R.drawable.<nameofimageinresources>);
// get w and h of it and put it in vars.
<put it in some vars> = tempBitmap.getWidth();
<put it in some vars> = tempBitmap.getHeight();

Or what is it your looking for?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top