net.paulhertz.aifile
Class BezEllipse

java.lang.Object
  extended by net.paulhertz.aifile.DisplayComponent
      extended by net.paulhertz.aifile.BezShape
          extended by net.paulhertz.aifile.BezEllipse
All Implemented Interfaces:
ColorableINF, Visitable

public class BezEllipse
extends BezShape

Provides factory methods to construct a closed elliptical BezShape. In methods that do not include a reference to a PApplet in the signature, the PApplet used for calls to the Processing environment is obtained from IgnoCodeLib, which must be correctly initialized in setup. If IgnoCodeLib does not have a reference to a PApplet, it throws a NullPointerException.

+Example
/**
 * March 14, 2012 3:35:47 PM CDT
 * Sample code for IgnoCodeLib Processing library by Paul Hertz
 * for IgnoCodeLib version 0.3.x and above
 * In this example, we use the various new methods in IgnoCodeLib 0.2 for creating ellipses and circles.
 * These methods belong to the new classes BezEllipse and BezCircle. You can't create an instance
 * of these classes with the new keyword because their constructors are protected. Instead, you call
 * one of their many "static factory methods" that return an instance of the class. See the code
 * below in createEllipses() and createCircles() for examples of each factory method.
 * Changes for IgnoCodeLib 0.3 include new package namespaces (net.paulhertz replaces com.ignotus),
 * and an IgnoCodeLib class that should be initialized on setup. When IgnoCodeLib is correctly initialized
 * as in "igno = new IgnoCodeLib(this);", you no longer need to pass a reference to the host PApplet 
 * to component constructors in IgnoCodeLib.  
 */

import net.paulhertz.aifile.*;
import net.paulhertz.geom.Matrix3;
import java.io.*;
import java.util.ArrayList;


/** list of ellipses */
public ArrayList ellipses; 
/** list of circles */
public ArrayList circles; 
/** list of colors, stored as Integer */
public ArrayList farben;
/** IgnoCodeLib instance for initializing the library */
IgnoCodeLib igno;


public void setup() {
  size(480, 720);
  smooth();
  // Set up the library, which will store a reference to the host PApplet 
  // for other classes in the library to use.
  igno = new IgnoCodeLib(this);
  ellipses = new ArrayList();
  circles = new ArrayList();
  initFarben();
  createEllipses();
  createCircles();
  println("Type 'x' to change drawing");
  println("Type 's' to save.");
}

public void draw() {
  background(127);
  // draw by stepping through the lists and calling draw() on each shape
  for (BezEllipse be : ellipses) {
    be.draw();
  }
  for (BezCircle bc : circles) {
    bc.draw();
  }
}


public void keyReleased() {
  if (key == 's' || key == 'S') {
    // save file
    ArrayList shapes = new ArrayList(ellipses);
    shapes.addAll(circles);
    String filename = "EllipsiCircles.ai";
    saveAI(filename, shapes, farben);
    println("saved" + "");
  }
  else if (key == 'x' || key == 'X') {
    // make colors, multiCurves and multiLines again
    initFarben();
    int[] ellipseChannelValues = {34, 55, 89, 144, 233};
    int[] circleChannelValues = {0, 127, 255};
    this.changeFillColors(circles, circleChannelValues);
    this.changeFillColors(ellipses, ellipseChannelValues);
  }
  else {
    ;
  }
}


/**
 * Generate some colors, save them in {@code farben}.
 */
private void initFarben() {
  farben = new ArrayList();
  // same as color(32, 64, 128, 255), but we can't use color() in straight Java
  int c = Palette.composeColor(32, 64, 128, 255);
  farben.add(c);
  c = Palette.composeColor(76, 123, 199, 255);
  int[] perm = Palette.colorPermutation(c);
  for (int pc : perm) {
    farben.add(pc);
  }
}


/**
 * Create some ellipses with various static factory methods belonging to 
 * class BezEllipse, save them in {@code ellipses} array.
 */
private void createEllipses() {
  int[] channelValues = {34, 55, 89, 144, 233};
  BezEllipse elly;
  stroke(0);
  int c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  elly = BezEllipse.makeLeftTopWidthHeight(20, 20, 100, 120);
  ellipses.add(elly);
  c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  elly = BezEllipse.makeCenterWidthHeight(width/2, height/2, 120, 100);
  ellipses.add(elly);
  c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  elly = BezEllipse.makeLeftTopRightBottom(200, 20, 400, 180);
  ellipses.add(elly);
  c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  elly = BezEllipse.makeLeftTopWidthHeightSectors(20, 420, 240, 160, 5);
  ellipses.add(elly);
  c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  elly = BezEllipse.makeCenterWidthHeightSectors(width/2,  7 * height/8, 160, 90, 6);
  ellipses.add(elly);
  c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  elly = BezEllipse.makeLeftTopRightBottomSectors(360, 420, 440, 640, 7);
  ellipses.add(elly);
}


/**
 * Create some circles with various static factory methods belonging to
 * class BezCircle, save them in {@code circles} array.
 */
public void createCircles() {
  int[] channelValues = {0, 127, 255};
  BezCircle circ;
  stroke(255);
  int c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  circ = BezCircle.makeCenterRadius(100, 240, 60);
  Matrix3 matx = new Matrix3();
  // throw in a transform
  matx.translateCTM(-circ.xctr(), -circ.yctr());
  float angle = (float) (Math.toRadians(30));
  matx.rotateCTM(angle);
  matx.translateCTM(circ.xctr(), circ.yctr());
  circ.transform(matx);
  circles.add(circ);
  c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  circ = BezCircle.makeCenterRadiusSectors(100, 240, 40, 5);
  circles.add(circ);
  c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  circ = BezCircle.makeLeftTopRadius(180, 200, 40);
  circles.add(circ);
  c = Palette.randColor(channelValues);
  fill(c);
  farben.add(c);
  circ = BezCircle.makeLeftTopRadiusSectors(180, 200, 20, 6);
  circles.add(circ);
}

/**
 * @param shapes          a list of shapes
 * @param channelValues   an array of possible values for color channels
 */
public void changeFillColors(ArrayList shapes, int[] channelValues) {
  for (BezShape sh : shapes) {
    int c = Palette.randColor(channelValues);
    farben.add(c);
    sh.setFillColor(c);
  }
}

/**
 * Saves shapes to an Adobe Illustrator file.
 * @param aiFilename      name of the file, should end with ".ai"
 * @param components      a list of DisplayComponents (any kind except DocumentComponent)
 * @param paletteColors   a list of Integers (colors)
 */
public void saveAI(String aiFilename, ArrayList components, ArrayList paletteColors) {
  // println("saving Adobe Illustrator file " + aiFilename + "...");
  PrintWriter output = createWriter(aiFilename);
  DocumentComponent doc = new DocumentComponent("Ellipses and Circles");
  // get lots of feedback as we save
  doc.setVerbose(true);
  Palette pal = doc.getPalette();
  // include black and white in the palette
  pal.addBlackWhiteGray();
  // add our colors
  pal.addColors(paletteColors);
  doc.setCreator("Ignotus");
  doc.setOrg("IgnoStudio");
  doc.setWidth(width);
  doc.setHeight(height);
  for (DisplayComponent comp : components) {
    doc.add(comp);
  }
  // write the file, transforming geometry from the Processing coordinate system 
  // into the Adobe Illustrator coordinate system
  doc.write(output);  
}



Nested Class Summary
 
Nested classes/interfaces inherited from class net.paulhertz.aifile.BezShape
BezShape.BezType
 
Field Summary
 
Fields inherited from class net.paulhertz.aifile.BezShape
bezType, CURVE_SEGMENT, KAPPA, LINE_SEGMENT
 
Method Summary
static BezEllipse makeCenterWidthHeight(float xctr, float yctr, float width, float height)
           
static BezEllipse makeCenterWidthHeight(PApplet parent, float xctr, float yctr, float width, float height)
           
static BezEllipse makeCenterWidthHeightSectors(float xctr, float yctr, float width, float height, int sectors)
           
static BezEllipse makeCenterWidthHeightSectors(PApplet parent, float xctr, float yctr, float width, float height, int sectors)
           
static BezEllipse makeLeftTopRightBottom(float left, float top, float right, float bottom)
           
static BezEllipse makeLeftTopRightBottom(PApplet parent, float left, float top, float right, float bottom)
           
static BezEllipse makeLeftTopRightBottomSectors(float left, float top, float right, float bottom, int sectors)
           
static BezEllipse makeLeftTopRightBottomSectors(PApplet parent, float left, float top, float right, float bottom, int sectors)
           
static BezEllipse makeLeftTopWidthHeight(float left, float top, float width, float height)
           
static BezEllipse makeLeftTopWidthHeight(PApplet parent, float left, float top, float width, float height)
           
static BezEllipse makeLeftTopWidthHeightSectors(float left, float top, float width, float height, int sectors)
           
static BezEllipse makeLeftTopWidthHeightSectors(PApplet parent, float left, float top, float width, float height, int sectors)
           
 
Methods inherited from class net.paulhertz.aifile.BezShape
accept, accept, add, add, append, append, append, asPolygon, asPolygon, asPolygon, asPolygon, bezCircle, bezCurve, bezCurveShape, bezEllipse, bezLine, bezMultiCurve, bezMultiLine, bezPoly, bezRectangle, bezRegularPoly, bezTriangle, bezTriangle, bezType, bounds, bounds, boundsRect, calculateCenter, centerVertex, children, clone, containsPoint, containsPoint, curveIterator, curves, curvesCopy, draw, draw, drawQuick, fillColor, fillOpacity, get, getAnchorCenter, getBoundsCenter, getCoords, getCtm, getGeoCenter, getMatrix, hasFill, hasStroke, isClosed, isTerminal, iterator, moveTo, polySize, polySize, polySteps, remove, rotateShape, rotateShape, scaleShape, scaleShape, scaleShape, scaleShape, setBezType, setCenter, setCenter, setCenter, setColors, setCtm, setCtm, setCtm, setCurves, setFillColor, setFillOpacity, setHasFill, setHasStroke, setIsClosed, setNoFill, setNoStroke, setPolySteps, setStartPoint, setStartPoint, setStartPoint, setStrokeColor, setStrokeOpacity, setUseTransparency, setWeight, setX, setY, startVertex, startVertexArray, strokeColor, strokeOpacity, transform, transform, transformShape, translateShape, useTransparency, weight, write, write, x, xcoords, xcoords, xctr, y, ycoords, ycoords, yctr
 
Methods inherited from class net.paulhertz.aifile.DisplayComponent
hide, id, isLocked, isVisible, parentComponent, setLocked, setParentComponent, setVisible, show
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

makeLeftTopWidthHeight

public static BezEllipse makeLeftTopWidthHeight(PApplet parent,
                                                float left,
                                                float top,
                                                float width,
                                                float height)

makeLeftTopWidthHeight

public static BezEllipse makeLeftTopWidthHeight(float left,
                                                float top,
                                                float width,
                                                float height)

makeCenterWidthHeight

public static BezEllipse makeCenterWidthHeight(PApplet parent,
                                               float xctr,
                                               float yctr,
                                               float width,
                                               float height)

makeCenterWidthHeight

public static BezEllipse makeCenterWidthHeight(float xctr,
                                               float yctr,
                                               float width,
                                               float height)

makeLeftTopRightBottom

public static BezEllipse makeLeftTopRightBottom(PApplet parent,
                                                float left,
                                                float top,
                                                float right,
                                                float bottom)

makeLeftTopRightBottom

public static BezEllipse makeLeftTopRightBottom(float left,
                                                float top,
                                                float right,
                                                float bottom)

makeLeftTopWidthHeightSectors

public static BezEllipse makeLeftTopWidthHeightSectors(PApplet parent,
                                                       float left,
                                                       float top,
                                                       float width,
                                                       float height,
                                                       int sectors)

makeLeftTopWidthHeightSectors

public static BezEllipse makeLeftTopWidthHeightSectors(float left,
                                                       float top,
                                                       float width,
                                                       float height,
                                                       int sectors)

makeCenterWidthHeightSectors

public static BezEllipse makeCenterWidthHeightSectors(PApplet parent,
                                                      float xctr,
                                                      float yctr,
                                                      float width,
                                                      float height,
                                                      int sectors)

makeCenterWidthHeightSectors

public static BezEllipse makeCenterWidthHeightSectors(float xctr,
                                                      float yctr,
                                                      float width,
                                                      float height,
                                                      int sectors)

makeLeftTopRightBottomSectors

public static BezEllipse makeLeftTopRightBottomSectors(PApplet parent,
                                                       float left,
                                                       float top,
                                                       float right,
                                                       float bottom,
                                                       int sectors)

makeLeftTopRightBottomSectors

public static BezEllipse makeLeftTopRightBottomSectors(float left,
                                                       float top,
                                                       float right,
                                                       float bottom,
                                                       int sectors)


Processing library IgnoCodeLib by Paul Hertz. (C) 2013