Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > display new image?

Reply
Thread Tools

display new image?

 
 
Geoff Cox
Guest
Posts: n/a
 
      07-25-2005
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();
}
}

 
Reply With Quote
 
 
 
 
Joan
Guest
Posts: n/a
 
      07-25-2005
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.

 
Reply With Quote
 
 
 
 
Geoff Cox
Guest
Posts: n/a
 
      07-25-2005
On Mon, 25 Jul 2005 11:49:53 -0500, "Joan" <(E-Mail Removed)>
wrote:

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

 
Reply With Quote
 
Joan
Guest
Posts: n/a
 
      07-25-2005

"Geoff Cox" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> On Mon, 25 Jul 2005 11:49:53 -0500, "Joan"
> <(E-Mail Removed)>
> wrote:
>
> Joan,
>
>><snip -- too much stuff to read>

>
> I have been taken to task before for not providing code which
> 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);

 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      07-25-2005
On Mon, 25 Jul 2005 12:39:12 -0500, "Joan" <(E-Mail Removed)>
wrote:

>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);
>
> // 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);


 
Reply With Quote
 
Joan
Guest
Posts: n/a
 
      07-25-2005

"Geoff Cox" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> On Mon, 25 Jul 2005 12:39:12 -0500, "Joan"
> <(E-Mail Removed)>
> wrote:
>
>>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


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


 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      07-25-2005
On Mon, 25 Jul 2005 15:38:36 -0500, "Joan" <(E-Mail Removed)>
wrote:

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
>>
>> 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

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


 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      07-25-2005
On Mon, 25 Jul 2005 15:38:36 -0500, "Joan" <(E-Mail Removed)>
wrote:

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.

>>
>> 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

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


 
Reply With Quote
 
Joan
Guest
Posts: n/a
 
      07-25-2005

"Geoff Cox" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> On Mon, 25 Jul 2005 15:38:36 -0500, "Joan"
> <(E-Mail Removed)>
> wrote:
>
> Joan,
>
> I have read through your code and just wish to thank you again.
> You
> have cleared up several questions for me.
>
> Many thanks!


Glad to help, I had a similar problem not too long ago so I had
some code to refer to.


> Cheers
>
> Geoff
>
>
>>>>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

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

>


 
Reply With Quote
 
Geoff Cox
Guest
Posts: n/a
 
      07-25-2005
On Mon, 25 Jul 2005 16:25:49 -0500, "Joan" <(E-Mail Removed)>
wrote:

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);
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript new-new-new-new-newbee weblinkunlimited@gmail.com Javascript 2 03-11-2008 01:15 AM
ASP.Net 2.0 Insert new item, then display this new item in Edit mo Ann ASP .Net Web Controls 0 03-28-2007 01:11 AM
choosing to display or not to display a checkbox in repeater control. Imran Aziz ASP .Net 4 08-11-2005 05:00 PM
Double double display display problem problem Tom Accuosti Firefox 3 09-27-2004 10:02 PM
Display and display model Marc Twain Java 1 01-13-2004 07:05 PM



Advertisments