cursor line controlled by a slider
-
- Posts: 20
- Joined: Fri Jul 24, 2015 2:20 pm
- antibot: No, of course not.
cursor line controlled by a slider
Hello,
How can i make an horizontal (parallel with X axis) line cursor that is controlled by a slider?
Thank you very much!
How can i make an horizontal (parallel with X axis) line cursor that is controlled by a slider?
Thank you very much!
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: cursor line controlled by a slider
Write an object that hold a reference to an e. g. ValueMarker and is also a changeListener for your JSlider.
if this object is catching a changeEvent from the JSlider, get the value from the JSlider, convert that by whatever rule you think is appropriate to a double data value, and call setValue on the ValueMarker with the data value. Use the ValueMarker as range marker in your plot.
if this object is catching a changeEvent from the JSlider, get the value from the JSlider, convert that by whatever rule you think is appropriate to a double data value, and call setValue on the ValueMarker with the data value. Use the ValueMarker as range marker in your plot.
-
- Posts: 20
- Joined: Fri Jul 24, 2015 2:20 pm
- antibot: No, of course not.
Re: cursor line controlled by a slider
ok,Thank's a lot!
I've done this untill now :
ValueMarker marker = new ValueMarker(0);
marker.setPaint(Color.blue);
XYPlot plot=(XYPlot) chart.getPlot();
plot.addRangeMarker(marker);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
marker.getValue();
marker.setValue(4);
}
});
I don't know how to convert to double value..so if you can help me again please?
Thank you very much!
I've done this untill now :
ValueMarker marker = new ValueMarker(0);
marker.setPaint(Color.blue);
XYPlot plot=(XYPlot) chart.getPlot();
plot.addRangeMarker(marker);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
marker.getValue();
marker.setValue(4);
}
});
I don't know how to convert to double value..so if you can help me again please?
Thank you very much!
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: cursor line controlled by a slider
The JSlider or better its BoundedRangeModel only works with integers. For simplicity, lets assume that the value range of the BoundedRangeModel goes from 0 to 100.hassan.elkhatib wrote: I don't know how to convert to double value..so if you can help me again please?
You could then interpret the current value of the JSlider as a percentage. The question is "a percentage of what range". I can´t answer that, because I have no idea what you want to indicate with your marker.
A few tempting assumptions what the "100%" could be:
- The current range of the y axis
- The auto range of the y axis, e.g. the range that is required to show all the data points
- A user defined range
Please give more hints on what you want to achieve.
-
- Posts: 20
- Joined: Fri Jul 24, 2015 2:20 pm
- antibot: No, of course not.
Re: cursor line controlled by a slider
ok, i have a preasure sensor and a arduino and i display the signal with jfreechart .i want now to have a cursor (controlled by a slider) that when i click on it (the slider range is between 0 and 5 :0,2,3,4,5) will calculate the frequency ,or the period of signal. I'm interested in The current range of the y axis
Last edited by hassan.elkhatib on Mon Aug 24, 2015 1:09 pm, edited 1 time in total.
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: cursor line controlled by a slider
Please put your code between the code tags in your next post and not after the closing code tag.
When I was suggesting to "write an object that hold a reference to an e. g. ValueMarker and is also a changeListener for your JSlider", I thought about a new class and not of an anonymous ChangeListener instance.
if you write such a class, you can define the constructor and its parameters as needed. You could have a constrcutor with a ValueMarker and a ValueAxis parameter that is storing the instances as instance variables and accesses them in the stateChanged method. You do not need to store a reference to the JSlider because that is supplied as source of the ChangeEvent. Something like that might do the job:
What is the "cursor"? The JSlider or the marker? And what is "it" that you want to click on? The JSlider or the marker?hassan.elkhatib wrote:i want now to have a cursor (controlled by a slider) that when i click on it (the slider range is between 0 and 5 :0,2,3,4,5) will calculate the frequency ,or the period of signal. I'm interested in The current range of the y axis
When I was suggesting to "write an object that hold a reference to an e. g. ValueMarker and is also a changeListener for your JSlider", I thought about a new class and not of an anonymous ChangeListener instance.
if you write such a class, you can define the constructor and its parameters as needed. You could have a constrcutor with a ValueMarker and a ValueAxis parameter that is storing the instances as instance variables and accesses them in the stateChanged method. You do not need to store a reference to the JSlider because that is supplied as source of the ChangeEvent. Something like that might do the job:
Code: Select all
public class MarkerShifter implements ChangeListener{
private ValueAxis axis;
private ValueMarker marker;
public MarkerShifter(ValueAxis axis, ValueMarker marker){
this.axis = axis;
this.marker = marker;
}
public void public void stateChanged(ChangeEvent e) {
if(!(e.getSource() instanceof JSlider)) return;
int sliderValue = ((JSlider)e.getSource).getValue();
Range axisRange = axis.getRange();
double markerValue = axisRange*sliderValue/5.0;//or anything else
marker.setValue(markerValue);
}
}
-
- Posts: 20
- Joined: Fri Jul 24, 2015 2:20 pm
- antibot: No, of course not.
Re: cursor line controlled by a slider
the "cursor" is the maker and "it" is the slider.Sorry for my English.
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: cursor line controlled by a slider
Than, the approach suggested above should work.hassan.elkhatib wrote:the "cursor" is the maker and "it" is the slider.
-
- Posts: 20
- Joined: Fri Jul 24, 2015 2:20 pm
- antibot: No, of course not.
Re: cursor line controlled by a slider
Thank's a lot! it apears an error :
"Syntax error on token "void", volatile expected."
at this line:
public void public void stateChanged(ChangeEvent e) {
"Syntax error on token "void", volatile expected."
at this line:
public void public void stateChanged(ChangeEvent e) {
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: cursor line controlled by a slider
I wrote the code without IDE and without a test whether it could be compiled.
Delete one of the two "public void public void".
The statement "axisRange*sliderValue/5.0" won´t compile as well since axisRange is an instance of Range, and you can´t multiply a range with a double.
The statement
should compile.
Delete one of the two "public void public void".
The statement "axisRange*sliderValue/5.0" won´t compile as well since axisRange is an instance of Range, and you can´t multiply a range with a double.
The statement
Code: Select all
double markerValue = axisRange.getLength()*sliderValue/5.0 + axis.getLowerBound();
-
- Posts: 20
- Joined: Fri Jul 24, 2015 2:20 pm
- antibot: No, of course not.
Re: cursor line controlled by a slider
When i write what you said i have an error now just at the line :"public class MarkerShifter implements ChangeListener"
and the error is :Multiple markers at this line
- The type MarkerShifter is never used locally
- Illegal modifier for the local class MarkerShifter; only abstract or final is
permitted
When i put "final" in place of "public" the program runs but it don't display any change..i mean i don't have any valuemarker on the screen.
code:
public class MarkerShifter implements ChangeListener{
private ValueAxis axis;
private ValueMarker marker;
public MarkerShifter (final ValueAxis axis, final ValueMarker marker){
this.axis = axis;
this.marker = marker;
}
public void stateChanged(ChangeEvent e) {
if(!(e.getSource() instanceof JSlider)) return;
int sliderValue = ((JSlider)e.getSource()).getValue();
Range axisRange = axis.getRange();
double markerValue = axisRange.getLength()*sliderValue/5.0 + axis.getLowerBound();
marker.setValue(markerValue);
and the error is :Multiple markers at this line
- The type MarkerShifter is never used locally
- Illegal modifier for the local class MarkerShifter; only abstract or final is
permitted
When i put "final" in place of "public" the program runs but it don't display any change..i mean i don't have any valuemarker on the screen.
code:
public class MarkerShifter implements ChangeListener{
private ValueAxis axis;
private ValueMarker marker;
public MarkerShifter (final ValueAxis axis, final ValueMarker marker){
this.axis = axis;
this.marker = marker;
}
public void stateChanged(ChangeEvent e) {
if(!(e.getSource() instanceof JSlider)) return;
int sliderValue = ((JSlider)e.getSource()).getValue();
Range axisRange = axis.getRange();
double markerValue = axisRange.getLength()*sliderValue/5.0 + axis.getLowerBound();
marker.setValue(markerValue);
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: cursor line controlled by a slider
When I add the two missing closing brackets, the code snippet looks ok in my IDE.
I assume that you have copy-pasted the fragment somewhere in your existing code, and that your use of the class is not correct. But these are issues outside the code snippet, and I obviously can not have a clue about how you use the class.
And once more: use code tags.
I assume that you have copy-pasted the fragment somewhere in your existing code, and that your use of the class is not correct. But these are issues outside the code snippet, and I obviously can not have a clue about how you use the class.
And once more: use code tags.
-
- Posts: 20
- Joined: Fri Jul 24, 2015 2:20 pm
- antibot: No, of course not.
Re: cursor line controlled by a slider
Thank's !!! now it works! thank very much!!!