/* @(#) SimpleGraphicsCanvas2D.java 1.1 99/7/11
 * 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 simple graphics canvas.
  * @version 1.0 11 july 1999
  * @author Lawrence Rodrigues
  **/
public class SimpleGraphicsCanvas2D extends JComponent {
   protected BufferedImage offScrImage;
   protected Graphics2D offScrGc;
   protected int width = 0;
   protected int height = 0;
   protected Color drawingColor = Color.green;

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

   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);
   }

   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);
   }
}
