import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxTest4 extends JFrame implements ItemListener {
    // the label the display the price
    private JLabel display = new JLabel();
    // a list of JCheckBox, with the the electronic names on it  
    // with the price
    private JCheckBox computer = new JCheckBox("Computer: $900");    
    private JCheckBox dvd = new JCheckBox("DVD: $250");
    private JCheckBox printer = new JCheckBox("Printer: $70");  
    private JCheckBox monitor = new JCheckBox("Monitor: $299");   
    private JPanel pricePanel = new JPanel(new GridLayout(2,2));
    private int price = 0;
    
    // constructor
    public CheckBoxTest4() {
	display.setOpaque(true);
	// set the background color
	display.setBackground(Color.white);
	// set the font color
	display.setForeground(Color.blue.darker());
	// use html file, <h1> .. </h1> set the font size 
	display.setText("<html><h1>The total price is $0</h1><html>");
	getContentPane().add(display);
	
	// add the components to pricePanel
	pricePanel.add(computer);
	pricePanel.add(dvd);
	pricePanel.add(printer);
	pricePanel.add(monitor);
	
	
	// setIcon set the icon for the checkbox
        // the icon is set to be a computer icon with a black cross
	// setSelectedIcon set the icon when you select the checkbox
	// the icon is set be a computer icon without cross
	computer.addItemListener(this);
	computer.setBackground(Color.white);
	computer.setIcon(new ImageIcon("images/computerx.jpg"));
	computer.setSelectedIcon(new ImageIcon("images/computer.jpg"));
	
	dvd.addItemListener(this);
	dvd.setBackground(Color.white);
	dvd.setIcon(new ImageIcon("images/dvdx.jpg"));
	dvd.setSelectedIcon(new ImageIcon("images/dvd.jpg"));
	
	printer.addItemListener(this);
	printer.setBackground(Color.white);
	printer.setIcon(new ImageIcon("images/printerx.jpg"));
	printer.setSelectedIcon(new ImageIcon("images/printer.jpg"));
	
	monitor.addItemListener(this);
	monitor.setBackground(Color.white);
	monitor.setIcon(new ImageIcon("images/monitorx.jpg"));
	monitor.setSelectedIcon(new ImageIcon("images/monitor.jpg"));
	
	getContentPane().add(pricePanel, BorderLayout.SOUTH);
    }
    
    public static void main(String[] args) {
        CheckBoxTest4 f = new CheckBoxTest4();
	f.setTitle("Bruin Computer Store");
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	f.pack();
	f.setVisible(true);
    }
    
    // override the method form ItemListener
    public void itemStateChanged(ItemEvent e) {
	// check the source first
	// find out the source and get the price change
	// if the source is selected, add the price change
	// if it is not selected, deducted the price change
	
	int change=0;
	if(e.getSource() == computer) {
	    change = 900;
	} else if (e.getSource() == dvd) {
	   change = 250;
	} else if(e.getSource() == printer) {
	    change = 70;
	} else if(e.getSource() == monitor) {
	    change =299;
	}
	
	if(e.getStateChange() == ItemEvent.SELECTED) {
	  price+=change;
	} else if(e.getStateChange() == ItemEvent.DESELECTED) {
	    price-=change;
	}
	display.setText("<html><h1>The total price is $" + price + "</h1></html>");
    }
	    
}
	
	
    
