NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
November 20, 2017, 07:36:14 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
You cannot always have happiness but you can always give happiness. ..."Mother Teresa(1910-1997, Roman Catholic Missionary, 1979 Nobel Peace Prize)"
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

Pages: [1]   Go Down
  Print  
Author Topic: how to make a variable number appear in the drawing panel?  (Read 15647 times)
0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
on: August 08, 2007, 09:37:27 am » posted from:Singapore,,Singapore

Hi prof. Hwang,

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



to show the value of the variable inside drawing panel.
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #1 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)  Grin
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #2 on: August 09, 2007, 09:39:01 pm » posted from:Singapore,,Singapore

cool Smiley



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
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #3 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.); ...
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #4 on: August 10, 2007, 11:00:45 am » posted from:Singapore,,Singapore

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  Sad



the function is fantastic !! i corrected the typo errors Smiley

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.);
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #5 on: August 10, 2007, 12:59:01 pm » posted from:Singapore,,Singapore


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 );

Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #6 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. 
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #7 on: August 10, 2007, 02:22:46 pm » posted from:Singapore,,Singapore

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 ? Smiley i need to buy coffee. really learnt alot from you and this forum.
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #8 on: August 10, 2007, 02:45:50 pm » posted from:Singapore,,Singapore

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    Grin cut and paste the codes Grin

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

%s_vx%
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #9 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)

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.
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #10 on: February 24, 2009, 01:32:50 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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 Smiley



* SS-20090224143209.png (24.33 KB, 495x650 - viewed 373 times.)
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #11 on: February 24, 2009, 04:16:18 pm » posted from:Taipei,T'ai-pei,Taiwan

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.
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #12 on: February 24, 2009, 04:25:10 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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  Grin

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  Grin Thanks!


* SS-20090224172457.png (23.62 KB, 489x656 - viewed 351 times.)
« Last Edit: February 24, 2009, 04:30:01 pm by lookang » Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #13 on: February 24, 2009, 04:45:54 pm » posted from:Taipei,T'ai-pei,Taiwan

Yes. You need to add "" to convert interger into String type.
You can use (String)((int)v), too!
But (int)(v)+""; is much easier!  Wink
Logged
Pages: [1]   Go Up
  Print  
You cannot always have happiness but you can always give happiness. ..."Mother Teresa(1910-1997, Roman Catholic Missionary, 1979 Nobel Peace Prize)"
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
how to make x and y axis automatically on the drawing panel
Questions related to EJS
lookang 4 13288 Last post August 10, 2007, 02:35:48 pm
by lookang
is it possible to make applet make a sound when i condition is met?
Questions related to EJS
lookang 8 10298 Last post December 22, 2008, 12:10:01 pm
by lookang
how to make a Ejs way to recognize that a number is 10,100,1000 etc
Questions related to EJS
lookang 3 5648 Last post March 30, 2009, 10:37:50 am
by Fu-Kwun Hwang
Dynamics With Variable Mass
Question related to Physics or physics related simulation
j142 5 11061 Last post May 31, 2009, 09:46:22 pm
by Fu-Kwun Hwang
Let user enter Mathematics function and variable ,then evaluate it's value
misc
Fu-Kwun Hwang 0 13393 Last post December 19, 2011, 09:24:54 pm
by Fu-Kwun Hwang
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.61 seconds with 22 queries.since 2011/06/15