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

class MyDrawPanel extends JPanel {

	public void paintComponent(Graphics g) {

		// clear background
		
		Color backgroundColor = new Color(0, 0, 0);
		g.setColor(backgroundColor);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());

		// make random color for circle we're about to draw 
		
		int red = (int) (Math.random() * 255);
		int green = (int) (Math.random() * 255);
		int blue = (int) (Math.random() * 255);

		Color randomColor = new Color(red, green, blue);
		g.setColor(randomColor);
		
		// draw circle

		
		g.fillOval(this.getWidth()/2-TwoButtons.circle_radius,this.getHeight()/2-TwoButtons.circle_radius, 
					     2*TwoButtons.circle_radius, 2*TwoButtons.circle_radius);
	}

}

public class TwoButtons { // implements ActionListener {

	JFrame frame;
	JLabel label;
	JButton colorButton;
	JButton labelButton;
	static int circle_x = 100;
	
	boolean ouch = false;
	static int circle_radius = 50;
	
	public static void main (String[] args) {
		TwoButtons gui = new TwoButtons();
		gui.go();
	}

	/*
	public void actionPerformed(ActionEvent event) {
		//System.out.println("button!");
		if (event.getSource() == colorButton)
			frame.repaint();
		else
			System.out.println("label button");
	}
	*/
	
	public void go() {
		
		frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		colorButton = new JButton("Change Circle");
//		colorButton.addActionListener(this);
		colorButton.addActionListener(new ColorButtonListener());
		
		labelButton = new JButton("Change Label");
		labelButton.addActionListener(new LabelButtonListener());
//		labelButton.addActionListener(this);

		MyDrawPanel drawPanel = new MyDrawPanel();
//		JPanel buttonPanel = new JPanel();
//		buttonPanel.add(colorButton);
//		buttonPanel.add(labelButton);
		
		label = new JLabel("I'm a label");  
		frame.getContentPane().add(BorderLayout.NORTH, label);
	
		frame.getContentPane().add(BorderLayout.EAST, labelButton);
//		frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
		
		//frame.getContentPane().add(drawPanel);
		frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
		frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
		

		frame.setSize(300,300);  // 420
		frame.setVisible(true);
	}
	
	
	
	
	
	
	

	// why do we need inner classes?  because if we make TwoButtons implement ActionListener, then
	// we can't tell which button is generating the action
	
	class LabelButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			if (!ouch)
				label.setText("Ouch.......");
			else
				label.setText("I'm a label");
			ouch = !ouch;
		}
	} // close inner class

	class ColorButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			frame.repaint();
		}
	}  // close inner class

}




