private CustomGraphicLayer syncSize( CyCustomGraphics<CustomGraphicLayer> graphics, final CustomGraphicLayer cg, double width, double height) { // final double nodeW = this.getWidth(); // final double nodeH = this.getHeight(); final Rectangle2D originalBounds = cg.getBounds2D(); // If this is just a paint, getBounds2D will return null and // we can use our own width and height if (originalBounds == null) return cg; if (width == 0.0 || height == 0.0) return cg; final double cgW = originalBounds.getWidth(); final double cgH = originalBounds.getHeight(); // In case size is same, return the original. if (width == cgW && height == cgH) return cg; final AffineTransform scale; final float fit = graphics.getFitRatio(); // Case 1: if custom graphic is a vector fit width and length if (cg instanceof PaintedShape || cg instanceof Cy2DGraphicLayer) { scale = AffineTransform.getScaleInstance(fit * width / cgW, fit * height / cgH); } else { double scaleW = width / cgW; double scaleH = height / cgH; // Case 2: node height value is larger than width if (scaleW >= scaleH) { scale = AffineTransform.getScaleInstance(fit * scaleH, fit * scaleH); // scale = AffineTransform.getScaleInstance(fit * (width / cgW) * (height / width), fit * // height / cgH); } else { scale = AffineTransform.getScaleInstance(fit * scaleW, fit * scaleW); // scale = AffineTransform.getScaleInstance(fit * (width / cgW) * (height / width), fit * // height / cgH); } } return cg.transform(scale); }