Skip to main content

Cloning of JavaFX objects?

1 reply [Last post]
Joined: 2008-02-28


I wonder if there's some JavaFX equivalent to clone() function in Java?

I have some quite complex shapes, which I need to add repeatedly to the canvas.
The rendering of the shape is a bit "heavy", so I'd prefer to generate it only once, and then just make new instances of the result to place them on the canvas.

Is there a way how to accomplish this in interpreted JavaFX? I was searching through all the documents on JavaFX scripting language I could found, but without a luck.

Thanks for any help...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-02-28

OK, I have it. I had to create a new descendant of Shape, able to clone itself. It is not completely universal solution (AWT shape is required), but it's ok for my needs.

In case anyone encounters the same situation, here is the source code:

import javafx.ui.canvas.*;
import java.awt.Shape as AWTShape;

class CloneableShape extends Shape {
attribute shape: AWTShape;
operation clone():CloneableShape;

operation CloneableShape.createShape() {
zshape = new ZShape();
return zshape;

operation CloneableShape.clone() {
return CloneableShape {
shape: this.shape
canAcceptDrop: this.canAcceptDrop
clip: this.clip
composite: this.composite
currentX: this.currentX
currentY: this.currentY
currentWidth: this.currentWidth
currentHeight: this.currentHeight
cursor: this.cursor
dragCount: this.dragCount
exportAsDrag: this.exportAsDrag
exportDrag: this.exportDrag
fill: this.fill
fillRule: this.fillRule
filter: this.filter
focusable: this.focusable
focused: this.focused
halign: this.halign
hover: this.hover
isSelectionRoot: this.isSelectionRoot
onDragEnter: this.onDragEnter
onDragExit: this.onDragExit
onDrop: this.onDrop
onKeyDown: this.onKeyDown
onKeyTyped: this.onKeyTyped
onKeyUp: this.onKeyUp
onMouseClicked: this.onMouseClicked
onMouseDragged: this.onMouseDragged
onMouseEntered: this.onMouseEntered
onMouseExited: this.onMouseExited
onMouseMoved: this.onMouseMoved
onMousePressed: this.onMousePressed
onMouseReleased: this.onMouseReleased
outline: this.outline
stroke: this.stroke
strokeComposite: this.strokeComposite
strokeDashArray: this.strokeDashArray
strokeDashOffset: this.strokeDashOffset
strokeLineCap: this.strokeLineCap
strokeLineJoin: this.strokeLineJoin
strokeMiterLimit: this.strokeMiterLimit
strokeWidth: this.strokeWidth
scaleToFitCanvas: this.scaleToFitCanvas
selectable: this.selectable
toolTipText: this.toolTipText
transform: this.transform
valign: this.valign
visible: this.visible