G
Geoff Cox
Hello
The code below displays an image and allows a user to answer a related
question by moving a slider. When the program is stopped (window
closed) the slider value is saved to a file.
I would like to develop this so that when the user is happy with the
slider postion he/she clicks a button which
1. saves the slider value to the file, with the question number
2. displays another image with a new question
3. the slider is used to answer this new question etc etc
I would appreciate any pointers on how to do this!
Thanks
Geoff
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.io.*;
class Changes2 extends JFrame implements ChangeListener
{
JSlider scale = new JSlider(-20,+20,0);
JLabel position = new JLabel("Set Position");
public Changes2()
{
super("Slider");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container contentArea = getContentPane();
GridLayout lay = new GridLayout(3,1);
contentArea.setLayout(lay);
scale.setMajorTickSpacing(10);
scale.setMinorTickSpacing(5);
scale.setPaintTicks(true);
scale.setPaintLabels(true);
scale.addChangeListener(this);
JLabel text = new JLabel("Look at the picture and indicate
on the slider how you feel.");
ImageIcon picicon = new ImageIcon("pic1.jpg");
JButton buttonpic = new JButton(picicon);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
panel1.add(buttonpic);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.add(text);
JPanel panel3 = new JPanel();
panel3.setBackground(Color.blue);
panel3.add(scale);
contentArea.add(panel1);
contentArea.add(panel2);
contentArea.add(panel3);
setContentPane(contentArea);
}
public void stateChanged(ChangeEvent event)
{
JSlider src = (JSlider) event.getSource();
if(!src.getValueIsAdjusting())
position.setText("Position is "+scale.getValue());
try
{
FileWriter outfile = new FileWriter ("data.txt");
PrintWriter pw = new PrintWriter(outfile);
pw.println(scale.getValue());
pw.close();
}
catch (IOException e) { System.out.println(e); }
}
public static void main(String[] args)
{
Changes2 eg = new Changes2();
}
}
The code below displays an image and allows a user to answer a related
question by moving a slider. When the program is stopped (window
closed) the slider value is saved to a file.
I would like to develop this so that when the user is happy with the
slider postion he/she clicks a button which
1. saves the slider value to the file, with the question number
2. displays another image with a new question
3. the slider is used to answer this new question etc etc
I would appreciate any pointers on how to do this!
Thanks
Geoff
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.io.*;
class Changes2 extends JFrame implements ChangeListener
{
JSlider scale = new JSlider(-20,+20,0);
JLabel position = new JLabel("Set Position");
public Changes2()
{
super("Slider");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Container contentArea = getContentPane();
GridLayout lay = new GridLayout(3,1);
contentArea.setLayout(lay);
scale.setMajorTickSpacing(10);
scale.setMinorTickSpacing(5);
scale.setPaintTicks(true);
scale.setPaintLabels(true);
scale.addChangeListener(this);
JLabel text = new JLabel("Look at the picture and indicate
on the slider how you feel.");
ImageIcon picicon = new ImageIcon("pic1.jpg");
JButton buttonpic = new JButton(picicon);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
panel1.add(buttonpic);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.add(text);
JPanel panel3 = new JPanel();
panel3.setBackground(Color.blue);
panel3.add(scale);
contentArea.add(panel1);
contentArea.add(panel2);
contentArea.add(panel3);
setContentPane(contentArea);
}
public void stateChanged(ChangeEvent event)
{
JSlider src = (JSlider) event.getSource();
if(!src.getValueIsAdjusting())
position.setText("Position is "+scale.getValue());
try
{
FileWriter outfile = new FileWriter ("data.txt");
PrintWriter pw = new PrintWriter(outfile);
pw.println(scale.getValue());
pw.close();
}
catch (IOException e) { System.out.println(e); }
}
public static void main(String[] args)
{
Changes2 eg = new Changes2();
}
}