JSci.swing.JSliderPlus Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsci Show documentation
Show all versions of jsci Show documentation
JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software.
It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ...
Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).
The newest version!
package JSci.swing;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;
/** A place where JPointer
s can be placed.
JPointer
s must be added. */
public class JSliderPlus extends JPanel {
private double startValue;
private double endValue;
private double minorTic = 10;
private double majorTic = 20;
private boolean setPaintMinorTicks = true;
private boolean setPaintMajorTicks = true;
private boolean setPaintLabels = true;
private ArrayList sliders = new ArrayList();
private ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent event) {
repaint();
}
};
/** For building a vertical SliderHolder */
public static final int VERTICAL = 0;
/** For building a horizontal SliderHolder */
public static final int HORIZONTAL = 1;
/** constructs a SliderHolder with no sliders; the sliders will move
* inside a given range.
* @param o orientation (currently only VERTICAL)
* @param start starting value of the SliderHolder
* @param end end value of the SliderHolder
*/
public JSliderPlus(int o,double start,double end) {
startValue=start;
endValue=end;
setSize(getPreferredSize());
updateMinorTics();
updateMajorTics();
addMouseListener(new TheMouseListener());
addMouseMotionListener(new TheMouseMotionListener());
}
/** set the limits of the Slider
* @param start starting value of the SliderHolder
* @param end end value of the SliderHolder
*/
public void setLimits(double start,double end) {
startValue=start;
endValue=end;
setSize(getPreferredSize());
updateMinorTics();
updateMajorTics();
}
/** as described in java.awt.Component */
public Dimension getPreferredSize() {
return new Dimension(100,200);
}
/** set the spacing between minor tics
* @param s the spacing between minor tics
*/
public void setMinorTickSpacing(double s) {
minorTic = s;
updateMinorTics();
repaint();
}
/** set the spacing between major tics
* @param s the spacing between major tics
*/
public void setMajorTickSpacing(double s) {
majorTic = s;
updateMajorTics();
repaint();
}
/** show the minor tics?
* @param b show the minor tics?
*/
public void setPaintMinorTicks(boolean b) {
setPaintMinorTicks = b;
repaint();
}
/** show the major tics?
* @param b show the major tics?
*/
public void setPaintMajorTicks(boolean b) {
setPaintMajorTicks = b;
repaint();
}
/** show the tic labels?
* @param b show the tic labels?
*/
public void setPaintLabels(boolean b) {
setPaintLabels = b;
repaint();
}
private Line2D [] minorTics = null;
private Line2D [] majorTics = null;
private Point2D [] ticLabelPos = null;
private String [] ticLabelText = null;
private static NumberFormat formatter;
static {
formatter = NumberFormat.getNumberInstance();
formatter.setMaximumFractionDigits(2);
formatter.setMaximumIntegerDigits(3);
}
private void updateMinorTics() {
double width = getSize().getWidth();
double heigth = getSize().getHeight();
int N =(int)Math.abs((endValue-startValue)/minorTic);
minorTics = new Line2D[N+1];
for (int j=0;j<=N;j++)
minorTics[j] = new Line2D.Double(
width*0.3,heigth*(0.9-j*0.8/N),
width*0.4,heigth*(0.9-j*0.8/N)
);
}
private void updateMajorTics() {
double width = getSize().getWidth();
double heigth = getSize().getHeight();
int N =(int)Math.abs((endValue-startValue)/majorTic);
majorTics = new Line2D[N+1];
ticLabelPos = new Point2D[N+1];
ticLabelText = new String[N+1];
for (int j=0;j<=N;j++) {
majorTics[j] =
new Line2D.Double(
width*0.25,heigth*(0.9-j*0.8/N),
width*0.4,heigth*(0.9-j*0.8/N)
);
ticLabelText[j] = formatter.format(startValue+j*(endValue-startValue)/N);
ticLabelPos[j] =
new Point2D.Double(0.0,heigth*(0.9-j*0.8/N)+6);
}
}
/** draw the component - don't call this directly, but use repaint() */
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.draw(new Rectangle2D.Double(
getSize().getWidth()*0.45,getSize().getHeight()*0.05,
getSize().getWidth()*0.1,getSize().getHeight()*0.9)
);
if (setPaintMinorTicks)
for (int j=0;j=0;j--)
if (((JPointer)sliders.get(j)).contains(e.getPoint())) {
dragged=(JPointer)sliders.get(j);
dragDelta=dragged.getValue()-mousePosition(e);
dragged.setAdjusting(true);
break;
}
}
public void mouseReleased(MouseEvent e) {
if (dragged!=null) dragged.setAdjusting(false);
dragged = null;
boolean inside = false;
for (int j=0;jMath.max(startValue,endValue)) to=Math.max(startValue,endValue);
if (to