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

Adjusting the font for plot axis labels - Dec 11, 2011 at 2:32PM
Larry Engelhardt
14 Posts

Is it possible to modify the font that is used for the axis labels of plots?

I see that PlottingPanel view elements have a setFont method, so I tried the following lines of code:

Font f = new Font(Font.SERIF, Font.PLAIN, 30);
_view.plottingPanel.setFont(f);

However, this does not seem to do it.

Thanks!


Replies to Adjusting the font for plot axis labels

Re: Adjusting the font for plot axis labels - Dec 11 2011 5:30PM
Francisco Esquembre
72 Posts

Larry,
The first answer is no. The PlottingPanel class doesn't provide this possibility. At least yet.
But we are having a second look. Maybe it is not too difficult to add it.
Paco



Re: Adjusting the font for plot axis labels - Jan 21 2012 12:18PM
Francisco Esquembre
72 Posts

Hi,
Because this solution was not satisfactory, we had to add a new feature.
The latest EJS releases, starting with EJS_4.3.5_120120, offer a new property for PlottingPanels called FontFactor. This factor multiplies the default font size of the axes labels to increase (factor > 1) or (decrease factor<1) the font displayed. Try 1.2 to increase, for instance. Do not use a 0 or negative factor!
Paco



Re: Adjusting the font for plot axis labels - Jan 24 2012 7:54AM
james adam (DS)
1 Posts

it should work... if dont... try this..

Font f = new Font(Font.SERIF, Font.PLAIN, 30);
_view.plottingPanel.setFont(f);




Modeling Agency
Modeling Agency in CA



Re: Re: Adjusting the font for plot axis labels - Jan 25 2012 4:18PM
Francisco Esquembre
72 Posts

I guess this will change some fonts in the panel, but not those of the axes labels...
I recommend using the new property for what Larry wants to do.

Paco



Rotation of a 2Dshape - Dec 9, 2011 at 7:18AM
Iannis Avramopoulos
4 Posts

Hello,

I have a 2Dshape and I want to rotate it by a variable angle, say "theta", which I have already declared in the variables panel.

So I try to input my variable to the dialog box that accompanies the cell "Transform".  I type "theta", in the cell next to the button "Rotation" and press the button. The line on the bottom reads "ro:thetad" which seems to me wrong syntax and it appears that it actually is, because in the output area reads

"Incorrect value for transformation: ro:thetad
java.lang.NumberFormatException:For input string:"theta"
at.sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1242)
....
..."
many more lines,   you get the idea

and of course it's not rotating when I run the simulation.

I can't figure out what's wrong with the syntax, as my programming skills are below average and I have little knowledge of java .

Thank you in advance

Iannis


Replies to Rotation of a 2Dshape

Re: Rotation of a 2Dshape - Dec 09 2011 8:29AM
Francisco Esquembre
72 Posts

The Transform property can be very sophisticated, if you use the dialog to add more than one transformation.

But it can also be very simple!

Just type "theta" or use the 'link' icon to link it to that variable.

That should do it. (Notice that if theta is a double variable, its value is considered to be in radians. If it is an int variable, the value is taken in degrees.)

Paco

PS: If you use the dialog for transformations, the right syntax would be: "ro:"+theta+"d". Thsi builds a string that EJS can later reinterpret as a rotation.

But if all you want is a rotation, a simple variable is enough.



Re: Re: Rotation of a 2Dshape - Dec 10 2011 12:15PM
Iannis Avramopoulos
4 Posts

Thank you Paco,

I just typed "theta" and it worked ok and I think that will do for my simulation.

I also tried to follow the more complicated way so, I opened the dialog of the "Transform" cell and typed in the last line "ro:"+theta+"d". When I run the simulation, my shape doesn't rotate. That's not a real problem for me cause I achieved the rotation the easy way.

Thank you again

Iannis



Elements in a ShapeSet - Nov 6, 2011 at 3:41PM
Eduardo Garcia
13 Posts

Hello:


Related to my other post, I am looking for a way to adress an element in a ShapeSet.

For example I have a ShapeSet of 8 elements, and I want to change the color of the single element that the user clicked. How can I get the Object with which the user has interacted from the ShapeSet?


Thank you for your time and patience.


Replies to Elements in a ShapeSet

Re: Elements in a ShapeSet - Nov 07 2011 1:10PM
Francisco Esquembre
72 Posts

Eduardo,

Knowing which element you have selected from a set is done using the "Interaction index" property. Link it to an int variable and this variable will receive the value of the shape with which you are interacting.

Changing the color can be done in two different ways.

Way #1:
EJS allows you to do this without object oriented programming, at the cost of creating an array of ints, linking this array to the "Fill Color" property of the set, and changing programmatically the value of this array at the right index when needed. EJS has a standard way of translating the int to a java.awt.Color.

Alternatively, you can create an array of Objects, link it to the "Fill Color" property of the set and setting and changing the objects in the array to the java.awt.Color object that you want. This is similar to the use of ints, if only you can decide the precise colors to use.

Way #2:
Using object-oriented programming is very clean and to the point, if only more technical. If the index of the shape you want to change its color to, say RED, is "index, use this:

_view.shapeSet.getElement(index).getStyle().setFillColor(java.awt.Color.RED);

Explanation:
- _view.shapeSet is an object of the class org.opensourcephysics.drawing2d.Set (if you created a ShapeSet with the name "shapeSet")
- getElementIndex(index) returns an object of the basic abstract class org.opensourcephysics.drawing2d.Element (which is actually a org.opensourcephysics.drawing2d.ElementShape)
- the abstract class Element has a Style for visual appearance. You get this with the method getStyle()
- You set the fill color of the style of the element (shape) with setFillColor.

As you see, EJS does all this for you using property linking at the price of some extra memory for variables.

Paco

PS1: I have attached an example that uses the second possibility.

PS2: To have access to the Javadoc reference for EJS elements, visit http://fem.um.es/Javadoc/EJS_javadoc/index.html

Attached File: ejs_ShapeSetExample.zip



Re: Re: Elements in a ShapeSet - Nov 12 2011 7:46PM
Eduardo Garcia
13 Posts

Hello:

Thank you very much for your response! Ill look into the Javadoc, thank you!

EDIT 2: I have solver the problem stated below. The code below works, my error was that the path of the .jpg was wrong...


EDIT:

I am having a problem with ImageSets. I am trying now to use a ImageSet and to change the image of a certain element of the set when clicked. I coded this on "on click" event, but I have been unsucessful at the moment. I tried:

int i=_view.imageSet.getInteractedIndex();


org.opensourcephysics.drawing2d.ElementImage gg=(org.opensourcephysics.drawing2d.ElementImage)_view.imageSet.getElement(i); //get ElementImage

gg.setImageFile("C:/Users/Dewey/Desktop/EJS/EJS_4.3.4/workspace/output/images0010.jpg");
System.out.println(gg.getImageFile());



I get in the Console that the ImageFile has been changed, but the Image still displays the initial file. I dont know if maybe I need to order the system to redraw or to update the ImageSet. Thank you very much for your help, and sorry for the constant questions :S




Post edited November 12, 2011 at 9:25 PM EST.

Post edited November 12, 2011 at 10:33 PM EST.



Objects in EJS - Oct 28, 2011 at 1:46AM
Eduardo Garcia
13 Posts

Hello.

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 - Oct 28 2011 2:25PM
Francisco Esquembre
72 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:
_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.



Re: Re: Objects in EJS - Oct 30 2011 4:49PM
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 - Oct 30 2011 5:12PM
Francisco Esquembre
72 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 - Nov 01 2011 12:52AM
Eduardo Garcia
13 Posts

Hello.


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

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

_view.Console.setEditable(true);
inicio=_view.Console.getSelectionStart();
System.setOut( (PrintStream)out );
System.out.println( "Hello World!" );

--------------------------



Re: Re: Re: Re: Re: Objects in EJS - Nov 01 2011 3:50PM
Francisco Esquembre
72 Posts

Eduardo,

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

Paco



Re: Re: Re: Re: Re: Objects in EJS - Nov 02 2011 10:43AM
lookang Avatar
lookang
49 Posts

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

all my simulation can be found here which i document my learning using Ejs.
http://www.phy.ntnu.edu.tw/ntnujava/index.php?board=28.0

Attached File: ntnujavaforumlookang.png


http://weelookang.blogspot.sg


Re: Re: Re: Re: Re: Re: Objects in EJS - Jun 13 2012 10:16AM
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