slider controlling height of wave

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newgal
    New Member
    • Jan 2008
    • 8

    #1

    slider controlling height of wave

    Hi all,

    I need to use a slider to control the height of the wave, but have no idea!

    I have created a slider. Do I have to formulate another formula to control the height or is there an easier way? I may be on the wrong track :(

    Here is part of the code. Any pointers would be great!

    Code:
    private JSlider adjustHeight = new JSlider(JSlider.VERTICAL, 1, 100, 5);
      
    public SineDraw() { 
      super(new BorderLayout());   
      setHeight(5);	
      new Thread(this).start();
    	
     adjustHeight.addChangeListener(new ChangeListener() { 
       public void stateChanged(ChangeEvent e) {
         for(int i = 0; i < sines.length; i++)
         setHeight(((JSlider)e.getSource()).getValue());
         }
      });
    add(BorderLayout.EAST, adjustHeight);	
    
     public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int maxWidth = getWidth();
        double hstep = (double)maxWidth / (double)points;
        int maxHeight = getHeight();
        pts = new int[points];
          for(int i = 0; i < points; i++)
            pts[i] =
             (int)(sines[i] * maxHeight/2 * .85 + maxHeight/2);
    	
        g.setColor(Color.BLUE);
        for(int i = 1; i < points; i++) {
          int x1 = (int)((i - 1) * hstep);
          int x2 = (int)(i * hstep);
          int y1 = pts[i-1];
          int y2 = pts[i];
          g.drawLine(x1, y1, x2, y2);
        }
      }
      
     public synchronized void setHeight(int newHeight) { 
        height = newHeight;
        int maxHeight = getHeight();
        sines = new double[points];
        pts = new int[points];
          for(int i = 0; i < points; i++){
            pts[i] = 
             (int)(sines[i] * maxHeight/2 * .25 + maxHeight/2);
        }
        System.out.print(" setHeight " + height);
        repaint();
      }
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by newgal
    Hi all,

    I need to use a slider to control the height of the wave, but have no idea!

    I have created a slider. Do I have to formulate another formula to control the height or is there an easier way? I may be on the wrong track :(

    Here is part of the code. Any pointers would be great!

    Code:
    private JSlider adjustHeight = new JSlider(JSlider.VERTICAL, 1, 100, 5);
      
    public SineDraw() { 
      super(new BorderLayout());   
      setHeight(5);	
      new Thread(this).start();
    	
     adjustHeight.addChangeListener(new ChangeListener() { 
       public void stateChanged(ChangeEvent e) {
         for(int i = 0; i < sines.length; i++)
         setHeight(((JSlider)e.getSource()).getValue());
         }
      });
    add(BorderLayout.EAST, adjustHeight);	
    
     public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int maxWidth = getWidth();
        double hstep = (double)maxWidth / (double)points;
        int maxHeight = getHeight();
        pts = new int[points];
          for(int i = 0; i < points; i++)
            pts[i] =
             (int)(sines[i] * maxHeight/2 * .85 + maxHeight/2);
    	
        g.setColor(Color.BLUE);
        for(int i = 1; i < points; i++) {
          int x1 = (int)((i - 1) * hstep);
          int x2 = (int)(i * hstep);
          int y1 = pts[i-1];
          int y2 = pts[i];
          g.drawLine(x1, y1, x2, y2);
        }
      }
      
     public synchronized void setHeight(int newHeight) { 
        height = newHeight;
        int maxHeight = getHeight();
        sines = new double[points];
        pts = new int[points];
          for(int i = 0; i < points; i++){
            pts[i] = 
             (int)(sines[i] * maxHeight/2 * .25 + maxHeight/2);
        }
        System.out.print(" setHeight " + height);
        repaint();
      }
    Yes, You will to set a formula for it.. base on what current value is in the Slider

    In every move, get the value, have the operation and repaint...

    I think you will need a new Runnable from it... but maybe there is another way...

    As the magnitude of the sinusoidal wave increases, the phase angle will also change slightly...

    That formula is what you have tried to show the wave form...



    Correct me if im wrong,
    Sukatoa...

    Comment

    Working...