/*File Prob12 Copyright 2012 R.G.Baldwin
The purpose of this program is to demonstrate how to get
a color value from a PictureExplorer object and to display
it in JTextField object.
It also demonstrates how to access and use a reference
to a JFrame object that serves as the container for a
PictureExplorer object.
Finally, it demonstrates how to disable the X-button in
a PictureExplorer object.
Note that this program requires access to a modified
version of the PictureExplorier class.
*********************************************************/
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.awt.Color;
public class Prob12{
public static void main(String[] args){
new Prob12Runner();
}//end main method
}//end class Prob12
//======================================================//
class Prob12Runner extends JFrame{
private JFrame explorerFrame = null;
private PictureExplorer explorer = null;
private Picture pix;
private JPanel controlPanel = new JPanel();
private JPanel colorPanel = new JPanel();
private JPanel buttonPanel = new JPanel();
private JTextField redField = new JTextField("000000");
private JButton getDataButton = new JButton(
"Get and Display Red Color Value");
private String fileName = "Prob12.jpg";
//----------------------------------------------------//
public Prob12Runner(){//constructor
controlPanel.setLayout(new GridLayout(2,1));
controlPanel.add(colorPanel);
controlPanel.add(buttonPanel);
colorPanel.setBackground(Color.RED);
colorPanel.add(new JLabel(
"Red pixel Color at Cursor: "));
colorPanel.add(redField);
buttonPanel.setBackground(Color.BLUE);
buttonPanel.add(getDataButton);
getContentPane().add(controlPanel);
setTitle("Dick Baldwin");
setVisible(true);
//Create a Picture object and add a name to
// the picture before using it to construct the
// PictureExplorer object.
pix = new Picture(fileName);
pix.addMessage("Dick Baldwin",10,20);
//Create a PictureExplorer object containing the
// picture and set its default close operation.
explorer = new PictureExplorer(pix);
//Get a ref to the JFrame in the PictureExplorer
// object.
explorerFrame = explorer.getFrame();
explorerFrame.setDefaultCloseOperation(
WindowConstants.DO_NOTHING_ON_CLOSE);
//Set the size of the control GUI.
setSize(explorerFrame.getWidth(),110);
//Set the location for the control GUI immediately
// below the PictureExplorer object, and set its
// default close operation.
setLocation(0,explorerFrame.getHeight());
setDefaultCloseOperation(
WindowConstants.EXIT_ON_CLOSE);
//--------------------------------------------------//
//Register a listener object on the button.
//--------------------------------------------------//
getDataButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
redField.setText(explorer.getRValue());
}//end action performed
}//end newActionListener
);//end addActionListener
//--------------------------------------------------//
}//end constructor
}//end class Prob12Runner
|