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

 Riven (28 views) 2015-04-16 10:48:47 Duke0200 (42 views) 2015-04-16 01:59:01 Fairy Tailz (32 views) 2015-04-14 20:13:12 Riven (33 views) 2015-04-12 21:36:37 bus hotdog (49 views) 2015-04-10 02:39:32 CopyableCougar4 (51 views) 2015-04-10 00:51:04 BurntPizza (51 views) 2015-04-06 22:06:58 ags1 (53 views) 2015-04-02 10:58:48 Riven (52 views) 2015-04-01 18:27:05 ags1 (69 views) 2015-03-31 10:55:12
 theagentd 26x BurntPizza 17x wessles 15x 65K 11x kingroka123 11x Rayvolution 11x alwex 10x KevinWorkman 9x kevglass 8x phu004 8x Roquen 7x chrislo27 7x Ecumene 7x Hanksha 7x SHC 7x ra4king 7x
 How 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:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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