Jump to content

Cropping an image to a percentage?


Recommended Posts

Posted (edited)

What im aiming for is similar to below :

332nr40.jpg

 

1 image - but the total width is cropped depending on the percentage.

 

i have percent to next level as so.

 

percentTNL = ((currentXp - currentLevelXp) / (nextLevelXp - currentLevelXp) * 100);

 

But i dont know how to crop the image to have the percent age D:

 

Sorry for being a nub :3

Edited by Fruity
Posted (edited)

try this

 

Rectangle bounds = new Rectangle(Whatever bounds you want to crop to on the game screen);

int dx1 = bounds.x;

int dy1 = bounds.y;

int dx2 = bounds.x + bounds.width;

int dy2 = bounds.y + bounds.height;

int sx2 = bounds.width;

int sy2 = bounds.height;

g.drawImage(image, dx1, dy1, dx2, dy2, 0, 0, sx2, sy2, null, null);

 

You can calculate the bounding rectangle by having a pre-defined "full" (100%) rectangle, and transform its width according to your percentage variable.

Edited by FrostBug
  • Like 1
Posted

try this

 

Rectangle bounds = new Rectangle(Whatever bounds you want to crop to on the game screen);

int dx1 = bounds.x;

int dy1 = bounds.y;

int dx2 = bounds.x + bounds.width;

int dy2 = bounds.y + bounds.height;

int sx2 = bounds.width;

int sy2 = bounds.height;

g.drawImage(image, dx1, dy1, dx2, dy2, 0, 0, sx2, sy2, null, null);

 

You can calculate the bounding rectangle by having a pre-defined "full" (100%) rectangle, and transform its width according to your percentage variable.

 

Thank you this worked :)<3

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...