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 › int/float changover
Page Index Toggle Pages: 1
int/float changover (Read 690 times)
int/float changover
Nov 9th, 2006, 2:29pm
 
Hey guys,

Ok, I'm writing a program that will allow you upon starting the applet to enter the dimensions of the window (size(x,y)) in a text box.

I also have a slider in my program which I would quite like to have adjusting its position depending on the window size. However, my only problem is that the x and y values for size must be integers and so must the slider values. But i need to half the value of x to place my slider there, by doing that it no longer is an integer but becomes a float number, which my slider will not except.

Any suggestions (and I'm sure there is something very easy I need to do) would be very much welcome, and just for your entertainment here is a snippet from my code:


int xval;  //xval is x window dimensions
int yval;  //yval is y window dimensions
int xval2; //xval2 is slider center point
int xval3; //xval3 is slider length


void setup() {
 
 String xyinput = JOptionPane.showInputDialog("Enter x and y diameters for window, seperated by spaces..");
 String[] args = split(xyinput);
 xval = int (args[0]);
 yval = int (args[1]);
 
 size(xval,yval);
 frameRate(30);
 smooth();
 
xval2 = (0.5*xval);  //trying to set slider midpoint
xval3 = (0.75*xval); //trying to set slider length
 
 
 gui = new MyGUI(this);
 slider = new MyGUIPinSlider(this, xval2, 20, xval3, 15, 0, 90);
 gui.add(slider);
 sliderval = 1;
 slider.setValue(sliderval);
Re: int/float changover
Reply #1 - Nov 9th, 2006, 3:08pm
 
You can convert any floating point value to an int by putting int( ) around it.

e.g.
Code:
float x=123.456;
int y=int(x); //y = 123
int z=int(12.34); //z=12
Re: int/float changover
Reply #2 - Nov 9th, 2006, 4:18pm
 
Many thanks

/James
Page Index Toggle Pages: 1