import java.awt.* ;
import java.awt.geom.* ;

public class Halloween {
  static final int winderSize = 480+2*20 ;
  static final int borderSize = 20 ;
  static final int swatchSize = winderSize-2*borderSize ;
  static final Color winderColor = new Color(  0, 200, 200) ;
  static final Color swatchColor = new Color(200, 200,   0) ;
  
  private static Picture demoPicture() {
    // return new Picture(winderSize, winderSize, winderColor) ;
    Picture  p = new Picture(winderSize, winderSize) ;
    p.setAllPixelsToAColor(winderColor) ;
    return p ;
  }
  
  private static void fillRoundRectDemo() {
    Picture  p = demoPicture() ;
    Graphics g = p.getGraphics() ;
    g.setColor(swatchColor) ;
    g.fillRoundRect(borderSize, borderSize,
                    swatchSize, swatchSize,
                    swatchSize/5, swatchSize/5) ;
    p.show() ;
    p.setTitle("fillRoundRect") ;
  }

  private static void fillTriangleDemo() {
    int[] xPoints = new int[3] ;
    int[] yPoints = new int[3] ;
    xPoints[0] = winderSize/2 ;
    yPoints[0] = borderSize ;
    xPoints[1] = (int)Math.round(winderSize/2.0 + (Math.sqrt(3)*swatchSize/4)) ;
    yPoints[1] = (2*winderSize + swatchSize+2)/4 ;
    xPoints[2] = winderSize - xPoints[1] ;
    yPoints[2] = yPoints[1] ;
    
    Picture  p = demoPicture() ;
    Graphics g = p.getGraphics() ;
    g.setColor(swatchColor) ;
    g.fillPolygon(xPoints, yPoints, 3) ;
    p.show() ;
    p.setTitle("fillPolygon") ;    
  }
  
  private static void fillTriangleTranslatedDemo() {
    int[] xPoints = new int[3] ;
    int[] yPoints = new int[3] ;
    xPoints[0] = swatchSize/2 ;
    yPoints[0] = 0 ;
    xPoints[1] = (int)Math.round(swatchSize/2.0 + (Math.sqrt(3)*swatchSize/4)) ;
    yPoints[1] = (3*swatchSize+2)/4 ;
    xPoints[2] = swatchSize - xPoints[1] ;
    yPoints[2] = yPoints[1] ;
    
    Picture  p = demoPicture() ;
    // Note the difference
    Graphics g = p.getGraphics().create() ;
    g.translate(borderSize, borderSize) ;
    g.setColor(swatchColor) ;
    g.fillPolygon(xPoints, yPoints, 3) ;
    p.show() ;
    p.setTitle("translated") ;    
  }
  
  private static void fillTriangleTransformedDemo() {
    // The path is within a unit square
    Path2D.Double tPath = new Path2D.Double() ;
    tPath.moveTo(0.5, 0) ;
    tPath.lineTo(0.5+Math.sqrt(3)/4, 0.75) ;
    tPath.lineTo(0.5-Math.sqrt(3)/4, 0.75) ;
    tPath.lineTo(0.5, 0) ;

    // Use a transform -- backwards ain't it
    AffineTransform tTrans = new AffineTransform() ;
    tTrans.translate(borderSize, borderSize) ;
    tTrans.scale(swatchSize, swatchSize) ;
    
    Picture  p = demoPicture() ;
    Graphics2D g = (Graphics2D) p.getGraphics().create() ;
    g.setColor(swatchColor) ;
    g.setTransform(tTrans) ;
    g.fill(tPath) ;
    p.show() ;
    p.setTitle("tranformed") ;    
  }

  private static void drawStringDemo() {
    Picture  p = demoPicture() ;
    Graphics g = p.getGraphics() ;
    g.setColor(swatchColor) ;
    String   m = "CSCI 201" ;
    g.setFont(new Font("Arial", Font.BOLD, 60)) ;
    int   mlen = g.getFontMetrics().stringWidth(m) ;
    g.drawString(m, (winderSize-mlen)/2, winderSize/2) ;
    p.show() ;
    p.setTitle("drawString") ; 
  }
  
  public static void drawImageDemo() {
    String  fn = "G:\\Pictures\\Halloween2.jpg" ;
    Picture fp = new Picture(fn) ;

    Picture  p = demoPicture() ;
    Graphics g = p.getGraphics() ;
    g.drawImage(fp.getImage(), borderSize, borderSize,
                swatchSize, swatchSize, swatchColor, null) ;
    p.show() ;
    p.setTitle("drawImage") ; 
  }

  private static void clipImageDemo() {
    // Start with the code to build the triangle path
    Path2D.Double tPath = new Path2D.Double() ;
    tPath.moveTo(0.5, 0) ;
    tPath.lineTo(0.5+Math.sqrt(3)/4, 0.75) ;
    tPath.lineTo(0.5-Math.sqrt(3)/4, 0.75) ;
    tPath.lineTo(0.5, 0) ;
    AffineTransform tTrans = new AffineTransform() ;
    tTrans.translate(borderSize, borderSize) ;
    tTrans.scale(swatchSize, swatchSize) ;
    
    Picture  p = demoPicture() ;
    Graphics2D g = (Graphics2D) p.getGraphics().create() ;
    g.setColor(swatchColor) ;
    
    // Clip with the triangle path
    g.setTransform(tTrans) ; 
    g.clip(tPath) ;
 
    // get rid of the old transform
    g.setTransform(new AffineTransform()) ;
    
    // Now display the image
    String  fn = "G:\\Pictures\\Halloween2.jpg" ;
    Picture fp = new Picture(fn) ;
    g.drawImage(fp.getImage(), borderSize, borderSize,
                swatchSize, swatchSize, swatchColor, null) ;
    
    p.show() ;
    p.setTitle("clipped") ;    
  }
    
  public static void main(String[] args) {
    fillRoundRectDemo() ;
    fillTriangleDemo() ;
    fillTriangleTranslatedDemo() ;
    fillTriangleTransformedDemo() ;
    drawStringDemo() ;
    drawImageDemo() ;
    clipImageDemo() ;
  }
}

  
  