source file name:  AppletCanvasShapesDemo.java

 /*
<applet code=AppletCanvasShapesDemo.class width=300 height=300 >
</applet >
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;


public class AppletCanvasShapesDemo extends Applet implements ItemListener
{
Choice ch;
Color col=null;
ShapeDisplay ob=null;
public AppletCanvasShapesDemo()
{
FlowLayout fl=new FlowLayout();
setLayout(fl);
 ch=new Choice();
Label lb=new Label("Select Color : ");
ch.add("Red");
ch.add("Green");
ch.add("Blue");
ch.add("Cyan");
ch.add("Pink");
add(lb); add(ch);
ch.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
String  color=ch.getSelectedItem();
System.out.println(" select  name is: "+color);

if(color.equals("Red"))
{
col=Color.red;
}
if(color.equals("Green"))
{
col=Color.green;
}
if(color.equals("Pink"))
{
col=Color.pink;
}
if(color.equals("Blue"))
{
col=Color.blue;
}
repaint();

ob=new ShapeDisplay(col);
add(ob);
}

class ShapeDisplay extends Canvas
{
Color col=Color.red;
public ShapeDisplay( Color c)
{
col=c;
setSize(300,300);
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(col);
g.fillRect(100,100,50,150);
}
}// end of canvas class


}// end of Main Applet class

 output: