2017-01-18 2 views
0

JSliderのノンリニアスケールを作成しようとしていますが、現在使用しているスケールではそれほど大きくないために大きなギャップがあります。JSliderのノンリニアスケールを作成する

これは私が部門を設定したためです。たとえば、スライダが300の場合、精度は1661ですが、スライダが301の場合、精度は9192です。

多くの余分な範囲を追加することなくギャップを小さくする方法はありますか?例えば

else if(temp > 150 && temp <= 250) 
    accuracy = (int) Math.pow((double) accSlide.getValue(), 1.2); 

編集
の多くを行うことなく、このJSliderの目的は、数字の間の加速度の3~4の異なるセクションを有することです。例えば

Section 1 (First quarter of JSlider) : 0-150 
Section 2 (Second quarter of JSlider): 150-10000 
Section 3 (Third quarter of JSlider) : 10000-100000 
Section 4 (Final quarter of JSlider) : 100000-1000000 

ためのこのaccuracy変数は、プログラム内の異なる機能に後で使用されます。次の短いコード例では、更新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()); 
    } 
} 
+2

は、それはあなたが、スライダーの値から(ひどくという名前の非Java標準)変数SQRT_DIGを計算するために使用し、あなたの方程式上のすべてのヒンジないのですか?あなたの現在の方程式は、あなたが見ているひどい不連続をもたらします。代わりに、より滑らかになるように方程式を微調整する必要があります。私たちはあなたの方程式の必要性を知らないので、これはあなたが解決できる唯一の問題であるようです。 –

+0

あなたの関数には巨大なジャンプがあります。 https://www.mathsisfun.com/calculus/continuity.html –

+0

300と301の等しい値を生成するように、適切なオフセットを方程式のケースに追加することをお勧めします。 –

答えて

1

あり、これを行うためのいくつかの簡単な方法がありますが、重要なことは、以前の範囲の最大値で開始することです。例えばより高い勾配を有する一連のラインのための

 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); 
関連する問題