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

The following examples show how to use javafx.scene.canvas.GraphicsContext#strokeRect() . 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: FXPaintUtils.java    From gef with Eclipse Public License 2.0 7 votes vote down vote up
/**
 * Creates a rectangular {@link Image} to visualize the given {@link Paint}.
 *
 * @param width
 *            The width of the resulting {@link Image}.
 * @param height
 *            The height of the resulting {@link Image}.
 * @param paint
 *            The {@link Paint} to use for filling the {@link Image}.
 * @return The resulting (filled) {@link Image}.
 */
public static ImageData getPaintImageData(int width, int height, Paint paint) {
	// use JavaFX canvas to render a rectangle with the given paint
	Canvas canvas = new Canvas(width, height);
	GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
	graphicsContext.setFill(paint);
	graphicsContext.fillRect(0, 0, width, height);
	graphicsContext.setStroke(Color.BLACK);
	graphicsContext.strokeRect(0, 0, width, height);
	// handle transparent color separately (we want to differentiate it from
	// transparent fill)
	if (paint instanceof Color && ((Color) paint).getOpacity() == 0) {
		// draw a red line from bottom-left to top-right to indicate a
		// transparent fill color
		graphicsContext.setStroke(Color.RED);
		graphicsContext.strokeLine(0, height - 1, width, 1);
	}
	WritableImage snapshot = canvas.snapshot(new SnapshotParameters(), null);
	return SWTFXUtils.fromFXImage(snapshot, null);
}
 
Example 2
Source File: WriteFxImageBenchmark.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public static void initalizeImage() {
    noisePixels = ByteBuffer.allocate(w * h * 4);
    noiseBuffer = new PixelBuffer<>(w, h, noisePixels, PixelFormat.getByteBgraPreInstance());
    testimage = new WritableImage(noiseBuffer);
    final Canvas noiseCanvas = new Canvas(w, h);
    final GraphicsContext noiseContext = noiseCanvas.getGraphicsContext2D();
    final byte[] randomArray = new byte[w * h * 4];
    new Random().nextBytes(randomArray);
    noiseContext.getPixelWriter().setPixels(0, 0, w, h, PixelFormat.getByteBgraInstance(), randomArray, 0, w);
    noiseCanvas.snapshot(null, testimage);

    final Canvas easyCanvas = new Canvas(w2, h2);
    final GraphicsContext easyContext = easyCanvas.getGraphicsContext2D();
    easyContext.setStroke(Color.BLUE);
    easyContext.strokeOval(20, 30, 40, 50);
    easyContext.setStroke(Color.RED);
    easyContext.strokeOval(30, 40, 50, 60);
    easyContext.setStroke(Color.GREEN);
    easyContext.strokeOval(40, 50, 60, 70);
    easyContext.setStroke(Color.ORANGE);
    easyContext.strokeRect(0, 0, w2, h2);
    testimage2 = easyCanvas.snapshot(null, null);

    initialized.set(true);
}
 
Example 3
Source File: DemoHelper.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
public static void refresh(final Canvas canvas)
{
    final GraphicsContext gc = canvas.getGraphicsContext2D();

    final Bounds bounds = canvas.getBoundsInLocal();
    final double width = bounds.getWidth();
    final double height = bounds.getHeight();

    gc.clearRect(0, 0, width, height);
    gc.strokeRect(0, 0, width, height);

    for (int i=0; i<ITEMS; ++i)
    {
        gc.setFill(Color.hsb(Math.random()*360.0,
                             Math.random(),
                             Math.random()));
        final double size = 5 + Math.random() * 40;
        final double x = Math.random() * (width-size);
        final double y = Math.random() * (height-size);
        gc.fillOval(x, y, size, size);
    }
}
 
Example 4
Source File: WidgetColorPropertyField.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
/** @param color Color to display */
public void setColor(final WidgetColor color)
{
    final GraphicsContext gc = blob.getGraphicsContext2D();

    gc.setFill(JFXUtil.convert(color));
    gc.fillRect(0, 0, 16, 16);

    gc.setLineCap(StrokeLineCap.SQUARE);
    gc.setLineJoin(StrokeLineJoin.MITER);
    gc.setLineWidth(1.75);
    gc.setStroke(Color.BLACK);
    gc.strokeRect(0, 0, 16, 16);

    button.setText(String.valueOf(color));
}
 
Example 5
Source File: WriteFxImageTests.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
@Start
public void setUp(@SuppressWarnings("unused") final Stage stage) {
    int w = 200;
    int h = 300;
    // generate test image with simple shapes
    final Canvas originalCanvas = new Canvas(w, h);
    final GraphicsContext originalContext = originalCanvas.getGraphicsContext2D();
    originalContext.setStroke(Color.BLUE);
    originalContext.strokeOval(20, 30, 40, 50);
    originalContext.setStroke(Color.RED);
    originalContext.strokeOval(30, 40, 50, 60);
    originalContext.setStroke(Color.GREEN);
    originalContext.strokeOval(40, 50, 60, 70);
    originalContext.setStroke(Color.ORANGE);
    originalContext.strokeRect(0, 0, w, h);
    imageOvals = originalCanvas.snapshot(null, null);

    //generate test image with noise data (not very compressible)
    final Canvas noiseCanvas = new Canvas(600, 333);
    final GraphicsContext noiseContext = noiseCanvas.getGraphicsContext2D();
    byte[] randomArray = new byte[600 * 333 * 4];
    new Random().nextBytes(randomArray);
    noiseContext.getPixelWriter().setPixels(0, 0, 600, 333, PixelFormat.getByteBgraInstance(), randomArray, 0, 600);
    imageRandom = noiseCanvas.snapshot(null, null);

    // generate test image with minimal dimensions
    final Canvas onexoneCanvas = new Canvas(1, 1);
    final GraphicsContext onexoneContext = onexoneCanvas.getGraphicsContext2D();
    onexoneContext.getPixelWriter().setArgb(0, 0, 0xdeadbeef);
    image1x1 = onexoneCanvas.snapshot(null, null);
}
 
Example 6
Source File: Button.java    From CircuitSim with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void paint(GraphicsContext graphics, CircuitState state) {
	GuiUtils.drawName(graphics, this, getProperties().getValue(Properties.LABEL_LOCATION));

	int x = getScreenX();
	int y = getScreenY();
	int width = getScreenWidth();
	int height = getScreenHeight();
	int offset = 3;
	
	graphics.setStroke(Color.BLACK);
	
	if(isPressed) {
		graphics.setFill(Color.WHITE);
	} else {
		graphics.setFill(Color.DARKGRAY);
	}
	
	graphics.fillRect(x + offset, y + offset, width - offset, height - offset);
	graphics.strokeRect(x + offset, y + offset, width - offset, height - offset);
	
	if(!isPressed) {
		graphics.setFill(Color.WHITE);
		
		graphics.fillRect(x, y, width - offset, height - offset);
		graphics.strokeRect(x, y, width - offset, height - offset);
		
		graphics.strokeLine(x, y + height - offset, x + offset, y + height);
		graphics.strokeLine(x + width - offset, y, x + width, y + offset);
		graphics.strokeLine(x + width - offset, y + height - offset, x + width, y + height);
	}
}
 
Example 7
Source File: ToolGraphics.java    From JetUML with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws a handle on pGraphics that is centered at the position
 * (pX, pY).
 * 
 * @param pGraphics The graphics context on which to draw the handle.
 * @param pX The x-coordinate of the center of the handle.
 * @param pY The y-coordinate of the center of the handle.
 */
private static void drawHandle(GraphicsContext pGraphics, int pX, int pY)
{
	Paint oldStroke = pGraphics.getStroke();
	Paint oldFill = pGraphics.getFill();
	pGraphics.setStroke(SELECTION_COLOR);
	pGraphics.strokeRect((int)(pX - HANDLE_SIZE / 2.0) + 0.5, (int)(pY - HANDLE_SIZE / 2.0)+ 0.5, HANDLE_SIZE, HANDLE_SIZE);
	pGraphics.setFill(SELECTION_FILL_COLOR);
	pGraphics.fillRect((int)(pX - HANDLE_SIZE / 2.0)+0.5, (int)(pY - HANDLE_SIZE / 2.0)+0.5, HANDLE_SIZE, HANDLE_SIZE);
	pGraphics.setStroke(oldStroke);
	pGraphics.setFill(oldFill);
}
 
Example 8
Source File: Entity.java    From FXTutorials with MIT License 5 votes vote down vote up
public void draw(GraphicsContext g) {
    g.save();

    Rotate r = new Rotate(rotation, x + w /2, y + h / 2);
    g.setTransform(r.getMxx(), r.getMyx(), r.getMxy(), r.getMyy(), r.getTx(), r.getTy());
    g.strokeRect(x, y, w, h);

    g.restore();
}
 
Example 9
Source File: MoleculeViewSkin.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void drawBorder(GraphicsContext ctx, double w, double h)
{
    if (borderColor != null) {
        ctx.save();
        ctx.setStroke(borderColor);
        ctx.strokeRect(0, 0, w, h);
        ctx.restore();
    }
}
 
Example 10
Source File: ColorGradientAxis.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
@Override
protected void drawAxisLine(final GraphicsContext gc, final double axisLength, final double axisWidth,
        final double axisHeight) {
    // N.B. axis canvas is (by-design) larger by 'padding' w.r.t.
    // required/requested axis length (needed for nicer label placements on
    // border.
    final double paddingX = getSide().isHorizontal() ? getAxisPadding() : 0.0;
    final double paddingY = getSide().isVertical() ? getAxisPadding() : 0.0;
    // for relative positioning of axes drawn on top of the main canvas
    final double axisCentre = getCenterAxisPosition();

    final double gradientWidth = getGradientWidth();

    // save css-styled line parameters
    final Path tickStyle = getMajorTickStyle();
    gc.save();
    gc.setStroke(tickStyle.getStroke());
    gc.setLineWidth(tickStyle.getStrokeWidth());

    if (getSide().isHorizontal()) {
        gc.setFill(new LinearGradient(0, 0, axisLength, 0, false, NO_CYCLE, getColorGradient().getStops()));
    } else {
        gc.setFill(new LinearGradient(0, axisLength, 0, 0, false, NO_CYCLE, getColorGradient().getStops()));
    }

    // N.B. important: translate by padding ie. canvas is +padding larger on
    // all size compared to region
    gc.translate(paddingX, paddingY);

    switch (getSide()) {
    case LEFT:
        // axis line on right side of canvas
        gc.fillRect(snap(axisWidth - gradientWidth), snap(0), snap(axisWidth), snap(axisLength));
        gc.strokeRect(snap(axisWidth - gradientWidth), snap(0), snap(axisWidth), snap(axisLength));
        break;
    case RIGHT:
        // axis line on left side of canvas
        gc.fillRect(snap(0), snap(0), snap(gradientWidth), snap(axisLength));
        gc.strokeRect(snap(0), snap(0), snap(gradientWidth), snap(axisLength));
        break;
    case TOP:
        // line on bottom side of canvas (N.B. (0,0) is top left corner)
        gc.fillRect(snap(0), snap(axisHeight - gradientWidth), snap(axisLength), snap(axisHeight));
        gc.strokeRect(snap(0), snap(axisHeight - gradientWidth), snap(axisLength), snap(axisHeight));
        break;
    case BOTTOM:
        // line on top side of canvas (N.B. (0,0) is top left corner)
        gc.rect(snap(0), snap(0), snap(axisLength), snap(gradientWidth));
        break;
    case CENTER_HOR:
        // axis line at the centre of the canvas
        gc.fillRect(snap(0), axisCentre * axisHeight - 0.5 * gradientWidth, snap(axisLength),
                snap(axisCentre * axisHeight + 0.5 * gradientWidth));
        gc.strokeRect(snap(0), axisCentre * axisHeight - 0.5 * gradientWidth, snap(axisLength),
                snap(axisCentre * axisHeight + 0.5 * gradientWidth));
        break;
    case CENTER_VER:
        // axis line at the centre of the canvas
        gc.fillRect(snap(axisCentre * axisWidth - 0.5 * gradientWidth), snap(0),
                snap(axisCentre * axisWidth + 0.5 * gradientWidth), snap(axisLength));
        gc.strokeRect(snap(axisCentre * axisWidth - 0.5 * gradientWidth), snap(0),
                snap(axisCentre * axisWidth + 0.5 * gradientWidth), snap(axisLength));
        break;
    default:
        break;
    }
    gc.restore();
}
 
Example 11
Source File: YOLO2_TF_Client.java    From SKIL_Examples with Apache License 2.0 4 votes vote down vote up
private void renderJavaFXStyle(GraphicsContext ctx) throws Exception {

        INDArray boundingBoxPriors = Nd4j.create(YOLO2.DEFAULT_PRIOR_BOXES);

        ctx.setLineWidth(3);
        ctx.setTextAlign(TextAlignment.LEFT);

        long start = System.nanoTime();
        for (int i = 0; i < 1; i++) {
            INDArray permuted = networkGlobalOutput.permute(0, 3, 1, 2);
            INDArray activated = YoloUtils.activate(boundingBoxPriors, permuted);
            List<DetectedObject> predictedObjects = YoloUtils.getPredictedObjects(boundingBoxPriors, activated, 0.6, 0.4);
            
            //System.out.println( "width: " + imageWidth );
            //System.out.println( "height: " + imageHeight );

            for (DetectedObject o : predictedObjects) {
                ClassPrediction classPrediction = labels.decodePredictions(o.getClassPredictions(), 1).get(0).get(0);
                String label = classPrediction.getLabel();
                long x = Math.round(imageWidth  * o.getCenterX() / gridWidth);
                long y = Math.round(imageHeight * o.getCenterY() / gridHeight);
                long w = Math.round(imageWidth  * o.getWidth()   / gridWidth);
                long h = Math.round(imageHeight * o.getHeight()  / gridHeight);

                System.out.println("\"" + label + "\" at [" + x + "," + y + ";" + w + "," + h + "], score = "
                                + o.getConfidence() * classPrediction.getProbability());

                double[] xy1 = o.getTopLeftXY();
                double[] xy2 = o.getBottomRightXY();
                int x1 = (int) Math.round(imageWidth  * xy1[0] / gridWidth);
                int y1 = (int) Math.round(imageHeight * xy1[1] / gridHeight);
                int x2 = (int) Math.round(imageWidth  * xy2[0] / gridWidth);
                int y2 = (int) Math.round(imageHeight * xy2[1] / gridHeight);

                int rectW = x2 - x1;
                int rectH = y2 - y1;
                //System.out.printf("%s - %d, %d, %d, %d \n", label, x1, x2, y1, y2);
                //System.out.println( "color: " + colors.get(label) );
                ctx.setStroke(colors.get(label));
                ctx.strokeRect(x1, y1, rectW, rectH);
                
                int labelWidth = label.length() * 10;
                int labelHeight = 14;
                
                ctx.setFill( colors.get(label) );
                ctx.strokeRect(x1, y1-labelHeight, labelWidth, labelHeight);
                ctx.fillRect(x1, y1 - labelHeight, labelWidth, labelHeight);
                ctx.setFill( Color.WHITE );
                ctx.fillText(label, x1 + 3, y1 - 3 );

            }

        }

        globalEndTime = System.nanoTime();
        long end = System.nanoTime();
        System.out.println("Rendering code took: " + (end - start) / 1000000 + " ms");

        System.out.println("Overall Program Time: " + (globalEndTime - globalStartTime) / 1000000 + " ms");

    }
 
Example 12
Source File: PriorityEncoderPeer.java    From CircuitSim with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void paint(GraphicsContext graphics, CircuitState circuitState) {
	GuiUtils.drawName(graphics, this, getProperties().getValue(Properties.LABEL_LOCATION));

	Direction direction = getProperties().getValue(Properties.DIRECTION);
	graphics.translate(getScreenX(), getScreenY());

	int height;
	int width;

	int inputSideLength = ((1 << getComponent().getNumSelectBits()) + 1) * GuiUtils.BLOCK_SIZE;
	int enabledSideLength = ENABLED_INOUT_SIDE_LEN * GuiUtils.BLOCK_SIZE;

	double zeroX = GuiUtils.BLOCK_SIZE >> 1;
	double zeroY;

	if(direction == Direction.EAST || direction == Direction.WEST) {
		height = inputSideLength;
		width = enabledSideLength;
		zeroY = GuiUtils.BLOCK_SIZE * 1.5;
		if(direction == Direction.WEST) {
			zeroX = width - GuiUtils.BLOCK_SIZE;
		}
	} else {
		height = enabledSideLength;
		width = inputSideLength;
		zeroY = height - (GuiUtils.BLOCK_SIZE >> 1);
		if(direction == Direction.SOUTH) {
			zeroY = GuiUtils.BLOCK_SIZE * 1.5;
		}
	}

	graphics.setStroke(Color.BLACK);
	graphics.strokeRect(0, 0, width, height);

	graphics.setFill(Color.WHITE);
	graphics.fillRect(0, 0, width, height);

	graphics.setFill(Color.DARKGRAY);
	graphics.fillText("0", zeroX, zeroY);

	graphics.setFill(Color.BLACK);
	graphics.fillText("Pri", (width >> 1) - graphics.getFont().getSize(), (height >> 1) + 0.5 * GuiUtils.BLOCK_SIZE);
}
 
Example 13
Source File: ViewUtils.java    From JetUML with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Strokes and fills a rectangle, originally in integer coordinates, so that it aligns precisely
 * with the JavaFX coordinate system, which is 0.5 away from the pixel. See
 * the documentation for javafx.scene.shape.Shape for details.
 * 
 * @param pGraphics The graphics context on which to draw the rectangle.
 * @param pStroke The stroke (border) color for the rectangle.
 * @param pFill The fill (background) color for the rectangle.
 * @param pX The x-coordinate of the origin.
 * @param pY The y-coordinate of the origin.
 * @param pWidth The width.
 * @param pHeight The height.
 */
public static void drawRectangle(GraphicsContext pGraphics, Paint pStroke, Paint pFill, 
		int pX, int pY, int pWidth, int pHeight)
{
	Paint oldFill = pGraphics.getFill();
	Paint oldStroke = pGraphics.getStroke();
	pGraphics.setFill(pFill);
	pGraphics.setStroke(pStroke);
	pGraphics.fillRect(pX + 0.5, pY + 0.5, pWidth, pHeight);
	pGraphics.strokeRect(pX + 0.5, pY + 0.5, pWidth, pHeight);
	pGraphics.setFill(oldFill);
	pGraphics.setStroke(oldStroke);
}