We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › calculating in a class
Page Index Toggle Pages: 1
calculating in a class (Read 502 times)
calculating in a class
Jan 12th, 2010, 7:56am
 
Hello,

I am a beginner at programming and I am trying to build a class for multiple graphic bars that visualize values. I have written the following code:

Graphbar Graphbar1;

void setup() {
 size(500,500);
 
 PFont font;
 font=loadFont("Tahoma-12.vlw");
 textFont(font);
 Graphbar1= new Graphbar(60,60,"Stroom:","A",180,200);
}

void draw() {
 background(150);
 Graphbar1.DisplayPhysScale();
 Graphbar1.DisplayBar();
 Graphbar1.DisplayUnit();
}
class Graphbar{
 int Xpos;
 int Ypos;
 String PhysScale;
 String Unit;
 int Value;
 int Range;
   
 Graphbar(int tempXpos, int tempYpos,String tempPhysScale, String tempUnit, int tempValue, int tempRange ){
   Xpos=tempXpos;
   Ypos=tempYpos;
   PhysScale=tempPhysScale;
   Unit=tempUnit;
   Value=tempValue;
   Range=tempRange;
 
 }
 void DisplayPhysScale(){
   fill(97,118,219);
   stroke(97,118,150);
   rect(Xpos,Ypos,50,20);
   fill(0,18,106);
   text("Stroom:",Xpos+2,Ypos+15);
 }
 
 void DisplayBar(){
   fill(97,118,219);
   stroke(97,118,150);
   rect(Xpos+52,Ypos,210,20);
   fill(188,193,219);
   stroke(0);
   rect(Xpos+57,Ypos+5,200,10);
   fill(0,18,106);
   stroke(0,18,156);
   int x=Value/Range*200;
   rect(Xpos+58,Ypos+6,x,8);
 }
 
 void DisplayUnit(){
   fill(97,118,219);
   stroke(97,118,150);
   rect(Xpos+264,Ypos,40,20);
   fill(0,18,106);
   text(Value,Xpos+267,Ypos+15);
   text(Unit,Xpos+290,Ypos+15);
 }
}

Variable x is declared as a integer and needs to calculate the following:
          Value
int x= --------- x 200
          Range

The problem is that the outcome is always 0 while in my opinion int Value =180 and int Range=200, am I wrong?

Thanks,

Willem

Re: calculating in a class
Reply #1 - Jan 12th, 2010, 8:03am
 
integer division problem

180 / 200 = .9

the integer value of .9 is 0.

use floats instead.
Re: calculating in a class
Reply #2 - Jan 12th, 2010, 8:10am
 
look at http://processing.org/reference/troubleshooting/#integers
Page Index Toggle Pages: 1