// (c) MIT 2003.  All rights reserved.

// $Id: Screen.java,v 1.18 2003/03/06 00:40:39 moltmans Exp $
// Time-stamp: <2002-05-16 17:58:31 moltmans>
package rec.sys;


import java.awt.*;
import javax.swing.*;
import rec.util.*;
import rec.geo.*;

public class Screen extends JPanel implements Displayable
{
  private Surface m_Surface;


  static {
    Colors.setDefaultColor( "stroke-born", "stroke" );
  }

  public Screen( int xsize, int ysize )
  {
    this( null, xsize, ysize );
  }

  public Screen( Surface s, int xsize, int ysize )
  {
    m_Surface = s;

    setSize( xsize, ysize ); // Inherited from Canvas.

    setBackground( Colors.getBackground() );
    setForeground( Colors.getColor( "stroke" ) );
  }

  /////////////////////////////////////////////////////////////////////////////

  /**
   * Part of Displayable interface.
   *
   * Overriding paint() normally will allow the Screen to
   *   work with different types of Canvas's.
   *
   * This allows repaint() to be called properly while still
   *   maintaining the Displayable interface for any object
   *   that wants to use it.
   */
  public final void display( Graphics g )
  {
    paint( g );
  }

  /////////////////////////////////////////////////////////////////////////////

  public boolean hasSurface()
  {
    return( (m_Surface == null) ? false : true );
  }

  public void setSurface( Surface s )
  {
    m_Surface = s;
  }

  public Surface getSurface()
  {
    Assert.check( hasSurface(), "Screen has no Surface." );

    return( m_Surface );
  }

  /////////////////////////////////////////////////////////////////////////////


  public Rectangle getBounds()
  {
    return( new Rectangle( -1, -1, getWidth() + 1, getHeight() + 1 ) );
  }

  /*
  public void update( Graphics g )
  {
    paint(g);
  }
  */

  //  public void paint( Graphics graph )
  public void paintComponent( Graphics graph )
  {
    super.paintComponent( graph );

    Assert.note( hasSurface(),
		 "screen has no surface (Possible race condition)" );
    if ( !hasSurface() )
      return;

    Graphics2D g = (Graphics2D)graph;

    setBackground( Colors.getBackground() );
    setForeground( Colors.getColor( "stroke" ) );

    // LW: setup the font--HACK!
    Font f = g.getFont();
    f = new Font( f.getName(),
		  Font.BOLD | f.getStyle(), 18 ); //f.getSize() );
    if( f != null )
      g.setFont( f );
    else
      Console.writeln( "font not found" );

    //    if( buff == null )
    //      buff = createImage( getWidth(), getHeight() );

    Graphics2D bg = g; // (Graphics2D) buff.getGraphics();
    
    int strokeWidth = 4;
    try {
      strokeWidth = Integer.parseInt( m_Surface.getSystemProperty( "stroke-width",
								   "4" ) );
    } catch( NumberFormatException e ) {
    }
    
    bg.setStroke( new BasicStroke( strokeWidth ) );

    //    bg.clearRect( 0, 0, getWidth(), getHeight() );

    m_Surface.display( bg );

    //    g.drawImage( buff, 0, 0, null );

    //    bg.dispose();
    
    // m_Surface.display( g );
  }



  /**
   * Call with each point in a stroke.
   */
  public void showSquiggle( GPoint p1, GPoint p2 )
  {
    Graphics2D g = (Graphics2D) getGraphics();

    int strokeWidth = 4;
    try {
      strokeWidth = Integer.parseInt( m_Surface.getSystemProperty( "stroke-width",
								   "4" ) );
    } catch( NumberFormatException e ) {
    }
    
    g.setStroke( new BasicStroke( strokeWidth ) );
    g.setColor( Colors.getColor( "stroke-born" ) );

    g.drawLine( p1.getIntX(), p1.getIntY(), p2.getIntX(), p2.getIntY() );
  }

}

