Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Displaying 100% from a number?  (Read 1307 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2009-09-17 22: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);
       }
Offline JL235

JGO Coder


Medals: 10



« Reply #1 - Posted 2009-09-18 00: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);

Offline CyanPrime
« Reply #2 - Posted 2009-09-18 05: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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JL235

JGO Coder


Medals: 10



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

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

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

Ah, alright. thanks.  Grin
Offline JL235

JGO Coder


Medals: 10



« Reply #5 - Posted 2009-09-18 08: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.

Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #6 - Posted 2009-09-18 09: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  Smiley)

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

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2009-09-18 16: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
Offline CyanPrime
« Reply #8 - Posted 2009-09-18 21: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (12 views)
2014-07-29 18:09:19

Riven (9 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (26 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!