APS Excellence in Physics Education Award
November 2019

Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

Question about EJS and Objects post and replies

Return to the Question about EJS and Objects thread
Login to post to this thread

Objects in EJS
Eduardo Garcia
13 Posts


I am making a simulation on EJS, but I am having problem with my custom code. I don't undestand how I can get the Object of a button or a 2dShape I make so I can use the class functions with it.

For example, I have created a 2Dshape.  I want to be able to change its color when I click on it, by using  the function "setColor()" of the class.. I cant seem to get the object of the 2DShape. I used the functions "_view.getvisual("shape")" to no avail. I don't have so much knowledge in programming.

Thank you for yout time and patience.

Post edited October 30, 2011 at 4:48 PM EST.

Replies to Objects in EJS

Re: Objects in EJS -
Francisco Esquembre
228 Posts

If you have created a view element called, say, my2DShape, you can access the underlying Object by simply using object orientation from the _view object. That is, the Object is:

Then, you can call any method that this particular object implements.

Now, knowing which methods are implemented is a more difficult thing, because this is only documented in the source code!

But changing the color is done like this:

java.awt.Color aColor = new java.awt.Color(255,0,0); // This is red
_view.my2DShape.getStyle().setLineColor(aColor);  // this sets the line (border) color
_view.my2DShape.getStyle().setFillColor(aColor);  // this sets the inside color


PS: EJS wraps each graphical object with a so-called ControlElement that does the trick of allowing changes through setting properties. _view.getElement("my2DShape") will return that ControlElement and you could, in principle, use this to change the appearance of the element. But this is not recommended. The approach shown above (introduced later) is much better.

Finally, _view.getVisual("my2DShape") returns a java.awt.Component for view elements that are based on Java Swing elements.

Re: Re: Objects in EJS -
Eduardo Garcia
13 Posts

Hello, thank you very much for your reply.

I had another question about EJS, concerning the Console. Is there a way I can read the messages to the console so I can print them in a TextArea in my Applet? I am using EJS to talk  remotely with Labview through JIL server, and I would like to read the messages that the JILserver is sending to the console to know the state of the connection, among other things.

Thank you for your time and patience.

Re: Re: Re: Objects in EJS -
Francisco Esquembre
228 Posts

No. I don't think you can intercept that. I guess JIL is using System.out.println, so the messages go to the standard output, which is the Console while running under EJS and the Operating System when running as an application.

A possible solution would be to edit the code for the JIL server to send those messages to you. But this  is not trivial...

Re: Re: Re: Re: Objects in EJS -
Eduardo Garcia
13 Posts


I found out a solution to my previous question. I thought it could help someone so I wanted to post the method I used.

What I did was re-define the PrintStream that goes to the System.out.println command. There are a lot of Public License Libraries that do a new class, TextOutputStream, that overrides the write commands cof the PrintStream class and instead writes to a JTextArea that is defined in its constructor.

For example I used a free library that has this class (sorry, I seem to have lost the link, but in Google it can be easily found):


In the Inicialization I did this:


JTxtConsole=_view.getVisual("Console"); //Gets the JTextArea of my TextArea which will be the               console of the app

out=new PrintStream(new net.sourceforge.squirrel_sql.plugins.firebirdmanager.TextAreaOutputStream((JTextArea)JTxtConsole, (JScrollPane)JPaneConsole));

System.setOut( (PrintStream)out );
System.out.println( "Hello World!" );


Re: Re: Re: Re: Re: Objects in EJS -
Francisco Esquembre
228 Posts


I can only say that I am impressed by your solution!


Re: Re: Re: Re: Re: Objects in EJS -
lookang Avatar
222 Posts

Hi Eduardo Garcia!
chance to share your simulation for my learning purposes?

all my simulation can be found here which i document my learning using Ejs.

Attached File: ntnujavaforumlookang.png

OSP@SG blog
OSP@SG Digital Library

Re: Re: Re: Re: Re: Re: Objects in EJS -
Eduardo Garcia
13 Posts

Hello Lookang:

I am sorry I am replying almost a year late! I do not have any simulations in EJS per se, i am using it to control remote laboratories in the area of control dynamics.

Thank you,

Eduardo Garcia

OSP Projects:
Open Source Physics - EJS Modeling
Physlet Physics
Physlet Quantum Physics
STP Book