import java.awt.*;
import java.lang.*;
import java.io.*;
import java.applet.*;
import java.awt.event.*;

/*
<APPLET CODE="choicegraf.class" CODEBASE="." WIDTH=400 HEIGHT=300>
</APPLET>
*/

public class choicegraf extends Applet implements ItemListener
{
   TextField tf;
   Label botao;
   Choice ch;

   public void init()
   {
      resize(400,300);// área de exibição
      setLayout(new BorderLayout());

      tf = new TextField();    add("North",tf);
      Panel p = new Panel();   add("South",p);
      ch=new Choice();
      botao = new Label("Escolha ao lado");
      p.add(botao);

      ch.add("Texto");
      ch.add("Retângulo");
      ch.add("Ovalo");
      p.add(ch);ch.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent e)
    {
	     if (e.getItem() == "Texto")
		 {
					tf.setText(String.valueOf(e.getItem()));
				    repaint();
         }
         else
	     if (e.getItem() == "Retângulo")
	     {
			tf.setText(String.valueOf(e.getItem()));
	        repaint();
         }
         else
         if (e.getItem() == "Ovalo")
		 {
		 	tf.setText(String.valueOf(e.getItem()));
		    repaint();
		 }
    }

    public String getChoice()
	{
	        return ch.getSelectedItem();
	}

	public String getTextString()
	{
	        return tf.getText();
    }

    public synchronized void paint (Graphics g)
    {
       Dimension dm = size();
       String s = getChoice();
       int x,y,width,height;
       x = dm.width / 4;
       y = dm.height / 4;
       width = dm.width / 2;
       height = dm.height / 2;

        if (s.compareTo("Texto") == 0)
		{
		         String displayText = getTextString();
		         g.setColor(Color.red);
		         g.drawString(displayText,x,y + (height/2));
        }
        else
		if (s.compareTo("Retângulo") == 0)
		{
		         g.setColor(Color.blue);
		         g.drawRect(x,y,width,height);
		         g.setColor(Color.yellow);
		         g.fillRect(x+1,y+1, width - 1, height - 1);
		}
		if (s.compareTo("Ovalo") == 0)
		{
				 g.setColor(Color.magenta);
				 g.drawOval(x,y,width,height);
				 g.setColor(Color.yellow);
				 g.fillOval(x+1,y+1, width-1, height-2);
		}
     }
 }
