Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (654) 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 1664 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

You cannot reply to this message, because it is very, very old.

 bilznatch (27 views) 2015-08-04 11:03:17 SHC (44 views) 2015-08-01 03:58:20 Jesse (25 views) 2015-07-29 04:35:27 Riven (48 views) 2015-07-27 16:38:00 Riven (26 views) 2015-07-27 15:35:20 Riven (28 views) 2015-07-27 12:26:13 Riven (19 views) 2015-07-27 12:23:39 BurntPizza (42 views) 2015-07-25 00:14:37 BurntPizza (56 views) 2015-07-24 22:06:39 BurntPizza (35 views) 2015-07-24 06:06:53
 theagentd 50x wessles 48x basil_ 32x KaiHH 26x Riven 21x mooman219 17x bornander 16x orangepascal 16x ags1 16x KudoDEV 13x klaus 11x princec 11x pquiring 11x CelestialCreator 11x israelg99 9x Spasi 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 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