グラフィック・オブジェクトのサブクラス化
独自のグラフィック・オブジェクトが、青いベジエ曲線を左下点から右上点へ、矩形内に描画する矩形のサブクラスであるとします。このコードを次に示します。
package mypackage; import java.awt.*; import ilog.views.*; import ilog.views.graphic.*; public class MyGraphic extends IlvRectangle { private IlvPoint[] pts = new IlvPoint[4]; private float[] dash = {4, 2}; public MyGraphic(IlvRect rect) { super(rect); for (int i = 0; i < 4; i++) pts[i] = new IlvPoint(); computeBezier(); } private computeBezier() { pts[0].x = drawrect.x; pts[0].y = drawrect.y + drawrect.height; pts[1].x = drawrect.x + drawrect.width / 4; pts[1].y = drawrect.y + drawrect.height / 4; pts[2].x = drawrect.x + 3*drawrect.width / 4; pts[2].y = drawrect.y + 3*drawrect.height / 4; pts[3].x = drawrect.x + drawrect.width; pts[3].y = drawrect.y; } public void draw(Graphics g, IlvTransformer t) { super.draw(g, t); // will draw the rectangle // Will draw a Bezier in blue g.setColor(Color.blue); IlvGraphicUtil.DrawBezier(g, pts, 4, 1, IlvStroke.JOIN_MITER, IlvStroke.CAP_ROUND, dash, t); } public void applyTransform(IlvTransform t) { super.applyTransform(t); computeBezier(); } // ... }
トランスレーターの作成とビルダー・コンフィギュレーターの設定
オブジェクトが汎用変換メカニズムを使用しないようにする場合、 SVGDocumentBuilderConfigurator.GraphicTranslator インターフェースを実装し、
MyGraphic
インスタンスを SVG Element
インスタンスに変換できるようにする必要があります。クラスの
translate
メソッドが新規 Element
インスタンスを作成し、そのメソッドをグラフィック・オブジェクトにある情報で埋めてスタイルを適用すると、メソッドはそのインスタンスを返します。Element
を埋める場合は、通常の DOM メソッド (Element.setAttribute()
)、または SVG DOM が提供したメソッドが使用できます。ただし、Rogue Wave JViews には限定された SVG DOM のサポートのみが実装されているため、すべてのメソッドがアクセス可能であるわけではありません。以下のコードは、トランスレーターの作成方法を示しています。
import org.w3c.dom.*; import org.w3c.svg.dom.*; public class MyGraphicTranslator implements SVGDocumentBuilderConfigurator.GraphicTranslator { // Method that translates the graphic to SVG public Element translate(IlvGraphic graphic, IlvTransformer t, SVGDocumentBuilder builder) { SVGDocument doc = builder.getDocument(); // The SVG ’group’ element will contain // all drawings necessary to display MyGraphic. SVGGElement group = (SVGGElement)doc.createElementNS ("http://www.w3.org/2000/svg", "g"); // First add to the group the element corresponding // to the parent class of MyRect (IlvRectangle). group.appendChild(builder.getConfigurator(). getTranslator(“ilog.views.graphic.IlvRectangle”). translate(graphic, t)); // Create the SVG element corresponding to // the drawing added at MyGraphic level. SVGPathElement path = (SVGPathElement)doc.createElementNS ("http://www.w3.org/2000/svg", "path"); IlvRect rect = graphic.boundingBox(graphic, t); SVGList list = path.getPathSegList(); // Go to the beginning point of the Bezier. list.appendItem(path.createPathSegMovetoAbs(rect.x, rect.y + rect.height)); // Add the Bezier. list.appendItem(path. createPathSegCurvetoCubicAbs(rect.x + rect.width, rect.y, rect.x + rect.width / 4, rect.y + rect.height / 4, rect.x + 3*rect.width / 4, rect.y + 3*rect.height / 4));
// Start to style the path. builder.startStyleElement(path, null); // Really apply the style (blue color...). builder.appendStyle("stroke", "blue"); builder.appendStyle("stroke-width", "1"); builder.appendStyle("stroke-join", "miter"); builder.appendStyle("stroke-cap", "round"); builder.appendStyle("stroke-dasharray", "4 2"); // Finish to style the path. builder.endStylingElement(); group.appendChild(path); return group; } }
SVG ストリーム・ファクトリーのカスタマイズ
新規トランスレーターを参照する適切に構成されたビルダー・コンフィギュレーターを作成する必要があります。SVGOutputStream を使用している場合は、以下のコードを作成できます。
SVGStreamFactory factory = new SVGStreamFactory(); factory.getBuilderConfigurator().putTranslator("mypackage.MyGraphic", new MyGraphicTranslator());
この新規のストリーム・ファクトリーは、通常のマネージャーではなく自分のマネージャーに設定するようにしてください。
SVG シン・クライアント・コンテキスト内にある場合、サーブレット用に使用した
SVGBuilderConfigurator
インスタンス上で putTranslator メソッドを呼び出すだけです。