public class Slider { final int w = 200; final int h = 10; int x; int y; int sliderX; int sliderY; float min; float max; float value; boolean dragging = false;

public Slider(int x, int y, float min, float max) {
    this.x = x;
    this.y = y;
    sliderX = x;
    sliderY = y;
    this.min = min;
    this.max = max;
    value = min;
}

public float getValue() {
    return value;
}

public boolean containsX(int x) {
    return x >= this.x && x <= (this.x + w);
}

public boolean containsY(int y) {
    return y >= this.y && y <= (this.y + h);
}

public boolean contains(int x, int y) {
    return containsX(x) && containsY(y);
}

public void paint(String suffix) {
    stroke(128);
    fill(128);
    rect(x, y, w, h);
    
    if (mousePressed) {
        if (contains(mouseX, mouseY) && mouseButton == LEFT) {
            dragging = true;
        }
        if (dragging && containsX(mouseX)) {
            sliderX = mouseX;
            value = (((sliderX - x) / (float) w) * (max - min)) + min;
        }
    } else {
        dragging = false;
    }
    
    stroke(0);
    fill(0);
    textSize(14);
    text(String.format('%.2f', value) + suffix, x + w + 10, y + h);
    rect(sliderX, sliderY, h, h);
}

}

Convert Processing .pde Slider Class to Java

原文地址: https://www.cveoy.top/t/topic/qA99 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录