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.
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: _view.my2DShape
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
Paco
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.
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.
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...
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):
firebirdmanager-3.2.1.jar
In the Inicialization I did this:
---------------------------
JTxtConsole=_view.getVisual("Console"); //Gets the JTextArea of my TextArea which will be the console of the app JPaneConsole=_view.getComponent("Console");
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.