jerome
jerome

Reputation: 2089

JavaFX 2 Slider for circular knob and snap to value

based on the volume knob from this example, http://fxexperience.com/2012/01/fun-javafx-2-0-audio-player/

How would you make a volume knob that snaps to particular angle value, every 15 or 30 degree for example ? Thanks for any hint. What i did is redefine rotateKnob in subclass and set the angle in this manner for the slider to snap at values 180, 120, 60, 0, -180, -160, -120, -60 :

Slider s = getSkinnable();
    double zeroOneValue = (s.getValue() - s.getMin()) / (s.getMax() - s.getMin());
    double angle = minAngle + ((maxAngle - minAngle) * zeroOneValue);

    int angleMod = (int) (angle % 60);
    double val;
    if (angleMod == 0) {
        val = (int) angle;
    } else if (angleMod > (60 / 2)) {
        val = (int) angle - angleMod;
    } else {
        val = (int) angle - angleMod + 60;
    }

Upvotes: 1

Views: 4436

Answers (2)

Adam
Adam

Reputation: 89

@Enwired has a good answer. you need a different graphical resource to change the number of dots. but that is not a part of what makes the angle between each tick around the rotary control.

just as he divided the circle into 1/14's, you can choose to divide it into 60 degree slices by 60.0/360.0 or 1.0/6.0.

Upvotes: 0

Enwired
Enwired

Reputation: 1593

You don't need to do anything special. Just make use of the "snapToTicks" property. In the main class, after the volume knob is defined, put this code:

    volumeKnob.setMinorTickCount(0);
    volumeKnob.setMajorTickUnit(1.0/14.0);
    volumeKnob.snapToTicksProperty().set(true);

The numbers here are based on the fact that there are 14 "dots" around the volume knob. This code will make sure that you can only set the volume to one of those dots.

Upvotes: 1

Related Questions