NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on August 08, 2007, 09:37:27 am



Title: how to make a variable number appear in the drawing panel?
Post by: lookang on August 08, 2007, 09:37:27 am
Hi prof. Hwang,

i am thinking if EJS can do this, it will be so impressive.

(http://)

to show the value of the variable inside drawing panel.


Title: Re: how to make a variable number appear in the drawing panel?
Post by: Fu-Kwun Hwang on August 08, 2007, 10:16:47 am
It can be done easily. Define a "String" s to show the value for variable v(double) and add a String GUI element into DrawingPanel.
You can set up the x,y coordinate for the String (to where you want the string to be displayed!).
Add the following code s=(int)(v*1000.+0.5)/1000.+""; will display values as you want in the previous message.
(if v<0 then change 0.5 to -0.5)  ;D


Title: Re: how to make a variable number appear in the drawing panel?
Post by: lookang on August 09, 2007, 09:39:01 pm
cool :)



s=(int)(vx*100.+0.5)/100.+" "       // what does it means ?




i keyed into the interactive text and it works fine

but how to show the text vx =

before the variable  s

so that the output looks like

vx = 2.3


i attached my file so that you can understand

thanks


Title: Re: how to make a variable number appear in the drawing panel?
Post by: Fu-Kwun Hwang on August 10, 2007, 08:47:03 am
All the properties in EJS's GUI elements is links to variables defined in the model.
If s the the "String" type variable you want to show, you can add s=(int)(vx*100.+0.5)/100.+" " ; in the constraint page. 
You should set the property for text to be %s%. (If you set it to s, it means to display "s" instead of the contain of variable to s). If you click the button next to the box, EJS will display the possible variables can be used. You can select one of them, and ejs will add it correctly.

If you want to show vx=2.3, you need to add another String variable (to stored the string to be displayed).
For example : define another variable called s_vx. and the variable for velocity is vx(double).
Add s_vx=(int)(vx*10+0.5)/10.+""; will give you vx=2.3

If you just add s_vx=vx+""; No error message will be shown , but the display might look like 2.3000000001
I think you can get the point.
If you use it many times, I would suggest you define a function at "Custom Page"

double dv;
public String double2String(double v){
 if(v>0)dv=0.5;
 else if(dv<0)dv=-0.5;
 else dv=0;
 return (int)(v*10.+dv)/10.+"";
}
 or
public String double2String(double v,double divider){
 if(v>0)dv=0.5;
 else if(dv<0)dv=-0.5;
 else dv=0;
 return (int)(v*devider+dv)/devider+"";
}

Them called it with s_vx=double2String(vx,10.); or s=double2String(vx,100.); ...


Title: Re: how to make a variable number appear in the drawing panel?
Post by: lookang on August 10, 2007, 11:00:45 am
1. Cool trick: %%

i understand the meaning of %s_vx%    // to show the value stored in variable s_vx


2. cool trick (int)(vx*10+0.5)/10.+"";

s_vx=(int)(vx*10+0.5)/10.+"";   // to show s_vx as a number say 2.3 instead of 2.3000001


3. my attempt to show 2.3 = vx

constraints page:
s_vx =(int)(vx*100.+0.5)/100.+"=vx" ; // to make display 2.30=vx instead of 2.3000000001

property text
%s_vx%

result is still ok  it shows     2.3 =vx


Question:
can show example codes to show vx= 2.3 ??

i read your post, i can't understand this part clearly to implement it the way i want  :(



the function is fantastic !! i corrected the typo errors :)

cut and paste in custom page:

double dv;
public String double2String(double v,double divider){
 if(v>0)dv=0.5;
 else if(v<0)dv=-0.5;
 else dv=0;
 return (int)(v*divider+dv)/divider+"";
}


cut and paste in constraint page:

s_vx=double2String(vx,10.);


Title: Re: how to make a variable number appear in the drawing panel?
Post by: lookang on August 10, 2007, 12:59:01 pm

how to automatically add the text in ? My syntax wrong 

Custom Page:

double dv;
public String double2String(double v,double divider,double text ){
 if(v>0)dv=0.5;
 else if(v<0)dv=-0.5;
 else dv=0;
 return (int)(v*divider+dv)/divider+"text"+"";
}


Constraint Page:

s_vx=double2String(vx,100.,vx );



Title: Re: how to make a variable number appear in the drawing panel?
Post by: Fu-Kwun Hwang on August 10, 2007, 01:31:45 pm
The function you have defined make no sense to me. I do not know what you really want to do.
Why you need to display the same value twice?


If you type "text", it mean a string with value "text";
However, text is a double when you declare the function.
It was never been used in your function.
And the screen shown what you asked for, add "text" after the displayed alue.

for example: if you defined a string variable called unit.
And set unit="cm";
Then return (int)(v*divider+dv)/divider+unit;
will add "cm" after the displayed value.

The type of (int)(v*divider+dv)/divider  is integer.
When I type (int)(v*divider+dv)/divider+"",
add integer and string together do not make sense, so the program change the integer to string and add(concate) two strings together.


If you want to show "vx= 2.3 ??" in the simulation
change the code to (int)(vx*100.+0.5)/100.+"=vx ??"
will do what you wanted. 


Title: Re: how to make a variable number appear in the drawing panel?
Post by: lookang on August 10, 2007, 02:22:46 pm
thanks prof. !

 i understand how the java snytax works now

cut and paste:

into constraints page:

s_vx ="vx="+(int)(vx*100.+0.5)/100.+"m/s" ;

into view property text

%s_vx%

Thanks for help,

When you come to sg can meet up with me ? :) i need to buy coffee. really learnt alot from you and this forum.


Title: Re: how to make a variable number appear in the drawing panel?
Post by: lookang on August 10, 2007, 02:45:50 pm
in summary for the benefit of the rest in the forum learning EJS like me.

Model Variables
s_vx    declared as string

Model constraints
s_vx ="vx="+double2String(vx,100.)+"m/s" ;

Model custom                      ;D cut and paste the codes ;D

double dv;
public String double2String(double v,double divider){
 if(v>0)dv=0.5;
 else if(v<0)dv=-0.5;
 else dv=0;
 return (int)(v*divider+dv)/divider+"";
}

View property for text  text(http://)

%s_vx%


Title: Re: how to make a variable number appear in the drawing panel?
Post by: Fu-Kwun Hwang on August 10, 2007, 03:01:23 pm
You can change the "Position" property to change the alignment of text related to the (x,y) property.
(i.e. Change the control point position).

In some case, you can also rotate text. (check out Polarizer (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=497.0))

I conducted a workshop about EJS in Singapore(Chinese high school) at the year 2001.
And I conducted another 3 days workshop about EJS at Univ. of Malaysia last month (July 16-18,2007).
I do not know when I will go to Singapore again. If someone do invited me over again, I will let you know.


Title: Re: how to make a variable number appear in the drawing panel?
Post by: lookang on February 24, 2009, 01:32:50 pm
Hi prof, sori to bother you again about this but i can't figure out for long time about this.

I have been using this custom function
double dv;
public String double2String(double v,double divider){
 if(v>0)dv=0.5;
 else if(v<0)dv=-0.5;
 else dv=0;
 return (int)(v*divider+dv)/divider+"";
}

// this code has to do with displaying the number 2.3 instead of 2.300000001 when using % %


l_n = double2String(i,1); // added to trace array of balls

The output is 1.0 , 2.0 , 3.0 and so on,

what must i do to the codes to show 1 , 2 , 3 and so on?

i thought i did it before but i just can't get it to work :)



Title: Re: how to make a variable number appear in the drawing panel?
Post by: Fu-Kwun Hwang on February 24, 2009, 04:16:18 pm
1,2,3 are all interger.
If v is a double variable, use
(int)(v)   // 2.3, 2.9 all become 2
or
(int)(v+0.5) // if v>0  // 2.3 become 2  2.5,2.7 become 3 (variable >=2.5 become 3, <2.5 become 2)
(int)(v-0.5) // if v<0
will give you integer value.

(int) will convert double to integer value.


Title: Re: how to make a variable number appear in the drawing panel?
Post by: lookang on February 24, 2009, 04:25:10 pm
okay! ic........ understand what you are explaining.......... o it explains why the need for the
Quote
(int)(v+0.5) // if v>0  // 2.3 become 2  2.5,2.7 become 3 (variable >=2.5 become 3, <2.5 become 2)
(int)(v-0.5) // if v<0
will give you integer value.

I did a little trial and error

use i replace i with j for typing here

l_n[j] = ""+j; // This is the code i was thinking of  ;D

because i or j is already an integer

but l_n[j] is a string, so i added the  "" and add a plus j, to make the variable j into a string

now it works  ;D Thanks!


Title: Re: how to make a variable number appear in the drawing panel?
Post by: Fu-Kwun Hwang on February 24, 2009, 04:45:54 pm
Yes. You need to add "" to convert interger into String type.
You can use (String)((int)v), too!
But (int)(v)+""; is much easier!  ;)