/* @(#) GraphicsCanvas2D.java 1.1 99/11/21
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package com.vistech.graphics;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;

/** A graphics canvas class.
  * @version 1.0 11 july 1999
  * @author Lawrence Rodrigues
  **/
public class GraphicsCanvas2D extends JComponent implements GraphicsDisplay {
   protected BufferedImage offScrImage;
   protected Graphics2D offScrGc;
   protected Shape clipShape;
   protected int width = 0, height = 0;
   protected Color drawingColor = Color.green;
   protected Color fillColor = Color.blue, textColor = Color.green;
   protected Paint paint = null;
   protected Shape clip = null;
   protected AffineTransform xform = null;
   protected Stroke basicStroke = null;
   protected RenderingHints renderingHints = null;
   protected Composite composite = null;
   protected Font font = null;

   protected float strokeWidth = 1.0f;
   protected int capStyle = BasicStroke.CAP_BUTT;
   protected int joinStyle = BasicStroke.JOIN_MITER;
   protected int fontStyle, fontSize =10,fontType = 5;
   static String[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

   public GraphicsCanvas2D() {}

   public void setFontType(int type){
      fontType = type;
      Font fnt = new Font(allfonts[fontType], fontStyle, fontSize);
      setFont(fnt);
   }
   public int getFontType(){return fontType;}

   public synchronized void setFontSize(int sz){
      fontSize = sz;
      Font fnt = new Font(allfonts[fontType], fontStyle, fontSize);
      setFont(fnt);
   }
   public int getFontSize(){return fontSize;}

   public synchronized void setFontStyle(int style){
      fontStyle = style;
      Font fnt = new Font(allfonts[fontType], fontStyle, fontSize);
   }
   public int getFontStyle(){return fontStyle;}

   public synchronized void setFont(Font font) {this.font = font;}
   public Font getFont() {return font;}

   public synchronized void setDrawingColor(Color drawingColor) {this.drawingColor = drawingColor;}
   public Color getDrawingColor() { return drawingColor;}

   public synchronized void setFillColor(Color fillColor) { this.fillColor = fillColor;}
   public Color getFillColor() { return fillColor;}

   public synchronized void setTextColor(Color textColor) { this.textColor = textColor;}
   public Color getTextColor() { return textColor;}

   public synchronized void setComposite(Composite composite) {
      this.composite = composite;
   }
   public Composite getComposite() { return composite;}

   public synchronized void setPaint(Paint paint) { this.paint = paint;}
   public Paint getPaint() { return paint; }

   public synchronized void setTransform(AffineTransform transform) { this.xform = transform;}
   public AffineTransform getTransform() { return xform; }

   public synchronized void setCapStyle(int capStyle){
      this.capStyle = capStyle;
      basicStroke = new BasicStroke(strokeWidth, capStyle, joinStyle);
      offScrGc.setStroke(basicStroke);
   }
   public int getCapStyle(){ return capStyle;}

   public synchronized void setStrokeWidth(int enum){
      strokeWidth = 0.5f + 0.5f*enum;
      basicStroke = new BasicStroke(strokeWidth, capStyle, joinStyle);
      offScrGc.setStroke(basicStroke);
   }
   public int getStrokeWidth(){ return (int)strokeWidth;}

   public synchronized void setJoinStyle(int style) {
      joinStyle = style;
      basicStroke = new BasicStroke(strokeWidth, capStyle, joinStyle);
      offScrGc.setStroke(basicStroke);
   }
   public int getJoinStyle() { return joinStyle; }

   public synchronized void setStroke(Stroke stroke) { this.basicStroke = stroke; }
   public Stroke getStroke() { return basicStroke; }

   public synchronized void setRenderingHints(RenderingHints renderingHints) {
      this.renderingHints = renderingHints;
   }
   public RenderingHints getRenderingHints() { return renderingHints;}

   public void draw(Shape s){
      if(offScrGc != null) offScrGc.draw(s);
      repaint();
   }

   public synchronized void setClip(Shape clipShape){
      this.clipShape = clipShape;
   }
   public Shape getClip(){ return clipShape;}

   public BufferedImage getDisplayedImage(){ return offScrImage; }

   public Graphics2D getDisplayedImageGC(){ return offScrGc;}

   public void createOffScreenImage(int wid, int ht){
      offScrImage = new BufferedImage(wid, ht,BufferedImage.TYPE_INT_RGB);
      offScrGc = offScrImage.createGraphics();
      offScrGc.setColor(drawingColor);
      if(basicStroke != null) offScrGc.setStroke(basicStroke);
   }

   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D)g;
      Rectangle bounds = getBounds();
      if((width <=0) || (height <=0) ||
         (width != bounds.width) ||
         (height != bounds.height)){
         createOffScreenImage(bounds.width, bounds.height);
         width = bounds.width;
         height = bounds.height;
       }
       g2d.drawImage(offScrImage, 0,0,bounds.width, bounds.height, this);
   }

   public void reset(){
     Rectangle bounds = getBounds();
     createOffScreenImage(bounds.width, bounds.height);
     width = bounds.width;
     height = bounds.height;
     repaint();
  }
}
