Witam
Moim zadaniem było stworzyć program przedstawiający powstawanie funkcji sinus (Cały kod jest w tym temacie Swing i aktualizacja wyświetlania animacji) . I teraz mam dodać do niego opcje na zmianę kąta początkowego. Postanowiłem zrobić to na suwaku. W klasie DrawGraphPanel dołączyłem suwak w ten sposób:
< code = java>
public class DrawGraphPanel extends JPanel {
DrawGraphBox b_graph ;
private DrawGraph graph = new DrawGraph();
int value;
private JSlider slider = new JSlider(0, 50, 5);
public DrawGraphPanel()
{
b_graph = new DrawGraphBox();
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
startButton.addActionListener(new StartAction());
stopButton.addActionListener(new StopAction());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setBorder(new TitledBorder("Zmien fazę początkową"));
MyChangeListener lst = new MyChangeListener();
slider.addChangeListener(lst);
this.setLayout(new BorderLayout());
this.add(buttonPanel, BorderLayout.NORTH);
this.add(b_graph , BorderLayout.CENTER);
this.add(slider, BorderLayout.SOUTH);
lst.stateChanged(new ChangeEvent(slider));
}
class StartAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
b_graph.setAnimation(true);
}
}
class StopAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
b_graph.setAnimation(false);
}
}
class MyChangeListener implements ChangeListener
{
public MyChangeListener() {
}
@Override
public synchronized void stateChanged(ChangeEvent e) {
value = slider.getValue();
graph.setPhase(value);
System.out.println(value);
}
}
}
W klasie DrawGraph zamieściłem metodę setPhase
Problem jest taki że nie wiem dlaczego wartość suwaka nie jest przesyłana do ustawienia fazy. Nie wiem czy trzeba jakiegoś ActionListenera czy może w ogóle robię to w zły sposób.
Czy ktoś mógłby mi wyjaśnić w jaki sposób mogę to zrobić ??
Z góry dziękuje za pomoc