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!
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();
}
Comment