import java.awt.*;
import javax.swing.*;

public class SwingComponentDemo {
    public static void main(String[] args) {
	// Create the main window with a title.
	JFrame myFrame = new JFrame("Swing Component Demo");

	// Setup the layout to hold up to 9 components each displayed on a row
	myFrame.getContentPane().setLayout(new GridLayout(9,1));

	// Button
	JButton button = new JButton("Test");

	// Canvas
	MyCanvas canvas = new MyCanvas();

	// JRadioButton
	JPanel sizePanel = new JPanel();
	ButtonGroup sizeGroup = new ButtonGroup();
	JRadioButton cbLarge = new JRadioButton("Large Pan Pizza"); 
	JRadioButton cbMedium = new JRadioButton("Medium Pan Pizza", true); 
	JRadioButton cbSmall = new JRadioButton("Small Pan Pizza");
	// add each radio button to the button group
	sizeGroup.add(cbLarge);
	sizeGroup.add(cbMedium); 
	sizeGroup.add(cbSmall);
	// add each button to the panel
	sizePanel.add(cbLarge); 
	sizePanel.add(cbMedium); 
	sizePanel.add(cbSmall);

	// JCheckBox
	JPanel toppingsPanel = new JPanel();
	toppingsPanel.add(new JCheckBox("Pineapple")); 
	toppingsPanel.add(new JCheckBox("Pepperoni")); 
	toppingsPanel.add(new JCheckBox("Mushroom")); 
	
	// JComboBox
	String[] toppings = {"Pineapple", "Pepperoni", "Black Olives", "Mushroom"};
	JComboBox<String> toppingsCombo = new JComboBox<String>(toppings); 
	
	// JLabel
	JLabel label = new JLabel("TEST LABEL!"); 

	// JList
	JList<String> toppingList = new JList<String>(toppings);

	// TextField
	JTextField textField = new JTextField(18);
	textField.setFont(new Font("Serif", Font.PLAIN, 12)); 
	textField.setText("Go ahead and type!");

	// TextArea
	JTextArea textArea = new JTextArea(2,10);
	textArea.setFont(new Font("Monospaced", Font.PLAIN, 18)); 
	textArea.setText("Mono\n12345678");
	//textArea.setEditable(false); 

	// create a DemoItem object for each component and add it to this frame
	DemoItem buttonDemo = new DemoItem("JButton:", button); 
	myFrame.getContentPane().add(buttonDemo);
	DemoItem canvasDemo = new DemoItem("JPanel:", canvas);
	myFrame.getContentPane().add(canvasDemo);
	DemoItem radioButtonDemo = new DemoItem("JRadioButton:", sizePanel);
	myFrame.getContentPane().add(radioButtonDemo);
	DemoItem toppingDemo = new DemoItem("JCheckBox:", toppingsPanel);
	myFrame.getContentPane().add(toppingDemo);
	DemoItem comboDemo = new DemoItem("JComboBox:", toppingsCombo);
	myFrame.getContentPane().add(comboDemo);
	DemoItem labelDemo = new DemoItem("JLabel:", label);
	myFrame.getContentPane().add(labelDemo);
	DemoItem listDemo = new DemoItem("JList:", toppingList);
	myFrame.getContentPane().add(listDemo);
	DemoItem TextFieldDemo = new DemoItem("JTextField:", textField);
	myFrame.getContentPane().add(TextFieldDemo);
	DemoItem TextAreaDemo = new DemoItem("JTextArea", textArea);
	myFrame.getContentPane().add(TextAreaDemo);

	myFrame.pack();
	myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	myFrame.setVisible(true);
    }
}


/** DemoItem class is a JPanel that contains a JLabel and a Component pair,
    to be displayed side-by-side. 
 */
class DemoItem extends JPanel {
    private JComponent comp; 
    private JLabel label;
    
    public DemoItem(String l, JComponent c) {
	comp = c;
	this.label = new JLabel(l);
	
	setLayout(new FlowLayout(FlowLayout.LEFT));
	add(this.label);
	add(comp);
    }
}

class MyCanvas extends JPanel {
    public Dimension getPreferredSize() {
	return new Dimension(300,40);
    }

    @Override
    public void paintComponent(Graphics g) {
	super.paintComponent(g);
	setBackground(Color.green);

	int x = 100, y = 15;
	for(int i = 0; i < 4; i++)
	    for(int j = 0; j < 4; j++)
		g.drawString("Canvas forever!", (i * x + (j * y)), j * y);
    }
}
