import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

// select the things by clicking on the picture
public class CheckBoxTest3 extends JFrame implements ActionListener {
    // 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 CheckBoxTest3() {
	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 a computer icon with a black cross
	// setSelectedIcon set the icon when you select the checkbox
	// the icon is set to a computer icon without cross
	computer.addActionListener(this);
	computer.setBackground(Color.white);
	computer.setIcon(new ImageIcon("images/computerx.jpg"));
	computer.setSelectedIcon(new ImageIcon("images/computer.jpg"));
	
	dvd.addActionListener(this);
	dvd.setBackground(Color.white);
	dvd.setIcon(new ImageIcon("images/dvdx.jpg"));
	dvd.setSelectedIcon(new ImageIcon("images/dvd.jpg"));
	
	printer.addActionListener(this);
	printer.setBackground(Color.white);
	printer.setIcon(new ImageIcon("images/printerx.jpg"));
	printer.setSelectedIcon(new ImageIcon("images/printer.jpg"));
	
	monitor.addActionListener(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) {
        CheckBoxTest3 f = new CheckBoxTest3();
	f.setTitle("Bruin Computer Store");
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	f.pack();
	f.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
	// check the source first
	// if the source is selected, than add the price
	// else, it is unselected, delete the price.
	if(e.getSource() == computer) {
	    if(computer.isSelected()) {
		price+=900;
	    } else {
		price-=900;
	    }
	} else if (e.getSource() == dvd) {
	    if(dvd.isSelected()) {
		price+=250;
	    } else {
		price-=250;
	    }
	} else if(e.getSource() == printer) {
	    if(printer.isSelected()){
		price+=70;
	    } else {
		price-=70;
	    }
	} else if(e.getSource() == monitor) {
	    if(monitor.isSelected()) {
		price+=299;
	    } else {
		price-=299;
	    }
	}
	// update the price
	display.setText("<html><h1>The total price is $" + price + "</h1></html>");
    }
	    
}
	
	
    
