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

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
147 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.



The OSP Network:
Open Source Physics - Tracker - EJS Modeling
Physlet Physics
Physlet Quantum Physics