Jump to content

Cropping an image to a percentage?


Fruity

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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