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

public class djalogo
   extends Applet
   implements ActionListener, AdjustmentListener, ItemListener {

   static int	blockSize = 50;

   static Color[] colors = {
      Color.black,
      Color.blue,
      Color.cyan,
      Color.darkGray,
      Color.gray,
      Color.green,
      Color.lightGray,
      Color.magenta,
      Color.orange,
      Color.pink,
      Color.red,
      Color.white,
      Color.yellow
   };

   static String[] 	colorNames = {
      "black",
      "blue",
      "cyan",
      "dark gray",
      "gray",
      "green",
      "light gray",
      "magenta",
      "orange",
      "pink",
      "red",
      "white",
      "yellow"
   };

   static int	maxX = blockSize * 7;
   static int	maxY = blockSize * 3;
   static int	pad = (int) (blockSize / 2);
   static int	paddedX = maxX + (pad * 2);
   static int	paddedY = maxY + (pad * 2);

   Choice	backChoice;
   Panel	controls;
   Panel	controls1;
   Panel	controls2;
   Button	draw;
   Choice	foreChoice;
   Label	onLabel;
   Panel	logo;
   Label	sizeLabel;
   Scrollbar	sizeScroll;
   Label	widthLabel;


   public void init() {

      int	i;

      // create user interface items -- should find a better layout mangler!
      backChoice = new Choice();
      controls = new Panel();
      controls1 = new Panel();
      controls2 = new Panel();
      draw = new Button ("Draw!");
      draw.setSize (maxX / 10, 20);
      foreChoice = new Choice();
      logo = new Panel ();
      logo.setSize (maxX, maxY);
      onLabel = new Label ("on");
      sizeLabel = new Label ("1");
      // make scrollbar able to put LEFT side of thumb at max legal value.
      // also should find some way to stretch it....
      sizeScroll = new Scrollbar (Scrollbar.HORIZONTAL,
				  1,
				  (int) (blockSize / 5.0),
				  1,
				  (int) (blockSize * 1.2));
      sizeScroll.setSize (maxX / 2, 20);
      widthLabel = new Label ("Line Width:");

      // load choice strings
      for (i = 0; i < colors.length; i++) {
	 backChoice.add (colorNames[i]);
	 foreChoice.add (colorNames[i]);
      }

      // set default selections
      backChoice.select ("white");
      foreChoice.select ("black");

      // add controls to that panel
      controls.setLayout (new BorderLayout());
      controls1.add (foreChoice);
      controls1.add (onLabel);
      controls1.add (backChoice);
      controls2.add (widthLabel);
      controls2.add (sizeScroll);
      controls2.add (sizeLabel);
      controls.add (controls1, BorderLayout.NORTH);
      controls.add (controls2, BorderLayout.CENTER);
      controls.add (draw, BorderLayout.SOUTH);

      // add panels into applet
      setLayout (new BorderLayout());
      add (logo, BorderLayout.CENTER);
      add (controls, BorderLayout.SOUTH);

      // register event handlers
      backChoice.addItemListener (this);
      draw.addActionListener (this);
      foreChoice.addItemListener (this);
      sizeScroll.addAdjustmentListener (this);

      // paint in initial config
      repaint();
   }


   // yes, horribly un-oop way to accomplish this, but GeoAtrocities
   // rejects the file that results from declaring an inner class,
   // because javac puts a $ in the name!  B-(
   public void paint (Graphics g) { paintLogo(); }


   public void actionPerformed (ActionEvent evt) { paintLogo(); }


   public void adjustmentValueChanged (AdjustmentEvent e) {
      paintLogo();
   }


   public void itemStateChanged (ItemEvent e) { paintLogo(); }


   public void paintLogo() {
      Color	backColor = colors[backChoice.getSelectedIndex()];
      int	boxIn;
      int	boxOut = blockSize * 2;
      Color	foreColor = colors[foreChoice.getSelectedIndex()];
      Graphics	g = logo.getGraphics();
      int	size = sizeScroll.getValue();

      // clear to background color
      g.setColor (backColor);
      g.fillRect (0, 0, paddedX, paddedY);

      // paint size label
      sizeLabel.setText (Integer.toString (size));

      // do other setup things needing the size
      boxIn = (blockSize - size) * 2;

      // set up to draw foreground
      g.setColor (foreColor);

      // side bar of d
      g.fillRect (pad, pad, size, blockSize * 3);

      // top arc of d
      g.fillArc (size - blockSize + pad, pad, boxOut, boxOut, 0, 90);
      g.setColor (backColor);
      g.fillArc (size * 2 - blockSize + pad, size + pad,
		 boxIn, boxIn, 0, 90);
      g.setColor (foreColor);

      // bottom arc of d
      g.fillArc (size - blockSize + pad, blockSize + pad,
		 boxOut, boxOut, -90, 90);
      g.setColor (backColor);
      g.fillArc (size * 2 - blockSize + pad, blockSize + size + pad,
		 boxIn, boxIn,
		 -90, 90);
      g.setColor (foreColor);

      // side of d
      g.fillRect (blockSize + pad, blockSize + pad, size, blockSize);


      // bottom block of j
      g.fillRect (blockSize + size + pad, blockSize * 3 - size + pad,
		  size, size);

      // bottom arc of j
      g.fillArc (size * 2 + pad, blockSize + pad, boxOut, boxOut, -90, 90);
      g.setColor (backColor);
      g.fillArc (size * 3 + pad, blockSize + size + pad,
		 boxIn, boxIn, -90, 90);
      g.setColor (foreColor);

      // shaft of j
      g.fillRect (blockSize * 2 + size + pad, pad, size, blockSize * 2);


      // bottom block of a
      g.fillRect (boxOut + size * 2 + pad, blockSize * 3 - size + pad,
		  size, size);

      // bottom arc of a
      g.fillArc (blockSize + size * 3 + pad, blockSize + pad,
		 boxOut, boxOut,
		 -90, 90);
      g.setColor (backColor);
      g.fillArc (blockSize + size * 4 + pad, blockSize + size + pad,
		 boxIn, boxIn,
		 -90, 90);
      g.setColor (foreColor);

      // left side of a
      g.fillRect (blockSize * 3 + size * 2 + pad, blockSize + pad,
		  size, blockSize);

      // upper arc of a
      g.fillArc (blockSize * 3 + size * 2 + pad, pad,
		 boxOut, boxOut,
		 90, 90);
      g.setColor (backColor);
      g.fillArc (blockSize * 3 + size * 3 + pad, size + pad,
		 boxIn, boxIn,
		 90, 90);
      g.setColor (foreColor);

      // cross of a
      g.fillRect (blockSize * 3 + size * 3 + pad,
		  (int) (blockSize * 1.5 - size/2.0) + pad,
		  blockSize - size, size);

      // right of a
      g.fillRect (blockSize * 4 + size * 2 + pad, pad, size, blockSize * 3);
   }
}
