import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.text.*;
import java.awt.image.*;
import java.awt.font.*;
import javax.imageio.*;
import javax.swing.ImageIcon;
import org.jibble.epsgraphics.*;


/**
 * @author  $Author: pjm2 $
 * @version $Id: TestMain.java,v 1.2 2003/11/29 17:50:21 pjm2 Exp $
 */
public class TestMain {
    
    public static void main(String[] args) throws Exception {
        
        // Draw on the EPS Graphics2D.
        Graphics2D g = new EpsGraphics2D("demo");
        paint(g);
        System.out.println(g.toString());
        
        // Draw on a standard Graphics2D.
        BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
        g = (Graphics2D) image.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, 500, 500);
        paint(g);
        ImageIO.write(image, "png", new File("./output.png"));
    }
    
    // Draws a few things on a Graphics2D (or EpsGraphics2D subclass)
    private static void paint(Graphics graphics) {
        Graphics2D g = (Graphics2D) graphics;
        
        //g.setClip(new Ellipse2D.Double(0, 0, 400, 400));
        //g.clipRect(100, 0, 500, 300);
        
        g.setBackground(Color.red);
        
        g.setColor(Color.decode("#ccccff"));
        g.setBackground(Color.white);
        g.setStroke(new BasicStroke(20.0f));
        g.setTransform(AffineTransform.getScaleInstance(1, 1));
        
        g.drawOval(0, 0, 500, 500);
        
        g.setStroke(new BasicStroke(1.0f));
        
        g.setColor(Color.gray);
        g.draw3DRect(5, 5, 30, 30, false);
        g.fill3DRect(45, 5, 30, 30, false);
        g.draw3DRect(85, 5, 30, 30, true);
        g.fill3DRect(125, 5, 30, 30, true);

        g.setColor(Color.black);        
        g.drawLine(20, 60, 100, 70);
        
        g.fillRect(20, 80, 30, 30);
        g.drawRect(40, 90, 30, 30);
        
        g.clearRect(20, 80, 10, 10);
        
        g.drawRoundRect(20, 130, 40, 40, 5, 5);
        g.fillRoundRect(80, 130, 40, 40, 5, 5);
        
        g.drawOval(20, 180, 30, 20);
        g.fillOval(60, 180, 30, 20);
        
        g.drawArc(20, 220, 40, 40, 0, 315);
        g.fillArc(80, 220, 40, 40, 0, 315);
        
        g.drawPolyline(new int[]{20, 40, 60, 80}, new int[]{280, 300, 290, 310}, 4);
        
        g.drawPolygon(new int[]{20, 40, 60, 50}, new int[]{340, 320, 340, 350}, 4);
        
        g.fillPolygon(new int[]{80, 100, 120, 110}, new int[]{340, 320, 340, 350}, 4);
        
        g.drawChars("toCharArray()".toCharArray(), 0, 13, 20, 390);
        g.drawBytes("getBytes()".getBytes(), 0, 10, 20, 430);
        
        
        // A more advanced test with a Venn 2
        Ellipse2D.Float j = new Ellipse2D.Float(200, 20, 100, 80);
        Ellipse2D.Float k = new Ellipse2D.Float(250, 20, 100, 80);
        Area jk = new Area(j);
        jk.intersect(new Area(k));
        g.setColor(Color.decode("#ffcccc"));
        g.fill(j);
        g.setColor(Color.decode("#ccffcc"));
        g.fill(k);
        g.setColor(Color.decode("#ffffcc"));
        g.fill(jk);
        g.setColor(Color.black);
        g.setStroke(new BasicStroke(2.0f));
        g.draw(j);
        g.draw(k);
        
        // Venn 3 unshaded
        Area a = new Area(new Ellipse2D.Float(200, 160, 100, 100));
        Area b = new Area(new Ellipse2D.Float(250, 160, 100, 100));
        Area c = new Area(new Ellipse2D.Float(225, 116.7f, 100, 100));
        
        Area shaded = new Area(b);
        shaded.intersect(c);
        shaded.subtract(a);
        g.setColor(Color.decode("#ccccff"));
        g.fill(shaded);

        g.setColor(Color.black);
        g.setStroke(new BasicStroke(2.0f));
        g.draw(a);
        g.draw(b);
        g.draw(c);
        
        // Venn 3 shaded
        a = new Area(new Ellipse2D.Float(200, 340, 100, 100));
        b = new Area(new Ellipse2D.Float(250, 340, 100, 100));
        c = new Area(new Ellipse2D.Float(225, 296.7f, 100, 100));

        g.setColor(Color.decode("#ffcccc"));
        g.fill(a);
        g.setColor(Color.decode("#ccffcc"));
        g.fill(b);
        g.setColor(Color.decode("#ccccff"));
        g.fill(c);

        Area ab = new Area(a);
        ab.intersect(b);
        g.setColor(Color.decode("#ffffcc"));
        g.fill(ab);
        
        Area ac = new Area(a);
        ac.intersect(c);
        g.setColor(Color.decode("#ffccff"));
        g.fill(ac);
        
        Area bc = new Area(b);
        bc.intersect(c);
        g.setColor(Color.decode("#ccffff"));
        g.fill(bc);
        
        Area abc = new Area(a);
        abc.intersect(b);
        abc.intersect(c);
        g.setColor(Color.decode("#ffffff"));
        g.fill(abc);
        

        g.setColor(Color.black);
        g.setStroke(new BasicStroke(1.0f));
        g.draw(a);
        g.draw(b);
        g.draw(c);
        
        // Draw some normal text.
        g.setFont(new Font("Serif", Font.ITALIC, 16));
        g.drawString("Hello, world!", 20, 50);
        
        // Draw some outlined text.
        AttributedString string = new AttributedString("EpsGraphics2D");
        string.addAttribute(TextAttribute.FONT, new Font("SansSerif", Font.PLAIN, 48));
        TextLayout layout = new TextLayout(string.getIterator(), g.getFontRenderContext());
        Shape textShape = layout.getOutline(AffineTransform.getTranslateInstance(100, 120));
        g.setStroke(new BasicStroke(1.0f));
        g.setColor(Color.decode("#ccccff"));
        g.fill(textShape);
        g.setColor(Color.black);
        g.draw(textShape);
        
        // Draw dashed rectangle around the outlined text.
        Rectangle2D boundingRect = textShape.getBounds2D();
        int curve = 10;
        RoundRectangle2D roundRect = new RoundRectangle2D.Double(boundingRect.getX() - curve, boundingRect.getY() - curve, boundingRect.getWidth() + curve * 2, boundingRect.getHeight() + curve * 2, curve, curve);
        g.setColor(Color.blue);
        g.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL, 1.0f, new float[]{10, 5}, 0));
        g.draw(roundRect);
        
        g.rotate(Math.PI / 2, 300, 300);
        g.drawString("Vertical Text", 300, 300);
        
    }
    
}