Basically, if you have ever used a BufferedImage you will notice that it's possible to use this method;
getSubimage(int x, int y, int w, int h)
Returns a subimage defined by a specified rectangular region.
So i decided to make one for the sprite class.
Here it is;
Code:
public Sprite getSubImage(Sprite sprite, int sX, int sY, int w, int h)
{
try {
int[] pixels = new int[w * h];
int pixOn = 0;
for (int i = sX; i < w; i++)
{
for (int i1 = sY; i1 < h; i1++)
{
int i2 = i - sX;
int i3 = i1 - sY;
pixels[i2 + i3 * w] = sprite.myPixels[i + i1 * sprite.myWidth];
}
}
sprite.myHeight = h - sY;
sprite.myWidth = w - sX;
sprite.myPixels = pixels;
return sprite;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
Basically the usage is;
Code:
mySprite = mySprite.getSubImage(mySprite, startX, startY, pixelWidth, pixelHeight);
You could also do;
Code:
Sprite newSprite = mySprite.getSubImage(mySprite, startX, startY, pixelWidth, pixelHeight);
Which would create a new sprite from the previous one.
Here is an example of using it for the hitpoints bar;
Code:
if(((Entity) (obj)).loopCycleStatus > loopCycle)
{
try {
npcScreenPos(((Entity) (obj)), ((Entity) (obj)).height + 15);
if(spriteDrawX > -1)
{
int i1 = (((Entity) (obj)).currentHealth * 30) / ((Entity) (obj)).maxHealth;
if(i1 > 30)
i1 = 30;
Sprite emptyBar = new Sprite("empty.png");
Sprite fullBar = new Sprite("full.png");
fullBar.drawSprite(spriteDrawX - 15, spriteDrawY - 3);
emptyBar = emptyBar.getSubImage(emptyBar, 0, 0, (30 - i1), emptyBar.myHeight);
emptyBar.drawSprite(spriteDrawX - 15, spriteDrawY - 3);
}
} catch(Exception e) {
e.printStackTrace();
}
}
Using these 2 sprites;
FULL:

EMPTY:

I get this output;

But the images don't have to be simple, you could do this for loading bar's etc aswell.
Have fun.
It also works with transparency from method drawSprite1.