JSci.swing.JRoundDial 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 round dial, where the pointer can rotate many turns; the turns are indicated
by digits inside the dial. JPointer s must be added. */
public class JRoundDial extends JPanel {
private double oneTurn = 360;
private double zero = 0.0;
private double minorTic = 10;
private double majorTic = 30;
private boolean setPaintMinorTicks = true;
private boolean setPaintMajorTicks = true;
private boolean setPaintLabels = true;
private JPointer setPaintTurns = null;
private double radius = 70;
private ArrayList pointers = new ArrayList();
private ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent event) {
repaint();
}
};
/** constructs a dial with no pointers
* @param o the value that corresponds to one turn
* (e.g. 360 for measures in degrees); negative values for
* counter-clockwise dial
*/
public JRoundDial(double o) {
oneTurn = o;
setSize((int)(2*radius),(int)(2*radius));
addMouseListener(new TheMouseListener());
addMouseMotionListener(new TheMouseMotionListener());
}
/** define the position at which the zero of the dial will be placed
* @param z the angle (from the top, clockwise, in rads) where the zero of
* the dial will be placed
*/
public void setZero(double z) {
zero = z;
}
/** as described in java.awt.Component */
public void setSize(int w,int h) {
super.setSize(w,h);
radius = (w=0;j--)
if (((JPointer)pointers.get(j)).contains(e.getPoint())) {
dragged=(JPointer)pointers.get(j);
dragDelta=2.0*Math.PI*dragged.getValue()/oneTurn-theta(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;jfrom+Math.abs(oneTurn/2)) to-=Math.abs(oneTurn);
dragged.setValue(to);
repaint();
}
}
}
// Main
public static void main(String [] args) {
JFrame frm = new JFrame();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRoundDial d = new JRoundDial(360.0);
d.setZero(Math.PI/6.0);
frm.getContentPane().add(d);
d.setSize(260,260);
frm.pack();
frm.show();
try { Thread.sleep(3000); } catch (InterruptedException e) {}
JPointer p = new JPointer(JPointer.POINTER_SIMPLE_QUADRANGLE);
p.setColor(Color.MAGENTA);
p.setValue(20.0);
d.addJPointer(p);
try { Thread.sleep(3000); } catch (InterruptedException e) {}
JPointer p1 = new JPointer(JPointer.POINTER_SIMPLE_STOP);
p1.setColor(Color.CYAN);
p1.setValue(130.0);
d.addJPointer(p1);
d.setPaintTurns(p1);d.repaint();
p1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
double v=((JPointer)e.getSource()).getValue();
System.out.println("State changed: "+v);
}
});
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
//p.setValue(p.getValue()+10.0);
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
//p.setValue(p.getValue()+10.0);
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
//p.setValue(p.getValue()+10.0);
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
//p.setValue(p.getValue()+10.0);
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
//d.removeJPointer(p);
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
//d.removeJPointer(p1);
//try { Thread.sleep(3000); } catch (InterruptedException e) {}
}
}