Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Displaying 100% from a number?  (Read 2677 times) 0 Members and 1 Guest are viewing this topic.
CyanPrime
 « Posted 2009-09-17 20:59:38 »

Okay, so I've got  two variables that I need to know what percent they are. I got a bar that goes up to 100, and the first variable is 120, and the second one is (100 - 60) it's the 60 in that equation. Anyway, how do I show them from only 1 to 100?

 1  2  3  4  5  6  7  8  9  10  11  12  13 `//draw particle bar background       g.setColor(new Color(0,0,255));       g.fillRect(10, 170, 100, 5);              if(player.particleMode){          g.setColor(new Color(0,255,255));           g.fillRect(10, 170, player.particleTime, 5);       }              else{              g.setColor(new Color(255,0,0));               g.fillRect(10, 170, 100 - player.particleDelay, 5);       }`
JL235

JGO Coder

Medals: 10

 « Reply #1 - Posted 2009-09-17 22:04:43 »

This code should do it for you:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28 `/** * The same as the other percent, only it presumes the range is from 0 to * max. *  * @param val The value to calculate the percentage of from the range. * @param max The maximum value in the range. */public float percent(float val, float max){    return percent(val, 0, max);}/** * This takes a range (defined by min and max) and return what percentage * the given val takes in that range. The percent is returned as a decimal * value where 0 is 0% and 1 is 100%. *  * For example the percentage of 150 in teh range 100 to 200 will return * 0.5 (50%). *  * @param val The value to get the percentage of. * @param min The minimum value in the range. * @param max The maximum value of the range. */public float percent(float val, float min, float max){    return (val-min) / (max-min);}`

I don't quite understand the range of values your converting, but you want the percentage of player.particleTime between 60 and 120? Then it's:
 1  2 `    float percent = percent( player.particleTime, 60, 120 );    int width = (int) (100 * percent);`

CyanPrime
 « Reply #2 - Posted 2009-09-18 03:22:46 »

When the counter is at 120 I need a variable to say 100. When it's at 60 I need a variable to say 50.
JL235

JGO Coder

Medals: 10

 « Reply #3 - Posted 2009-09-18 04:34:52 »

 1  2 `    float percent = percent( counter, 120 );    int percentOf100 = (int) (100 * percent);`

CyanPrime
 « Reply #4 - Posted 2009-09-18 05:22:25 »

Ah, alright. thanks.
JL235

JGO Coder

Medals: 10

 « Reply #5 - Posted 2009-09-18 06:59:24 »

No problem.

I know it's tiny but if you (or anyone else) uses the code directly then please add an url to this topic in the method headers, just so it's credited.

Hansdampf

Senior Devvie

Projects: 3

too offending?

 « Reply #6 - Posted 2009-09-18 07:14:33 »

Quote
public float percent(float val, float min, float max)
{
return (val-min) / (max-min);
}

you need to check if min==max.

Oh, please add an extra url to this post in the method headers if you include this check, just so it's extra credited.
(just kidding  )

lots of sillystupid games: http://www.emaggame.com
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #7 - Posted 2009-09-18 14:42:36 »

Just so you don't have to provide a URL, you can use mine:

 1  2  3  4  5  6  7 `public void draw(){     float clipX = (value - minimum) / (maximum - minimum);     clipX = Math.min(clipX, 1.0f);     clipX = Math.max(clipX, 0.0f);     g.fillRect(x, y, width * clipX, height);}`

Yes I know it's the same thing, but it's not like this "algorithm" is secret or anything. It's just simple arithmetic. I can quote at least 10 places in my own games where I've used it, except obvious I didn't bother to make a getPercent() method in any of them, I just calculate what I need when I need it, as it's a simple operation.

See my work:
OTC Software
CyanPrime
 « Reply #8 - Posted 2009-09-18 19:43:18 »

No problem.

I know it's tiny but if you (or anyone else) uses the code directly then please add an url to this topic in the method headers, just so it's credited.
To be honest, I was just saying that to be nice >_>
I ended up using a simple (float) (var * (var/100)) in my drawing.
Pages: [1]
 ignore  |  Print

 ivj94 (579 views) 2018-03-24 14:47:39 ivj94 (42 views) 2018-03-24 14:46:31 ivj94 (346 views) 2018-03-24 14:43:53 Solater (59 views) 2018-03-17 05:04:08 nelsongames (104 views) 2018-03-05 17:56:34 Gornova (122 views) 2018-03-02 22:15:33 buddyBro (665 views) 2018-02-28 16:59:18 buddyBro (89 views) 2018-02-28 16:45:17 xxMrPHDxx (491 views) 2017-12-31 17:17:51 xxMrPHDxx (727 views) 2017-12-31 17:15:51
 philfrei 12x KaiHH 10x SHC 10x NuclearPixels 10x Zemlaynin 10x ByerN 7x Guerra2442 6x Spasi 6x ags1 4x orangepascal 4x theagentd 4x VaTTeRGeR 4x kreagoth 3x chrislo27 3x mesterh 3x ndnwarrior15 3x
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org