import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JPanel;

/**
 * Draws "Hello World" directly onto this extended JPanel
 *
 * @author Roedy Green
 * @version 1.0
 */
class Draw extends JPanel
   {
   public Draw()
      {
      // arrange for background to be automatically drawn in the background colour
      this.setOpaque( true );
      }

   /**
    * does drawing
    *
    * @param g      where to paint
    */
   public void paintComponent ( Graphics g )
      {
      // this gets background cleared
      super.paintComponent();

      // ideally avoid rendering outside the clip region
      Rectangle r = g.getClipBounds();

      int x = 4;
      int y = 16;
      g.setColor( this.getForeground() );
      String text = "Hello World";
      g.drawString( text, x, y );
      x += g.getFontMetrics().stringWidth( text );

      // draw a red box
      g.setColor( Color.red );
      g.fillRect ( x, y, 10 /* width */, 10 /* height */ );
      x += 20;

      } // end paintComponent

   /**
    * get size to render the panel
    *
    * @return Dimensions needed for full rendering.
    */
   public Dimension getPreferredSize()
      {
      return new Dimension( 200, 30 );
      }
   }  // end class Draw