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

public class HelloApplet extends JApplet {
	
	JButton colorButton;
	MyDrawPanel drawPanel;
	
	int circle_x;
	int circle_y;
	int circle_radius = 50;
	
  //Called when this applet is loaded into the browser.
 
	public void init() {
    		
		// create button and drawing canvas
		
		colorButton = new JButton("Change Circle");
		colorButton.addActionListener(new ColorButtonListener());

		drawPanel = new MyDrawPanel();

		// add both to applet content pane
		
		add(BorderLayout.SOUTH, colorButton);
		add(BorderLayout.CENTER, drawPanel);
	}

	class ColorButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			repaint();
		}
	}  
	
	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 centered

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

	}

}


