package xmp_gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class CircleDemo2 extends JPanel implements Runnable,ActionListener {

	private static final long serialVersionUID = 1L;
	private MyCircle yellowLabel;

	public CircleDemo2() {
		super(); // If left out, this will be implicitly called

		// Define a Grow smaller button
		JButton grows = new JButton("Shrink<");
		add(grows);
		grows.setActionCommand("grows");
		grows.addActionListener(this);

		// Define a bump left button
		JButton bumpl = new JButton("<Bump");
		add(bumpl);
		bumpl.setActionCommand("bumpl");
		bumpl.addActionListener(this);

		//Create a yellow label to put in the Panel.
		yellowLabel = new MyCircle();
		yellowLabel.setOpaque(true);
		yellowLabel.setBackground(new Color(248, 213, 131));
		yellowLabel.setPreferredSize(new Dimension(200, 180));
		add(yellowLabel); // Add the yellowLabel to the panel

		// Define a bump right button
		JButton bumpr = new JButton("Bump>");
		add(bumpr);
		bumpr.setActionCommand("bumpr");
		bumpr.addActionListener(this);
		
		// Define a Grow bigger button
		JButton growb = new JButton(">Grow");
		add(growb);
		growb.setActionCommand("growb");
		growb.addActionListener(this);
	}

	public void run() {
		//Create and set up the window.
		JFrame frame = new JFrame("MyCircle Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Create the menu bar.  Make it have a green background.
		JMenuBar greenMenuBar = new JMenuBar();
		greenMenuBar.setOpaque(true);
		greenMenuBar.setBackground(new Color(154, 165, 127));
		greenMenuBar.setPreferredSize(new Dimension(200, 20));

		//Set the menu bar and reset the content pane to the current object.
		frame.setJMenuBar(greenMenuBar);
		frame.setContentPane(this);

		//Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		String ac=e.getActionCommand();
		if (ac.equals("bumpr")) yellowLabel.moveCenter(10,0);
		else if (ac.equals("bumpl")) yellowLabel.moveCenter(-10,0);
		else if (ac.equals("growb")) yellowLabel.grow(1.2);
		else if (ac.equals("grows")) yellowLabel.grow(0.8);
		else {
			System.out.println("Unrecognized action command string: " + ac + " ignored.");
		}
	}

	public static void main(String[] args) {

		CircleDemo2 cd2 = new CircleDemo2();    	
		javax.swing.SwingUtilities.invokeLater(cd2);
	}
}
