source code: fillrect.java

import java.awt.*;
import java.awt.event.*;
class fillrect extends Frame implements ActionListener
{
Color col=Color.pink;
public fillrect()
{
FlowLayout fl=new FlowLayout();
setLayout(fl);
Button b1=new Button("red");
Button b2=new Button("green");
Button b3=new Button("blue");
add(b1);add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void paint(Graphics g)
{
g.setColor(col);
g.fillRect(100,100,200,50);
}
public void actionPerformed(ActionEvent ae)
{
String s=ae.getActionCommand();
System.out.println("bt pressed is "+s);
if(s.equals("red"))
col=Color.red;
if(s.equals("green"))
col=Color.green;
if(s.equals("blue"))
col=Color.blue;
repaint();
}
public static void main(String as[])
{
fillrect f=new fillrect();
f.setSize(300,300);
f.setVisible(true);
}
}

output: