public class Page
{
  protected int height;
  protected Line lines[];
  protected int width;

  public Page(int height, int width) throws PlotException
  {
    if (height <= 0)
    {
      throw new PlotException("Bad height " + height);
    }
    else
    {
      this.height = height;
      this.width = width;
      lines = new Line[height];
      for (int i = 0; i < height; i++)
      {
        lines[i] = new Line(width, ' ');
      }
    }
  }
   
 
  public Page(int height, int width, char fill) throws PlotException
  {
    if (height <= 0)
    {
      throw new PlotException("Bad height " + height);
    }
    else
    {
      this.height = height;
      this.width = width;
      lines = new Line[height];
      for (int i = 0; i < height; i++)
      {
        lines[i] = new Line(width, fill);
      }
    }
  }

  public String toString()
  {
    String retval = "";

    for (int i = 0; i < height; i++)
    {
      retval = retval + lines[i] + '\n';
    }
    return retval;
  }

  public void setPixel(int x, int y, char c)
  {
    if (y >= 0 && y < height)
    {
      lines[y].setLocation(x, c);
    }
    return;
  }

  public char getPixel(int x, int y)
  {
    char retval;

    if (y >= 0 && y < height)
    {
      retval = lines[y].getLocation(x);
    }
    else
    {
      retval = ' ';
    }
    return retval;
  }   

  public void hLine(int x1, int x2, int y, char c)
  {
    for (int x = min(x1, x2) ; x <= max(x1, x2); x++)
    {
      this.setPixel(x, y, c);
    }
  }

  public void vLine(int x, int y1, int y2, char c)
  {
    for (int y = min(y1, y2); y <= max(y1, y2); y++)
    {
      this.setPixel(x, y, c);
    }
  }

  public void rectangle(int x1, int y1, int x2, int y2, char c)
  {
    this.hLine(x1, x2, y1, c);
    this.hLine(x1, x2, y2, c);
    this.vLine(x1, y1, y2, c);
    this.vLine(x2, y1, y2, c);
  }

  public void fillRectangle(int x1, int y1, int x2, int y2, char c)
  {
    for (int y = min(y1, y2); y <= max(y1, y2); y++)
    {
      this.hLine(x1, x2, y, c);
    }
  }

 public void line(int x1, int y1, int x2, int y2, char c)
 {
   int y;
   for (int x = min(x1, x2); x <= max(x1, x2); x++)
   {
     y = y2 - (x2 - x) * (y2 - y1) / (x2 - x1);
     this.setPixel(x, y, c);
   }
 } 

  public static void main(String args[]) throws PlotException
  {
    Page p1 = new Page(20, 15);


    p1.hLine(2,12, 12, '%');
    p1.vLine(2, 12, 2,  '*');
    System.out.println(p1);

  }


  protected int max(int a, int b)
  {
    if (a > b)
      return a;
    else
      return b;
  }

  protected int min(int a, int b)
  {
    if (a > b)
      return b;
    else
      return a;
  }

}
