Java Forum / First Aid / July 2005
display new image?
Geoff Cox - 25 Jul 2005 17:42 GMT Hello
The code below allows me to display an image and a question and when I click on the "Next" button I can save the slider value (the answer to the question) to a file.
I cannot at the moment see how I can display a new image and question after the "Next" button has been clicked ...
Help!
Cheers
Geoff
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.io.*;
class Changes3 extends JFrame implements ChangeListener, ActionListener { JSlider scale = new JSlider(-20,+20,0); JLabel position = new JLabel("Set Position"); JButton nb = new JButton("Next"); public Changes3() {
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); nb.addActionListener(this);
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.white); JLabel slidertext = new JLabel("-20 = Very Bad, +20 = Very Good"); panel3.add(slidertext); panel3.add(scale); panel3.add(nb); 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()); }
public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == nb ) { tofile(); }
}
public void tofile() { 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) { Changes3 eg = new Changes3(); } }
Joan - 25 Jul 2005 17:49 GMT Hello
The code below allows me to display an image and a question and when I click on the "Next" button I can save the slider value (the answer to the question) to a file.
I cannot at the moment see how I can display a new image and question after the "Next" button has been clicked ...
Help!
Cheers
Geoff <snip -- too much stuff to read> Did you try delete the icon and question then re-display it.
Geoff Cox - 25 Jul 2005 18:33 GMT Joan,
><snip -- too much stuff to read> I have been taken to task before for not providing code which can be run!
>Did you try delete the icon and question then re-display it. No - will give that a go.
Thanks
Geoff
Joan - 25 Jul 2005 18:39 GMT > On Mon, 25 Jul 2005 11:49:53 -0500, "Joan" > <Joan@nospam.invalid> [quoted text clipped - 7 lines] > can be > run! No prob, it is just me that doesn't like to read a bunch of stuff.
>>Did you try delete the icon and question then re-display it. > > No - will give that a go. I do this in one of my programs.
// remove the old table ExcelUtil.frame.getContentPane().remove(myTable);
// create new table myTable = new JTable(tempArray, colName); myTable.setToolTipText("Split File Names"); myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); myTable.setRowSelectionAllowed(true); myTable.setEnabled(true); myTable.setVisible(true);
ExcelUtil.frame.getContentPane().add(myTable, BorderLayout.SOUTH); ExcelUtil.frame.pack(); ExcelUtil.frame.setVisible(true);
Geoff Cox - 25 Jul 2005 20:15 GMT >I do this in one of my programs. Joan,
Not sure how to remove the icon but thought it might be
panel1.remove(buttonpic) which I have after the slider value has been saved to a file. This will not compile, problem with "variable panel1 etc" - if you could kindly (really kindly!) scan the code below I guess you will see why?
Thanks
Geoff
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.io.*;
class Changes3 extends JFrame implements ChangeListener, ActionListener { JSlider scale = new JSlider(-20,+20,0); JLabel position = new JLabel("Set Position"); JButton nb = new JButton("Next"); ImageIcon picicon = new ImageIcon("pic1.jpg"); JButton buttonpic = new JButton(picicon);
public Changes3() {
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.");
nb.addActionListener(this);
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.white); JLabel slidertext = new JLabel("-20 = Very Bad, +20 = Very Good"); panel3.add(slidertext); panel3.add(scale); panel3.add(nb); 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()); }
public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == nb ) { tofile(); }
}
public void tofile() { 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); }
// System.exit(0);
panel1.remove(buttonpic); ImageIcon picicon = new ImageIcon("pic2.jpg"); JButton buttonpic = new JButton(picicon);
} public static void main(String[] args) { Changes3 eg = new Changes3(); } }
> // remove the old table > ExcelUtil.frame.getContentPane().remove(myTable); [quoted text clipped - 11 lines] > ExcelUtil.frame.pack(); > ExcelUtil.frame.setVisible(true); Joan - 25 Jul 2005 21:38 GMT > On Mon, 25 Jul 2005 12:39:12 -0500, "Joan" > <Joan@nospam.invalid> [quoted text clipped - 17 lines] > > Geoff I copied your sample code and got it to work. there are a few things that are different from what you were doing from what I was doing. I took poetic licence to change variable names and to move some local variables to fields etc. The reason it did not compile for you was a scope thing. I don't use GridLayout often enough to know if I can replace something at a particular location, so I changed it to BorderLayout. ;-) The basic idea is to replace the NORTH piece of the puzzle when you click the button. I replace the panel with a new panel, but I also have to put the new panel into the contentArea to complete the replacement. Here is the new program. Hope it makes sense.
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter;
import javax.swing.ImageIcon; import javax.swing.JButton; 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;
class Changes3 extends JFrame implements ChangeListener, ActionListener {
private JSlider scale = new JSlider(-20, +20, 0); private JLabel position = new JLabel("Set Position"); private JButton nxtButton = new JButton("Next"); private ImageIcon picIcon1 = new ImageIcon("pic1.jpg"); private ImageIcon picIcon2 = new ImageIcon("pic2.jpg"); private JButton buttonPic1 = new JButton(picIcon1); private JButton buttonPic2 = new JButton(picIcon2); private JPanel myPanel; private static Changes3 egg; private Container contentArea; private BorderLayout myLayout; private JLabel labText;
public Changes3() {
super("Slider"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true);
contentArea = getContentPane(); myLayout = new BorderLayout(); contentArea.setLayout(myLayout);
scale.setMajorTickSpacing(10); scale.setMinorTickSpacing(5); scale.setPaintTicks(true); scale.setPaintLabels(true);
scale.addChangeListener(this);
labText = new JLabel( "Look at the picture and indicate on the slider how you feel."); nxtButton.addActionListener(this);
myPanel = new JPanel(); myPanel.setBackground(Color.blue); myPanel.add(buttonPic1);
JPanel panel2 = new JPanel(); panel2.setBackground(Color.white); panel2.add(labText);
JPanel panel3 = new JPanel(); panel3.setBackground(Color.white); JLabel slidertext = new JLabel("-20 = Very Bad, +20 = Very Good"); panel3.add(slidertext); panel3.add(scale); panel3.add(nxtButton);
contentArea.add(myPanel, BorderLayout.NORTH); contentArea.add(panel2, BorderLayout.CENTER); contentArea.add(panel3, BorderLayout.SOUTH);
setContentPane(contentArea); }
public void stateChanged(ChangeEvent event) { JSlider src = (JSlider) event.getSource(); if (!src.getValueIsAdjusting()) position.setText("Position is " + scale.getValue()); }
public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == nxtButton) { tofile(); }
contentArea.remove(myPanel);
myPanel = new JPanel(); myPanel.setBackground(Color.blue); myPanel.add(buttonPic2);
contentArea.add(myPanel, BorderLayout.NORTH);
setContentPane(contentArea);
contentArea.setVisible(true); }
public void tofile() { 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); } // System.exit(0); }
public static void main(String[] args) { egg = new Changes3(); } }
Geoff Cox - 25 Jul 2005 21:54 GMT Joan,
Many thanks for your efforts! It works fine - will now try to understand how you have solved the problem!
Thanks again
Geoff
>> Not sure how to remove the icon but thought it might be >> [quoted text clipped - 151 lines] > } >} Geoff Cox - 25 Jul 2005 22:15 GMT Joan,
I have read through your code and just wish to thank you again. You have cleared up several questions for me.
Many thanks!
Cheers
Geoff
>>>I do this in one of my programs. >> [quoted text clipped - 155 lines] > } >} Joan - 25 Jul 2005 22:25 GMT > On Mon, 25 Jul 2005 15:38:36 -0500, "Joan" > <Joan@nospam.invalid> [quoted text clipped - 7 lines] > > Many thanks! Glad to help, I had a similar problem not too long ago so I had some code to refer to.
> Cheers > [quoted text clipped - 167 lines] >> } >>} Geoff Cox - 26 Jul 2005 00:16 GMT Joan,
If you are still around?!
I have declared a
private int count;
and added
count = ++count; pw.print(count+": ");
to tofile() in order to put a question number in front of the slider value and this seems to work OK. I also added "true" to the FileWriter so as to not overwrite the data file.
I would now like to be able to display a series of images rather than just the 2 so far ... I thought of using the incremented count value for buttonpic3, buttonpic4 etc by writing (buttonpic + count) but this does not work. Any thoughts? Some sort of loop needed?
Cheers
Geoff
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter;
import javax.swing.ImageIcon; import javax.swing.JButton; 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;
class Changes4 extends JFrame implements ChangeListener, ActionListener {
private JSlider scale = new JSlider(-20, +20, 0); private JLabel position = new JLabel("Set Position"); private JButton nxtButton = new JButton("Next"); private ImageIcon picIcon1 = new ImageIcon("pic1.jpg"); private ImageIcon picIcon2 = new ImageIcon("pic2.jpg"); private ImageIcon picIcon3 = new ImageIcon("pic3.jpg");
private JButton buttonPic1 = new JButton(picIcon1); private JButton buttonPic2 = new JButton(picIcon2); private JButton buttonPic3 = new JButton(picIcon3);
private JPanel myPanel; private static Changes4 egg; private Container contentArea; private BorderLayout myLayout; private JLabel labText; private int count = 0;
public Changes4() {
super("Slider"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true);
contentArea = getContentPane(); myLayout = new BorderLayout(); contentArea.setLayout(myLayout);
scale.setMajorTickSpacing(10); scale.setMinorTickSpacing(5); scale.setPaintTicks(true); scale.setPaintLabels(true);
scale.addChangeListener(this);
labText = new JLabel( "Look at the picture and indicate on the slider how you feel."); nxtButton.addActionListener(this);
myPanel = new JPanel(); myPanel.setBackground(Color.blue); myPanel.add(buttonPic1);
JPanel panel2 = new JPanel(); panel2.setBackground(Color.white); panel2.add(labText);
JPanel panel3 = new JPanel(); panel3.setBackground(Color.white); JLabel slidertext = new JLabel("-20 = Very Bad, +20 = Very Good"); panel3.add(slidertext); panel3.add(scale); panel3.add(nxtButton);
contentArea.add(myPanel, BorderLayout.NORTH); contentArea.add(panel2, BorderLayout.CENTER); contentArea.add(panel3, BorderLayout.SOUTH);
setContentPane(contentArea); }
public void stateChanged(ChangeEvent event) { JSlider src = (JSlider) event.getSource(); if (!src.getValueIsAdjusting()) position.setText("Position is " + scale.getValue()); }
public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == nxtButton) { tofile(); }
contentArea.remove(myPanel);
myPanel = new JPanel(); myPanel.setBackground(Color.blue); myPanel.add(buttonPic2);
contentArea.add(myPanel, BorderLayout.NORTH);
setContentPane(contentArea);
contentArea.setVisible(true); }
public void tofile() { try { FileWriter outfile = new FileWriter("data.txt",true); PrintWriter pw = new PrintWriter(outfile);
count = ++count; pw.print(count+": "); pw.println(scale.getValue()); pw.close(); } catch (IOException e) { System.out.println(e); } // System.exit(0); }
public static void main(String[] args) { egg = new Changes4(); } }
public void tofile() { try { FileWriter outfile = new FileWriter("data.txt", true); PrintWriter pw = new PrintWriter(outfile);
count = ++count; pw.print(count+": ");
pw.println(scale.getValue()); pw.close(); } catch (IOException e) { System.out.println(e); }
Joan - 26 Jul 2005 01:01 GMT > On Mon, 25 Jul 2005 16:25:49 -0500, "Joan" > <Joan@nospam.invalid> [quoted text clipped - 22 lines] > rather than > just the 2 so far I thought you might. ;-)
... I thought of using the incremented count value
> for buttonpic3, buttonpic4 etc by writing (buttonpic + count) > but this > does not work. This is a trick they use quite effectively in Java Script, but not for java.
> Any thoughts? Some sort of loop needed? Since this is driven by user input which is slow you can generate the Icons on the fly (unless you expect this to be run by hundreds of people simultaneously.)
You could put a "static int" field into the class that starts at 0 and is incremented up to N in "actionPerformed" to keep track of which image you are using. You'll need a String[] array to keep the names of the jpg files. Use the length of the array as N.
Of course, now the values that you write into file "data.txt" write on top of each other. Maybe you want to have an array of filenames for that too. --------------- this works ---------- import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter;
import javax.swing.ImageIcon; import javax.swing.JButton; 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;
class Changes3 extends JFrame implements ChangeListener, ActionListener {
private JSlider scale = new JSlider(-20, +20, 0); private JLabel position = new JLabel("Set Position"); private JButton nxtButton = new JButton("Next"); private ImageIcon picIcon; private JButton picButton; private JPanel myPanel; private static Changes3 egg; private Container contentArea; private BorderLayout myLayout; private JLabel labText; private static int jpgIndex = 0; private static final String[] jpgNames = { "pic1.jpg", "pic2.jpg", };
public Changes3() {
super("Slider"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true);
contentArea = getContentPane(); myLayout = new BorderLayout(); contentArea.setLayout(myLayout);
scale.setMajorTickSpacing(10); scale.setMinorTickSpacing(5); scale.setPaintTicks(true); scale.setPaintLabels(true);
scale.addChangeListener(this);
labText = new JLabel( "Look at the picture and indicate on the slider how you feel."); nxtButton.addActionListener(this);
myPanel = new JPanel(); myPanel.setBackground(Color.blue); picIcon = new ImageIcon(jpgNames[jpgIndex]); picButton = new JButton(picIcon); myPanel.add(picButton); if (++jpgIndex == jpgNames.length) jpgIndex = 0;
JPanel panel2 = new JPanel(); panel2.setBackground(Color.white); panel2.add(labText);
JPanel panel3 = new JPanel(); panel3.setBackground(Color.white); JLabel slidertext = new JLabel("-20 = Very Bad, +20 = Very Good"); panel3.add(slidertext); panel3.add(scale); panel3.add(nxtButton);
contentArea.add(myPanel, BorderLayout.NORTH); contentArea.add(panel2, BorderLayout.CENTER); contentArea.add(panel3, BorderLayout.SOUTH);
setContentPane(contentArea); }
public void stateChanged(ChangeEvent event) { JSlider src = (JSlider) event.getSource(); if (!src.getValueIsAdjusting()) position.setText("Position is " + scale.getValue()); }
public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == nxtButton) { tofile(); }
contentArea.remove(myPanel);
myPanel = new JPanel(); myPanel.setBackground(Color.blue); picIcon = new ImageIcon(jpgNames[jpgIndex]); picButton = new JButton(picIcon); myPanel.add(picButton); if (++jpgIndex == jpgNames.length) jpgIndex = 0;
contentArea.add(myPanel, BorderLayout.NORTH);
setContentPane(contentArea);
contentArea.setVisible(true); }
public void tofile() { 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); } // System.exit(0); }
public static void main(String[] args) { egg = new Changes3(); } }
Geoff Cox - 26 Jul 2005 08:02 GMT Joan,
I follow what you have done re jpgIndex etc but not clear why now when I add "true" to
FileWriter outfile = new FileWriter("data.txt", true);
the slider figures are not successively added to the data.txt file? They did before and I cannot see how your jpgIndex additions would affect this?!
Geoff
>This is a trick they use quite effectively in Java Script, but >not for java. [quoted text clipped - 148 lines] > } >} Geoff Cox - 26 Jul 2005 12:56 GMT Joan,
Must have been my mistake, slider values are now being appended to the file.
Cheers
Geoff
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|