Java Code Examples for javafx.scene.canvas.GraphicsContext#scale()

The following examples show how to use javafx.scene.canvas.GraphicsContext#scale() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAxis.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
protected static void drawTickMarkLabel(final GraphicsContext gc, final double x, final double y,
        final double scaleFont, final TickMark tickMark) {
    gc.save();

    gc.setFont(tickMark.getFont());
    gc.setFill(tickMark.getFill());
    gc.setTextAlign(tickMark.getTextAlignment());
    gc.setTextBaseline(tickMark.getTextOrigin());

    gc.translate(x, y);
    if (tickMark.getRotate() != 0.0) {
        gc.rotate(tickMark.getRotate());
    }
    gc.setGlobalAlpha(tickMark.getOpacity());
    if (scaleFont != 1.0) {
        gc.scale(scaleFont, 1.0);
    }

    gc.fillText(tickMark.getText(), 0, 0);
    // gc.fillText(tickMark.getText(), x, y);C
    gc.restore();
}
 
Example 2
Source File: StateTransitionEdgeViewer.java    From JetUML with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Canvas createIcon(Edge pEdge)
{   //CSOFF: Magic numbers
	Canvas canvas = new Canvas(BUTTON_SIZE, BUTTON_SIZE);
	GraphicsContext graphics = canvas.getGraphicsContext2D();
	graphics.scale(0.6, 0.6);
	Line line = new Line(new Point(2,2), new Point(40,40));
	final double tangent = Math.tan(Math.toRadians(DEGREES_10));
	double dx = (line.getX2() - line.getX1()) / 2;
	double dy = (line.getY2() - line.getY1()) / 2;
	Point control = new Point((int)((line.getX1() + line.getX2()) / 2 + tangent * dy), 
			(int)((line.getY1() + line.getY2()) / 2 - tangent * dx));         
	
	Path path = new Path();
	MoveTo moveTo = new MoveTo(line.getPoint1().getX(), line.getPoint1().getY());
	QuadCurveTo curveTo = new QuadCurveTo(control.getX(), control.getY(), line.getPoint2().getX(), line.getPoint2().getY());
	path.getElements().addAll(moveTo, curveTo);
	
	ToolGraphics.strokeSharpPath(graphics, path, LineStyle.SOLID);
	ArrowHead.V.view().draw(graphics, control, new Point(40, 40));
	return canvas;
}
 
Example 3
Source File: FieldNodeViewer.java    From JetUML with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Canvas createIcon(Node pNode)
{
	Dimension dimension = EQUALS_VIEWER.getDimension(ICON_LABEL);
	int width = dimension.getWidth();
	int height = dimension.getHeight();
	double scaleX = (BUTTON_SIZE - OFFSET)/ (double) width;
	double scaleY = (BUTTON_SIZE - OFFSET)/ (double) height;
	double scale = Math.min(scaleX, scaleY);
	Canvas canvas = new Canvas(BUTTON_SIZE, BUTTON_SIZE);
	GraphicsContext graphics = canvas.getGraphicsContext2D();
	graphics.scale(scale, scale);
	graphics.translate(Math.max((height - width) / 2, 0), Math.max((width - height) / 2, 0));
	graphics.setFill(Color.WHITE);
	graphics.setStroke(Color.BLACK);
	EQUALS_VIEWER.draw(ICON_LABEL, graphics, 
			new Rectangle(0, BUTTON_SIZE/2 - height/2+OFFSET, width, height));
	return canvas;
}
 
Example 4
Source File: AbstractNodeViewer.java    From JetUML with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Canvas createIcon(Node pNode)
{
	Rectangle bounds = getBounds(pNode);
	int width = bounds.getWidth();
	int height = bounds.getHeight();
	double scaleX = (BUTTON_SIZE - OFFSET)/ (double) width;
	double scaleY = (BUTTON_SIZE - OFFSET)/ (double) height;
	double scale = Math.min(scaleX, scaleY);
	Canvas canvas = new Canvas(BUTTON_SIZE, BUTTON_SIZE);
	GraphicsContext graphics = canvas.getGraphicsContext2D();
	graphics.scale(scale, scale);
	graphics.translate(Math.max((height - width) / 2, 0), Math.max((width - height) / 2, 0));
	graphics.setFill(Color.WHITE);
	graphics.setStroke(Color.BLACK);
	draw(pNode, canvas.getGraphicsContext2D());
	return canvas;
}
 
Example 5
Source File: DebugDrawJavaFX.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void drawCircle(Vec2 center, float radius, Vec2 axis, Color3f color) {
  GraphicsContext g = getGraphics();
  Color s = cpool.getColor(color.x, color.y, color.z, 1f);
  saveState(g);
  double scaling = transformGraphics(g, center) * radius;
  g.setLineWidth(stroke / scaling);
  g.scale(radius, radius);
  g.setStroke(s);
  g.strokeOval(circle.getMinX(), circle.getMinX(), circle.getWidth(), circle.getHeight());
  if (axis != null) {
    g.rotate(MathUtils.atan2(axis.y, axis.x));
    g.strokeLine(0, 0, 1, 0);
  }
  restoreState(g);
}
 
Example 6
Source File: DebugDrawJavaFX.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void drawSolidCircle(Vec2 center, float radius, Vec2 axis, Color3f color) {
  GraphicsContext g = getGraphics();
  Color f = cpool.getColor(color.x, color.y, color.z, .4f);
  Color s = cpool.getColor(color.x, color.y, color.z, 1f);
  saveState(g);
  double scaling = transformGraphics(g, center) * radius;
  g.setLineWidth(stroke / scaling);
  g.scale(radius, radius);
  g.setFill(f);
  g.fillOval(circle.getMinX(), circle.getMinX(), circle.getWidth(), circle.getHeight());
  g.setStroke(s);
  g.strokeOval(circle.getMinX(), circle.getMinX(), circle.getWidth(), circle.getHeight());
  if (axis != null) {
    g.rotate(MathUtils.atan2(axis.y, axis.x));
    g.strokeLine(0, 0, 1, 0);
  }
  restoreState(g);
}
 
Example 7
Source File: DebugDrawJavaFX.java    From jbox2d with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void drawParticles(Vec2[] centers, float radius, ParticleColor[] colors, int count) {
  GraphicsContext g = getGraphics();
  saveState(g);
  double scaling = transformGraphics(g, zero) * radius;
  g.setLineWidth(stroke / scaling);
  for (int i = 0; i < count; i++) {
    Vec2 center = centers[i];
    Color color;
    if (colors == null) {
      color = pcolorA;
    } else {
      ParticleColor c = colors[i];
      color = cpool.getColor(c.r * 1f / 127, c.g * 1f / 127, c.b * 1f / 127, c.a * 1f / 127);
    }
    Affine old = g.getTransform();
    g.translate(center.x, center.y);
    g.scale(radius, radius);
    g.setFill(color);
    g.fillOval(circle.getMinX(), circle.getMinX(), circle.getWidth(), circle.getHeight());
    g.setTransform(old);
  }
  restoreState(g);
}
 
Example 8
Source File: ContourDataSetRenderer.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
private void drawHexagonHeatMap(final GraphicsContext gc, final ContourDataSetCache lCache) {
    final long start = ProcessingProfiler.getTimeStamp();

    // process z quantisation to colour transform
    final WritableImage image = localCache.convertDataArrayToImage(lCache.reduced, lCache.xSize, lCache.ySize, getColorGradient());

    final int tileSize = Math.max(getMinHexTileSizeProperty(), (int) lCache.xAxisWidth / lCache.xSize);
    final int nWidthInTiles = (int) (lCache.xAxisWidth / (tileSize * Math.sqrt(3))) + 1;

    final HexagonMap map2 = new HexagonMap(tileSize, image, nWidthInTiles, (q, r, imagePixelColor, map) -> {
        final Hexagon h = new Hexagon(q, r);
        h.setFill(imagePixelColor);
        h.setStroke(imagePixelColor);

        h.setStrokeWidth(0.5);
        map.addHexagon(h);
    });
    localCache.add(image);

    ProcessingProfiler.getTimeDiff(start, "drawHexagonMap - prepare");
    final double scaleX = lCache.xDataPixelRange / lCache.xAxisWidth;
    final double scaleY = lCache.yDataPixelRange / lCache.yAxisHeight;

    gc.save();
    gc.translate(lCache.xDataPixelMin, lCache.yDataPixelMin);
    gc.scale(scaleX, scaleY);

    map2.render(gc.getCanvas());
    gc.restore();

    // gc.drawImage(image, lCache.xDataPixelMin, lCache.yDataPixelMin, lCache.xDataPixelRange,
    // lCache.yDataPixelRange);

    ProcessingProfiler.getTimeDiff(start, "drawHexagonMap");
}
 
Example 9
Source File: ContourDataSetRenderer.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
private void drawHexagonMapContour(final GraphicsContext gc, final ContourDataSetCache lCache) {
    final long start = ProcessingProfiler.getTimeStamp();

    // process z quantisation to colour transform
    final WritableImage image = localCache.convertDataArrayToImage(lCache.reduced, lCache.xSize, lCache.ySize, getColorGradient());

    final int tileSize = Math.max(getMinHexTileSizeProperty(), (int) lCache.xAxisWidth / lCache.xSize);
    final int nWidthInTiles = (int) (lCache.xAxisWidth / (tileSize * Math.sqrt(3)));
    final HexagonMap hexMap = new HexagonMap(tileSize, image, nWidthInTiles, (q, r, imagePixelColor, map) -> {
        final Hexagon h = new Hexagon(q, r);
        h.setFill(Color.TRANSPARENT); // contour being plotted
        h.setStroke(imagePixelColor);
        h.setStrokeType(StrokeType.CENTERED);

        h.setStrokeWidth(1);
        map.addHexagon(h);
    });
    localCache.add(image);

    ProcessingProfiler.getTimeDiff(start, "drawHexagonMapContour - prepare");

    final double scaleX = lCache.xDataPixelRange / lCache.xAxisWidth;
    final double scaleY = lCache.yDataPixelRange / lCache.yAxisHeight;
    gc.save();
    gc.translate(lCache.xDataPixelMin, lCache.yDataPixelMin);
    gc.scale(scaleX, scaleY);

    hexMap.renderContour(gc.getCanvas());
    gc.restore();

    ProcessingProfiler.getTimeDiff(start, "drawHexagonMapContour");
}
 
Example 10
Source File: TintedImageHelperRenderer.java    From arma-dialog-creator with MIT License 5 votes vote down vote up
/** This method applies to {@link #setToPreviewMode(boolean)} */
public void paintTintedImage(@NotNull GraphicsContext gc) {
	gc.save();
	if (rotated) {
		gc.translate(x + w / 2, y + h / 2); //move to center
		gc.rotate(rotateDeg);
	}
	if (flipped) {
		gc.scale(-1, 1);
	}
	gc.setFill(Color.TRANSPARENT);
	gc.setEffect(previewMode ? effect2 : effect1);
	gc.fillRect(0, 0, 1, 1); //this is just to trigger drawing the effect. Won't draw anything itself
	gc.restore();
}
 
Example 11
Source File: ObjectReferenceEdgeViewer.java    From JetUML with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Canvas createIcon(Edge pEdge)
{   //CSOFF: Magic numbers
	Canvas canvas = new Canvas(BUTTON_SIZE, BUTTON_SIZE);
	GraphicsContext graphics = canvas.getGraphicsContext2D();
	graphics.scale(0.6, 0.6);
	Path path = getCShape(new Line(new Point(5, 5), new Point(15,25)));
	ToolGraphics.strokeSharpPath(graphics, path, LineStyle.SOLID);
	ArrowHead.BLACK_TRIANGLE.view().draw(graphics, new Point(20,25), new Point(15, 25));
	return canvas;
}
 
Example 12
Source File: DebugDrawJavaFX.java    From jbox2d with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void drawCircle(Vec2 center, float radius, Color3f color) {
  GraphicsContext g = getGraphics();
  Color s = cpool.getColor(color.x, color.y, color.z, 1.0f);
  saveState(g);
  double scaling = transformGraphics(g, center) * radius;
  g.setLineWidth(stroke / scaling);
  g.scale(radius, radius);
  g.setStroke(s);
  g.strokeOval(circle.getMinX(), circle.getMinX(), circle.getWidth(), circle.getHeight());
  restoreState(g);
}
 
Example 13
Source File: ContourDataSetRenderer.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
private void drawContour(final GraphicsContext gc, final ContourDataSetCache lCache) {
    final double[] levels = new double[getNumberQuantisationLevels()];
    for (int i = 0; i < levels.length; i++) {
        levels[i] = (i + 1) / (double) levels.length;
    }

    final int xSize = lCache.xSize;
    final int ySize = lCache.ySize;
    final double[][] data = new double[ySize][xSize];
    for (int yIndex = 0; yIndex < ySize; yIndex++) {
        for (int xIndex = 0; xIndex < xSize; xIndex++) {
            final double offset = lCache.reduced[yIndex * xSize + xIndex];
            data[ySize - 1 - yIndex][xIndex] = offset;
        }
    }

    // abort if min/max == 0 -> cannot compute contours
    final double zRange = Math.abs(lCache.zMax - lCache.zMin);
    if (zRange <= 0) {
        return;
    }

    final ColorGradient colorGradient = getColorGradient();
    final MarchingSquares marchingSquares = new MarchingSquares();
    final double scaleX = lCache.xDataPixelRange / xSize;
    final double scaleY = lCache.yDataPixelRange / ySize;
    gc.save();
    gc.translate(lCache.xDataPixelMin, lCache.yDataPixelMin);
    gc.scale(scaleX, scaleY);
    final GeneralPath[] isolines;
    try {
        isolines = marchingSquares.buildContours(data, levels);
        int levelCount = 0;
        for (final GeneralPath path : isolines) {
            if (path.size() > getMaxContourSegments()) {
                levelCount++;
                continue;
            }
            final Color color = lCache.zInverted ? colorGradient.getColor(1 - levels[levelCount++])
                                                 : colorGradient.getColor(levels[levelCount++]);
            gc.setStroke(color);
            gc.setLineDashes(1.0);
            gc.setMiterLimit(10);
            gc.setFill(color);
            gc.setLineWidth(0.5);
            path.draw(gc);
        }
    } catch (InterruptedException | ExecutionException e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.atError().setCause(e).log("marchingSquares algorithm");
        }
    } finally {
        gc.restore();
    }
}
 
Example 14
Source File: MiscHelpers.java    From arma-dialog-creator with MIT License 4 votes vote down vote up
public static void paintFlippedImage(@NotNull GraphicsContext gc, @NotNull Image img, int x, int y, int w, int h) {
	gc.save();
	gc.scale(-1, 1);
	gc.drawImage(img, -x - w, y, w, h);
	gc.restore();
}