|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.paulhertz.aifile.DisplayComponent
net.paulhertz.aifile.DocumentComponent
public class DocumentComponent
Handles the root level of the document hierarchy. All the children of a document component are layer components.
Any other components you add will be placed in the default layer. A document hierarchy can have only one DocumentComponent
,
at the top of the hierarchy. The document can act as a display list, by cascading draw()
commands
down the document tree, or as a file output list, similarly cascading the write(PrintWriter)
command.
Processing and Adobe Illustrator use different coordinate systems. To
/** * January 29, 2012 3:03:31 PM CST * Sample code for IgnoCodeLib 0.3 Processing library by Paul Hertz * Updated June 25, 2013 for IgonoCodeLib 0.3 release. * The writeNoTransform call is only available in 0.3 and the packages have been renamed * from com.ignotus to net.paulhertz in the import section. Calls to component constructors * no longer require you to pass in a reference to the host PApplet, if you initialize * the library correctly as in "igno = new IgnoCodeLib(this);" * Shows how to create a document tree with layers, groups, and shapes and use it to draw * to the screen, perform a global transform, and export to file. */ import java.util.*; import java.io.*; import java.awt.geom.Point2D; import net.paulhertz.aifile.*; // library that handles Adobe Illustrator document structure and export import net.paulhertz.geom.*; // library for geometric transforms and matrices /** list of colors */ ArrayListfarben; /** a geometric transform */ Matrix3 aiTransform; String fileName = "documentExport.ai"; /** document component, root of the document structure */ DocumentComponent doc; /** IgnoCodeLib library */ IgnoCodeLib igno; public void setup() { size(480,720); background(127); 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); farben = new ArrayList (); setupTransform(); // we'll create the document up front and use it for drawing and saving createDocument(); println("type 's' to output file"); } public void draw() { // the document knows how to draw itself--so do layers and groups and shapes, but if they are // part of the document, we just have to tell the document to draw. doc.draw(); } /** * Processing and Illustrator use different coordinate systems. We can move from one to * the other by applying a geometric transform to our graphics before outputting to Illustrator. * The DocumentComponent class now performs the transform automatically when you call write, so * this method is here for informational purposes. * See the SaveAI() method for more information. * 2-D geometric transforms can be captured by a matrix with three rows and three columns. * The class net.paulhertz.geom.Matrix3 handles transforms with such matrices. * In its initial state, a Matrix3 is the "identity matrix," to which we add various transforms. * The matrix transforms 2D geometry through the operation of matrix multiplication. The * nuts and bolts need not concern you: the essential thing to understand is that you can * add a series of geometric transforms to the matrix and then execute them all at once. * The matrix is also referred to as the CTM or Current Transoformation Matrix. * This transform is also available directly from the document through the getAITransform() method. */ public void setupTransform() { // start with the identity matrix aiTransform = new Matrix3(); // add a horizontal reflection around x = 0 aiTransform.scaleCTM(1.0, -1.0); // and translate by "height" distance on the y-axis aiTransform.translateCTM(0, height); } /** * Creates a document tree with layers, groups, and shapes * Note that the Document, Layer, and Group components need to be * initialized with "this" (the PApplet that runs the show) as the first argument. */ public void createDocument() { float x, y, radius, h, w; int sides; color c; // initialize an integer array with values constrained to 0..255 int[] channelValues = {34, 55, 89, 144, 233}; float minX = width * 0.125; float maxX = width * 0.875; float minY = height * 0.125; float maxY = height * 0.875; // create a document doc = new DocumentComponent("Layers and Groups"); // by setting verbose to true, we get some feedback as we add components doc.setVerbose(true); // create and add a layer LayerComponent bg = new LayerComponent("background"); doc.add(bg); // add some geometry to the layer bg.add(bgRect()); // create another layer and add it to the document LayerComponent polygons = new LayerComponent("Polygons"); doc.add(polygons); // create some geometry and add it to the polygons layer ArrayList bezzies = new ArrayList (); for (int i = 0; i < 10; i++) { x = random(minX, maxX); y = random(minY, maxY); radius = random(2, 48); sides = int(random(3, 18)); BezShape bez = BezRegularPoly.makeCenterRadiusSides(x, y, radius, sides); c = Palette.randColor(channelValues); farben.add(c); bez.setFillColor(c); bez.setNoStroke(); bezzies.add(bez); polygons.add(bez); } // create a group and add it to the polygons layer GroupComponent boxes = new GroupComponent(this); polygons.add(boxes); // step through the polygons we created and put a bounding box around each one // add the bounding boxes to the group for (BezShape b : bezzies) { float[] bounds = b.bounds(this); float left = bounds[0]; float top = bounds[1]; float right = bounds[2]; float bottom = bounds[3]; color s = b.fillColor(); BezShape br = bezRect(left, top, right, bottom, color(0), s); br.setNoFill(); br.setWeight(2); boxes.add(br); } // create a new layer and add it to the document LayerComponent ellipses = new LayerComponent("Ellipses"); doc.add(ellipses); // add some geometry to the layer for (int i = 0; i < 10; i++) { x = random(minX, maxX); y = random(minY, maxY); w = random(16, 96); h = random(16, 96); sides = int(random(4, 7)); BezShape bez = BezEllipse.makeCenterWidthHeightSectors(x, y, w, h, sides); c = Palette.randColor(channelValues); farben.add(c); bez.setFillColor(c); bez.setNoStroke(); ellipses.add(bez); } } void keyPressed() { if (key == 's') { saveAI(fileName, doc, farben); } } /** * Creates a rectangle from four points, with fill and stroke colors */ public BezShape bezRect(float left, float top, float right, float bottom, color f, color s) { // instatiate a BezShape with a reference to our PApplet // and the x and y coordinates of its starting vertex. BezShape r = new BezShape(left, top); r.setFillColor(f); r.setStrokeColor(s); r.setWeight(3.0); r.append(right, top); r.append(right, bottom); r.append(left, bottom); r.append(left, top); return r; } public BezShape bgRect() { color f = color(233, 220, 254); color s = color(21, 34, 55); return bezRect(0, height, width, 0, f, s); } /** * saves shapes to an Adobe Illustrator file */ public void saveAI(String aiFilename, DocumentComponent doc, ArrayList colors) { println("saving Adobe Illustrator file " + aiFilename + "..."); PrintWriter output = createWriter(aiFilename); Palette pal = doc.getPalette(); pal.addBlackWhiteGray(); pal.addColors(colors); doc.setCreator("Ignotus"); doc.setOrg("IgnoStudio"); doc.setWidth(width); doc.setHeight(height); // the transform we created reflects everything around a horizontal line // it's a "symmetrical" transform: doing it a second time undoes it. // we do it once to flip graphics into Adobe Illustrator's coordinate system, // and once more to restore Processing's coordinate system. // the new version of IgnoCodeLib (0.2) provides a single method, writeWithAITransform() // that creates and applies the Processing to Illustrator coordinate system transform. // "doc.writeWithAITransform(output)" could replace the next three lines, and you would // not need to generate aiTransform. // As of IgnoCodeLib 0.3, write(output) performs the transform for you. The writeWithAITransform // method is superfluous and will be deprecated in the future. Use writeNoTransform if you // don't want the default transform applied. /* // the old way...or what to do if you want your own transform applied (however, it modifies // the document geometry, so be prepared to run an inverse transform). doc.transform(aiTransform); doc.writeNoTransform(output); doc.transform(aiTransform); */ doc.write(output); }
Constructor Summary | |
---|---|
DocumentComponent()
PApplet used for calls to the Processing environment is obtained from IgnoCodeLib , which must be correctly initialized in setup. |
|
DocumentComponent(PApplet parent)
|
|
DocumentComponent(PApplet parent,
String title)
|
|
DocumentComponent(String title)
PApplet reference is obtained from initialized IgnoCodeLib . |
Method Summary | |
---|---|
void |
accept(ComponentVisitor visitor)
Accepts a ComponentVisitor that traverses a document structure tree. |
void |
accept(ComponentVisitor visitor,
boolean order)
Accepts a ComponentVisitor that traverses a document structure tree in preorder or postorder. |
void |
add(ArrayList<? extends DisplayComponent> comps)
Adds all components in a list to this document |
void |
add(DisplayComponent component)
Adds a component to children() of this component. |
void |
draw()
Draws geometry or text to the Processing window. |
void |
draw(PGraphics pg)
Draws a component to a supplied PGraphics. |
Matrix3 |
getAITransform()
Creates a transform to go from Processing coordinate system to Illustrator coordinate system. |
Rectangle |
getBbox()
Returns the bounding box of this document. |
String |
getCreator()
Returns the document creator, used in the document header. |
LayerComponent |
getDefaultLayer()
Returns the default layer. |
int |
getHeight()
Returns the height (in points) this document will have when exported. |
String |
getOrg()
Returns the org (organization), used in the document header. |
Palette |
getPalette()
Returns the palette associated with this document. |
String |
getTitle()
Returns the document title, used in the document header. |
int |
getWidth()
Returns the width (in points) of this document will have when exported. |
boolean |
isLocked()
|
boolean |
isTerminal()
|
boolean |
isVerbose()
Returns true if debugging information is requested. |
boolean |
isVisible()
|
void |
setBbox(Rectangle bbox)
Sets the bounding box of this document. |
void |
setCreator(String creator)
Sets the document creator, used in the document header. |
void |
setDefaultLayer(LayerComponent defaultLayer)
Sets the default layer to a user-specified layer. |
void |
setHeight(int height)
Sets the height of this document when it is exported. |
void |
setLocked(boolean isLocked)
|
void |
setOrg(String org)
Sets the org (organization), used in the document header. |
void |
setPalette(Palette palette)
Sets the palette associated with this document. |
void |
setTitle(String title)
Set the document title, used in the document header. |
void |
setVerbose(boolean verbose)
Set verbose to true to get some debugging information, default is false |
void |
setVisible(boolean isVisible)
|
void |
setWidth(int width)
Sets the width of this document when it is exported. |
void |
transform(Matrix3 matx)
Transforms geometry of shapes and location of text using the supplied matrix. |
void |
write(PrintWriter pw)
Writes an Adobe Illustrator 7.0 file format encoding structure, geometry and text. |
void |
writeDisplayList(ArrayList<DisplayComponent> comps,
PrintWriter pw)
Writes display list (middle) portion of an Adobe Illustrator file. |
void |
writeDisplayList(PrintWriter pw)
Writes all the children of this document. |
void |
writeDisplayListWithTransform(ArrayList<DisplayComponent> comps,
PrintWriter pw)
Writes display list (middle) portion of an Adobe Illustrator file. |
void |
writeHeader(PrintWriter pw)
Writes header portion of an Adobe Illustrator file, including palette. |
void |
writeNoTransform(PrintWriter pw)
|
void |
writeTrailer(PrintWriter pw)
Writes trailer portion of an Adobe Illustrator file, flushes and closes output. |
void |
writeWithAITransform(PrintWriter pw)
Writes the document hierarchy to the supplied file. |
Methods inherited from class net.paulhertz.aifile.DisplayComponent |
---|
children, get, hide, id, iterator, parentComponent, remove, setParentComponent, show |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DocumentComponent()
IgnoCodeLib
, which must be correctly initialized in setup.
If IgnoCodeLib does not have a reference to a PApplet, it throws a NullPointerException.
public DocumentComponent(PApplet parent)
parent
- PApplet used for calls to the Processing environment, notably for drawingpublic DocumentComponent(PApplet parent, String title)
parent
- PApplet used for calls to the Processing environment, notably for drawingtitle
- Title stored in the document headerpublic DocumentComponent(String title)
IgnoCodeLib
.
title
- Title stored in the document headerMethod Detail |
---|
public String getTitle()
public void setTitle(String title)
title
- the title to setpublic String getCreator()
public void setCreator(String creator)
creator
- the creator to setpublic String getOrg()
public void setOrg(String org)
org
- the org to setpublic int getWidth()
public void setWidth(int width)
width
- the width to setpublic int getHeight()
public void setHeight(int height)
height
- the height to setpublic Rectangle getBbox()
public void setBbox(Rectangle bbox)
bbox
- the bbox to setpublic Palette getPalette()
public void setPalette(Palette palette)
palette
- the palette to setpublic boolean isVerbose()
true
if debugging information is requested.
public void setVerbose(boolean verbose)
verbose
- the verbose to setpublic LayerComponent getDefaultLayer()
public void setDefaultLayer(LayerComponent defaultLayer)
defaultLayer
- the defaultLayer to setpublic void add(DisplayComponent component)
add
in class DisplayComponent
component
- DisplayComponent to add to this component's children
UnsupportedOperationException
public void add(ArrayList<? extends DisplayComponent> comps)
add
in class DisplayComponent
comps
- an ArrayList of DisplayComponentspublic void draw()
DisplayComponent
draw
in class DisplayComponent
public void draw(PGraphics pg)
DisplayComponent
draw
in class DisplayComponent
pg
- a PGraphics instancepublic boolean isLocked()
isLocked
in class DisplayComponent
public boolean isVisible()
isVisible
in class DisplayComponent
public void setLocked(boolean isLocked)
setLocked
in class DisplayComponent
isLocked
- the isLocked to setpublic void setVisible(boolean isVisible)
setVisible
in class DisplayComponent
isVisible
- the isVisible to setpublic boolean isTerminal()
isTerminal
in class DisplayComponent
public void write(PrintWriter pw)
DisplayComponent
write
in class DisplayComponent
pw
- a PrintWriter for file output.Writes the document hierarchy to the supplied file. Performs a transform
from Processing coordinate system to Illustrator coordinate system on all
geometry to make the file have the same appearance as the display.
public void writeWithAITransform(PrintWriter pw)
public void writeNoTransform(PrintWriter pw)
Writes the document hierarchy to the supplied file without a transform.
Image will be upside-down and reflected with respect to the display.
public void writeHeader(PrintWriter pw)
pw
- PrintWriter for outputpublic void writeDisplayList(PrintWriter pw)
write()
method.
Unlike the write()
method, this method does not perform a transform to make the
geometry in the file have the same orientation as the geometry on the computer display.
pw
- PrintWriter for outputpublic void writeDisplayList(ArrayList<DisplayComponent> comps, PrintWriter pw)
BezShape
and
other components and it will write them out for you. Does not transform the
geometry of the ArrayList.
comps
- an ArrayList<DisplayComponent>
pw
- PrintWriter for outputpublic void writeDisplayListWithTransform(ArrayList<DisplayComponent> comps, PrintWriter pw)
BezShape
and
other components and it will write them out for you. Uses the matrix generated by
the document's getAITransform()
method to transform the geometry in the ArrayList
before writing it to file, then transforms it back to its original position. This ensures
that the geometry in the file and the geometry on the display have the same orientation.
comps
- an ArrayList<DisplayComponent>
pw
- PrintWriter for outputpublic void writeTrailer(PrintWriter pw)
pw
- PrintWriter for outputpublic Matrix3 getAITransform()
write()
method handles the transform between coordinates systems for you.
public void transform(Matrix3 matx)
DisplayComponent
transform
in class DisplayComponent
matx
- a Matrix3 that encapsulates an affine geometric transform.public void accept(ComponentVisitor visitor)
Visitable
When called from an object that implements the Visitable
interface:
accept( visitor )
on all its children
The reference is passed back to the visitor through a method of the form
visitor.visit<ComponentClassName>( this );
See ComponentVisitor
The order of steps 1 and 2 determines if traversal of the composite structure is preorder or postorder. As shown above, it's preorder. Depending on what you want to do, one traversal may be better suited than the other. Preorder visits parents first, postorder visits children first.
visitor
- a ComponentVisitor
public void accept(ComponentVisitor visitor, boolean order)
Visitable
visitor
- a ComponentVisitor
order
- boolean to determine if traversal is preorder or postorder
accept( visitor )
should implement the default order of traversal,
accept( visitor, <false>
) should implement the other order.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |