Java 为JSlider创建非线性比例

Java 为JSlider创建非线性比例,java,swing,Java,Swing,我正在尝试为一个JSlider制作一个非线性量表,但是,我目前使用的量表有很大的差距,这使得它不太理想 这是因为我设置分区的方式。例如,当滑块打开时300精度为1661,但当滑块打开时301精度为9192 是否有一种方法可以在不增加大量额外范围的情况下缩小间距。例如,没有做很多工作 else if(temp > 150 && temp <= 250) accuracy = (int) Math.pow((double) accSlide.getValue(),

我正在尝试为一个
JSlider
制作一个非线性量表,但是,我目前使用的量表有很大的差距,这使得它不太理想

这是因为我设置分区的方式。例如,当滑块打开时
300
精度为
1661
,但当滑块打开时
301
精度为
9192

是否有一种方法可以在不增加大量额外范围的情况下缩小间距。例如,没有做很多工作

else if(temp > 150 && temp <= 250)
    accuracy = (int) Math.pow((double) accSlide.getValue(), 1.2);
精度
变量随后将在程序的不同功能中使用。在下面的简短代码示例中,它显示为更新
JLabel
。使用
JSlider
实现这一点的最佳方法是什么


SSCCE/MCVE

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class Slider extends JFrame {
private int accuracy = 150;
    private Slider() {
        super("Slider");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createLayout();
        setSize(400, 100);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void createLayout() {
        JPanel rootPanel = new JPanel();

        JLabel acc = new JLabel("Accuracy: " + accuracy);
        JSlider accSlide = new JSlider(1, 1000, accuracy);

        accSlide.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent arg0) {
                int temp = accSlide.getValue();
                if(temp <= 150)
                    accuracy = accSlide.getValue();
                else if(temp > 150 && temp <= 300)
                    accuracy = (int) Math.pow((double) accSlide.getValue(), 1.3);
                else if(temp > 300 && temp <= 600)
                    accuracy = (int) Math.pow((double) accSlide.getValue(), 1.6);
                else if(temp > 600 && temp <= 1000)
                    accuracy = (int) Math.pow((double) accSlide.getValue(), 2);
                acc.setText("Accuracy: " + accuracy);
            }
        });
        rootPanel.add(acc);
        rootPanel.add(accSlide, "grow");

        add(rootPanel);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new Slider());
    }
}
导入java.awt.EventQueue;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JSlider;
导入javax.swing.event.ChangeEvent;
导入javax.swing.event.ChangeListener;
@抑制警告(“串行”)
公共类滑块扩展JFrame{
私人整数精度=150;
专用滑块(){
超级(“滑块”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createLayout();
设置大小(400100);
setLocationRelativeTo(空);
setVisible(真);
}
私有void createLayout(){
JPanel rootPanel=newjpanel();
JLabel acc=新JLabel(“精度:+精度”);
JSlider accSlide=新JSlider(1,1000,精度);
accsiled.addChangeListener(新的ChangeListener(){
@凌驾
公共无效状态已更改(ChangeEvent arg0){
int temp=accSlide.getValue();
如果(临时150和临时300和临时600和临时新滑块());
}
}

有一些简单的方法可以做到这一点,但重要的是从上一个范围的最大值开始;例如

        public void stateChanged(ChangeEvent arg0) {
            int temp = accSlide.getValue();
            double max=0;
            if(temp <= 150)
                accuracy = accSlide.getValue();
            else if(temp <= 300)
                accuracy = (int) (150+2*(accSlide.getValue()-150));
           else if(temp <= 600)
                accuracy = (int) (150+2*(300-150)+3*(accSlide.getValue()-300));
            else
                accuracy = (int) ( (150+2*(300-150)+3*(600-300))+4*(accSlide.getValue()-600));
           acc.setText("i " + temp+" Accuracy: " + accuracy);
        }
    });
公共无效状态已更改(ChangeEvent arg0){
int temp=accSlide.getValue();
双最大值=0;

if(temp)这一切不都取决于你用来计算的等式(非Java标准名为变量SQRT_从滑块的值中挖掘?您当前的方程会导致严重的不连续性,您可以看到,相反,您需要调整方程使其更加平滑。因为我们不知道您的方程需要什么,这似乎是一个只有您才能解决的问题。您的函数有巨大的跳跃。您可能应该添加Approvr将偏移量调整到等式情况,使其在300和301@HovercraftFullOfEels我编辑了我的问题,希望能更清楚地说明我想要实现的目标。您需要查找曲线拟合算法,然后用Java编写最适合数据转换的最佳曲线。
        public void stateChanged(ChangeEvent arg0) {
            int temp = accSlide.getValue();
            double max=0;
            if(temp <= 150)
                accuracy = accSlide.getValue();
            else if(temp <= 300)
                accuracy = (int) (150+2*(accSlide.getValue()-150));
           else if(temp <= 600)
                accuracy = (int) (150+2*(300-150)+3*(accSlide.getValue()-300));
            else
                accuracy = (int) ( (150+2*(300-150)+3*(600-300))+4*(accSlide.getValue()-600));
           acc.setText("i " + temp+" Accuracy: " + accuracy);
        }
    });
        public void stateChanged(ChangeEvent arg0) {
            int temp = accSlide.getValue();
            double max=0;
            if(temp <= 150)
                accuracy = accSlide.getValue();
            else if(temp <= 300) {
                max=150;
                accuracy = (int) (max+2*(accSlide.getValue()-150));
           }
           else if(temp <= 600) {
                max=150+2*(300-150);
                accuracy = (int) (max+3*(accSlide.getValue()-300));
           }
            else {
                max=150+2*(300-150)+3*(600-300);
                accuracy = (int) ( (max+4*(accSlide.getValue()-600));
           acc.setText("i " + temp+" Accuracy: " + accuracy);
        }
    });
            if(temp <= 150)
                accuracy = accSlide.getValue();
              else if(temp <= 300)
                accuracy = (int) (150+Math.pow(accSlide.getValue()-150, 1.3));
           else if(temp <= 600)
                accuracy = (int) (150+Math.pow(300-150, 1.3)+Math.pow((double) accSlide.getValue()-300, 1.6));
            else
                accuracy = (int) ( (150+Math.pow(300-150, 1.3)+Math.pow(600-300, 1.6))+Math.pow((double) accSlide.getValue()-600, 2));
            acc.setText("i " + temp+" Accuracy: " + accuracy);