public class ClosureGlyph extends Glyphish
Glyphish
.
Each of the five abstract methods of the abstract Glyphish can be specified by supplying
a JLambda closure:
public boolean inside(Point2D p); public boolean intersects(Rectangle2D rect); public void paint(Graphics2D g2d); public void transform(AffineTransform a); public Rectangle2D getBounds();By a rigidly adhered to convention, the closure should take one more argument than the corresponding abstract method. The first argument, which will be the
this
object of the ClosureGlyph instance to which the closure belongs. The following is a simple
example of the use of such dynamically created glyphs, it creates a frame with
two draggable nodes (a blue one and a green one), connected together by a line.
The line is the ClosureGlyph.
(let ((h (int 50)) (w (int 70)) (black java.awt.Color.black) (blue java.awt.Color.blue) (green java.awt.Color.green) (stroke (object ("java.awt.BasicStroke" (float 2.5)))) (ellipse (object ("java.awt.geom.Ellipse2D$Double" (int 0) (int 0) w h))) (view (object ("g2d.swing.IOPView"))) (frame (object ("g2d.swing.IOPFrame" "Node Example" view))) (mkPoint (lambda (xPos yPos) (object ("java.awt.geom.Point2D$Double" xPos yPos)))) (mkLine (lambda (pointA pointB) (let ((lineGlyph (object ("g2d.closure.ClosureGlyph"))) (lineDraw (lambda (self g2d) (let ((line (object ("java.awt.geom.Line2D$Double" pointA pointB)))) (invoke g2d "draw" line))))) (seq (invoke lineGlyph "setPaintClosure" lineDraw) (invoke view "add" lineGlyph) lineGlyph)))) (mkNode (lambda (xPos yPos fillC) (let ((nodeGlyph (let ((temp (object ("g2d.glyph.Glyph" ellipse black fillC)))) (seq (invoke temp "setStroke" stroke) temp))) (pressed (lambda (self event) (seq (setAttr self "pointF" (apply mkPoint (invoke event "getX") (invoke event "getY"))) (setAttr self "draggedF" (boolean true))))) (released (lambda (self event) (setAttr self "draggedF" (boolean false)))) (dragged (lambda (self event) (if (getAttr self "draggedF") (let ((pnt (getAttr self "pointF")) (cnt (getAttr self "centerF")) (eX (invoke event "getX")) (eY (invoke event "getY")) (a (let ((temp (object ("java.awt.geom.AffineTransform")))) (seq (invoke temp "translate" (- eX (invoke pnt "getX")) (- eY (invoke pnt "getY"))) temp)))) (seq (invoke self "transform" a) (invoke pnt "setLocation" (invoke event "getX") (invoke event "getY")) (invoke a "transform" cnt cnt) (invoke view "repaint")))))) (trans (let ((temp (object ("java.awt.geom.AffineTransform")))) (seq (invoke temp "translate" xPos yPos) temp))) ) (seq (setAttr nodeGlyph "centerF" (apply mkPoint (+ xPos (/ w (int 2))) (+ yPos (/ h (int 2))))) (invoke nodeGlyph "setMouseAction" java.awt.event.MouseEvent.MOUSE_PRESSED pressed) (invoke nodeGlyph "setMouseAction" java.awt.event.MouseEvent.MOUSE_RELEASED released) (invoke nodeGlyph "setMouseAction" java.awt.event.MouseEvent.MOUSE_DRAGGED dragged) (invoke nodeGlyph "transform" trans) nodeGlyph)))) (nodeA (apply mkNode (* w (int 4)) (* h (int 4)) blue)) (nodeB (apply mkNode (* w (int 1)) (* h (int 1)) green)) (lineAB (apply mkLine (getAttr nodeA "centerF") (getAttr nodeB "centerF"))) ) (seq (invoke view "add" nodeA) (invoke view "add" nodeB) (invoke frame "setVisible" (boolean true)) (invoke view "repaint") ) )
Constructor and Description |
---|
ClosureGlyph() |
Modifier and Type | Method and Description |
---|---|
java.awt.geom.Rectangle2D |
getBounds() |
boolean |
inside(java.awt.geom.Point2D p) |
boolean |
intersects(java.awt.geom.Rectangle2D rect) |
void |
paint(java.awt.Graphics2D g2d) |
void |
setBoundsClosure(Closure fun)
Specifies the egtBounds closure value.
|
void |
setInsideClosure(Closure fun)
Specifies the inside closure value.
|
void |
setIntersectsClosure(Closure fun)
Specifies the intersects closure value.
|
void |
setPaintClosure(Closure fun)
Specifies the paint closure value.
|
void |
setTransformClosure(Closure fun)
Specifies the transform closure value.
|
void |
transform(java.awt.geom.AffineTransform a) |
keyPressed, keyReleased, keyTyped, mouseClicked, mouseDragged, mouseEntered, mouseExited, mouseMoved, mousePressed, mouseReleased, rotate, rotate, scale, setKeyAction, setMouseAction, shear, translate, unsetMouseAction
_getAttribute, _setAttribute, getAttribute, getAttributeAsBoolean, getAttributeAsByte, getAttributeAsChar, getAttributeAsClosure, getAttributeAsDouble, getAttributeAsFloat, getAttributeAsInt, getAttributeAsShort, getAttributeAsString, getKeys, getStaticAttribute, getStaticKeys, isAttribute, isStaticAttribute, removeAllAttributes, removeAllStaticAttributes, removeAttribute, removeStaticAttribute, setAttribute, setStaticAttribute, showAttributes, showAttributes
public boolean intersects(java.awt.geom.Rectangle2D rect)
intersects
in class Glyphish
public void transform(java.awt.geom.AffineTransform a)
public void setPaintClosure(Closure fun)
fun
- the closure.public void setInsideClosure(Closure fun)
fun
- the closure.public void setIntersectsClosure(Closure fun)
fun
- the closure.public void setTransformClosure(Closure fun)
fun
- the closure.public void setBoundsClosure(Closure fun)
fun
- the closure.