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

The following examples show how to use javafx.scene.canvas.GraphicsContext#clearRect() . 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: AmpSkin.java    From Medusa with Apache License 2.0 6 votes vote down vote up
private void drawLed(final GraphicsContext CTX) {
    CTX.clearRect(0, 0, ledSize, ledSize);
    CTX.setFill(frameGradient);
    CTX.fillOval(0, 0, ledSize, ledSize);

    CTX.save();
    if (gauge.isLedOn()) {
        CTX.setEffect(ledOnShadow);
        CTX.setFill(ledOnGradient);
    } else {
        CTX.setEffect(ledOffShadow);
        CTX.setFill(ledOffGradient);
    }
    CTX.fillOval(0.14 * ledSize, 0.14 * ledSize, 0.72 * ledSize, 0.72 * ledSize);
    CTX.restore();

    CTX.setFill(highlightGradient);
    CTX.fillOval(0.21 * ledSize, 0.21 * ledSize, 0.58 * ledSize, 0.58 * ledSize);
}
 
Example 2
Source File: CustomPlainAmpSkin.java    From medusademo with Apache License 2.0 6 votes vote down vote up
private void drawLed(final GraphicsContext CTX) {
    CTX.clearRect(0, 0, ledSize, ledSize);
    CTX.setFill(frameGradient);
    CTX.fillOval(0, 0, ledSize, ledSize);

    CTX.save();
    if (gauge.isLedOn()) {
        CTX.setEffect(ledOnShadow);
        CTX.setFill(ledOnGradient);
    } else {
        CTX.setEffect(ledOffShadow);
        CTX.setFill(ledOffGradient);
    }
    CTX.fillOval(0.14 * ledSize, 0.14 * ledSize, 0.72 * ledSize, 0.72 * ledSize);
    CTX.restore();

    CTX.setFill(highlightGradient);
    CTX.fillOval(0.21 * ledSize, 0.21 * ledSize, 0.58 * ledSize, 0.58 * ledSize);
}
 
Example 3
Source File: ChartCanvas.java    From jfreechart-fx with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Draws the content of the canvas and updates the 
 * {@code renderingInfo} attribute with the latest rendering 
 * information.
 */
public final void draw() {
    GraphicsContext ctx = getGraphicsContext2D();
    ctx.save();
    double width = getWidth();
    double height = getHeight();
    if (width > 0 && height > 0) {
        ctx.clearRect(0, 0, width, height);
        this.info = new ChartRenderingInfo();
        if (this.chart != null) {
            this.chart.draw(this.g2, new Rectangle((int) width, 
                    (int) height), this.anchor, this.info);
        }
    }
    ctx.restore();
    for (OverlayFX overlay : this.overlays) {
        overlay.paintOverlay(g2, this);
    }
    this.anchor = null;
}
 
Example 4
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 5
Source File: TaChartCanvas.java    From TAcharting with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Draws the content colorOf the canvas and updates the
 * {@code renderingInfo} attribute with the latest rendering
 * information.
 */
public final void draw() {
    GraphicsContext ctx = getGraphicsContext2D();
    ctx.save();
    double width = getWidth();
    double height = getHeight();
    if (width > 0 && height > 0) {
        ctx.clearRect(0, 0, width, height);
        this.info = new ChartRenderingInfo();
        if (this.chart != null) {
            this.chart.draw(this.g2, new Rectangle((int) width,
                    (int) height), this.anchor, this.info);
        }
    }
    ctx.restore();
    for (TaOverlayFX overlay : this.overlays) {
        overlay.paintOverlay(g2, this);
    }
    this.anchor = null;
}
 
Example 6
Source File: DefaultControlTreeItemGraphic.java    From arma-dialog-creator with MIT License 6 votes vote down vote up
private void fillBox(Color color) {
	GraphicsContext gc = box.getGraphicsContext2D();
	gc.save();
	gc.clearRect(0, 0, box.getWidth(), box.getHeight());
	gc.setFill(color);
	gc.fillRect(0, 0, box.getWidth(), box.getHeight());
	gc.restore();

	//generate hex string and bind it to tooltip
	final double f = 255.0;
	int r = (int) (color.getRed() * f);
	int g = (int) (color.getGreen() * f);
	int b = (int) (color.getBlue() * f);
	String opacity = DecimalFormat.getNumberInstance().format(color.getOpacity() * 100);

	Tooltip.install(box, new Tooltip(String.format(
			"red:%d, green:%d, blue:%d, opacity:%s%%", r, g, b, opacity))
	);
}
 
Example 7
Source File: ChartCanvas.java    From ccu-historian with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the content of the canvas and updates the 
 * {@code renderingInfo} attribute with the latest rendering 
 * information.
 */
public final void draw() {
    GraphicsContext ctx = getGraphicsContext2D();
    ctx.save();
    double width = getWidth();
    double height = getHeight();
    if (width > 0 && height > 0) {
        ctx.clearRect(0, 0, width, height);
        this.info = new ChartRenderingInfo();
        this.chart.draw(this.g2, new Rectangle((int) width, (int) height), 
                this.anchor, this.info);
    }
    ctx.restore();
    this.anchor = null;
}
 
Example 8
Source File: ChartCanvas.java    From ECG-Viewer with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Draws the content of the canvas and updates the 
 * {@code renderingInfo} attribute with the latest rendering 
 * information.
 */
public final void draw() {
    GraphicsContext ctx = getGraphicsContext2D();
    ctx.save();
    double width = getWidth();
    double height = getHeight();
    if (width > 0 && height > 0) {
        ctx.clearRect(0, 0, width, height);
        this.info = new ChartRenderingInfo();
        this.chart.draw(this.g2, new Rectangle((int) width, (int) height), 
                this.anchor, this.info);
    }
    ctx.restore();
    this.anchor = null;
}
 
Example 9
Source File: ChartCanvas.java    From SIMVA-SoS with Apache License 2.0 5 votes vote down vote up
/**
 * Draws the content of the canvas and updates the 
 * {@code renderingInfo} attribute with the latest rendering 
 * information.
 */
public final void draw() {
    GraphicsContext ctx = getGraphicsContext2D();
    ctx.save();
    double width = getWidth();
    double height = getHeight();
    if (width > 0 && height > 0) {
        ctx.clearRect(0, 0, width, height);
        this.info = new ChartRenderingInfo();
        this.chart.draw(this.g2, new Rectangle((int) width, (int) height), 
                this.anchor, this.info);
    }
    ctx.restore();
    this.anchor = null;
}
 
Example 10
Source File: MoleculeViewSkin.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void drawBackground(GraphicsContext ctx, double w, double h)
    {
        ctx.save();
        ctx.clearRect(0, 0, w, h);
        if (!control.isTransparent()) {
            ctx.setFill(getBackgroundColor());
            ctx.fillRect(0, 0, w, h);
        } else {
//            System.out.println("Controle is Transparent");
        }
        ctx.restore();
    }
 
Example 11
Source File: BulletChartSkin.java    From Medusa with Apache License 2.0 5 votes vote down vote up
private void drawSections(final GraphicsContext CTX) {
    sectionsCanvas.setCache(false);
    CTX.clearRect(0, 0, sectionsCanvas.getWidth(), sectionsCanvas.getHeight());
    CTX.setFill(gauge.getBackgroundPaint());
    if (Orientation.VERTICAL == orientation) {
        CTX.fillRect(0, 0, 0.5 * width, 0.9 * height);
    } else {
        CTX.fillRect(0, 0, 0.79699248 * width, 0.5 * height);
    }

    double tmpStepSize = stepSize * 1.11111111;
    double minValue = gauge.getMinValue();
    double maxValue = gauge.getMaxValue();

    int listSize = gauge.getSections().size();
    for (int i = 0 ; i < listSize ; i++) {
        final Section SECTION = gauge.getSections().get(i);
        final double SECTION_START;

        final double SECTION_SIZE = SECTION.getRange() * tmpStepSize;
        if (Double.compare(SECTION.getStart(), maxValue) <= 0 && Double.compare(SECTION.getStop(), minValue) >= 0) {
            if (Double.compare(SECTION.getStart(), minValue) < 0 && Double.compare(SECTION.getStop(), maxValue) < 0) {
                SECTION_START = minValue * tmpStepSize;
            } else {
                SECTION_START = height - (SECTION.getStart() * tmpStepSize) - SECTION_SIZE;
            }
            CTX.save();
            CTX.setFill(SECTION.getColor());
            if (Orientation.VERTICAL == orientation) {
                CTX.fillRect(0.0, SECTION_START, 0.5 * width, SECTION_SIZE);
            } else {
                CTX.fillRect(SECTION_START, 0.0, SECTION_SIZE, 0.5 * height);
            }
            CTX.restore();
        }
    }
    sectionsCanvas.setCache(true);
    sectionsCanvas.setCacheHint(CacheHint.QUALITY);
}
 
Example 12
Source File: TagBoard.java    From OpenLabeler with Apache License 2.0 5 votes vote down vote up
private void clearLastDrawing(GraphicsContext gc, List<Point2D> points, Point2D lastPt) {
   List<Point2D> clearPts = new ArrayList<>();
   clearPts.addAll(points);
   if (lastPt != null) {
      clearPts.add(lastPt);
   }
   var bounds = AppUtils.getBounds(clearPts.toArray(new Point2D[] {}));
   bounds = AppUtils.insetBounds(bounds, -10d);
   gc.clearRect(bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight());
}
 
Example 13
Source File: OverlayPane.java    From paintera with GNU General Public License v2.0 5 votes vote down vote up
public void drawOverlays()
{
	final Runnable r = () -> {
		final Canvas          canvas = canvasPane.getCanvas();
		final GraphicsContext gc     = canvas.getGraphicsContext2D();
		gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
		overlayRenderers.forEach(or -> or.drawOverlays(gc));
	};
	InvokeOnJavaFXApplicationThread.invoke(r);
}
 
Example 14
Source File: ChartCanvas.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the content of the canvas and updates the 
 * {@code renderingInfo} attribute with the latest rendering 
 * information.
 */
public final void draw() {
    GraphicsContext ctx = getGraphicsContext2D();
    ctx.save();
    double width = getWidth();
    double height = getHeight();
    if (width > 0 && height > 0) {
        ctx.clearRect(0, 0, width, height);
        this.info = new ChartRenderingInfo();
        this.chart.draw(this.g2, new Rectangle((int) width, (int) height), 
                this.anchor, this.info);
    }
    ctx.restore();
    this.anchor = null;
}
 
Example 15
Source File: GridDrawer.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
public void draw(final Canvas canvas) {
    // registerCanvasMouseLiner(canvas); // TODO move elsewhere

    final GraphicsContext gc = canvas.getGraphicsContext2D();
    gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());

    for (final Hexagon hexagon : map.getAllHexagons()) {
        // draw hexagon according to Node specifications
        hexagon.draw(gc);

        if (map.renderCoordinates) {
            hexagon.renderCoordinates(gc);
        }
    }
}
 
Example 16
Source File: ChartCanvas.java    From openstock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the content of the canvas and updates the 
 * {@code renderingInfo} attribute with the latest rendering 
 * information.
 */
public final void draw() {
    GraphicsContext ctx = getGraphicsContext2D();
    ctx.save();
    double width = getWidth();
    double height = getHeight();
    if (width > 0 && height > 0) {
        ctx.clearRect(0, 0, width, height);
        this.info = new ChartRenderingInfo();
        this.chart.draw(this.g2, new Rectangle((int) width, (int) height), 
                this.anchor, this.info);
    }
    ctx.restore();
    this.anchor = null;
}
 
Example 17
Source File: BulletChartSkin.java    From Medusa with Apache License 2.0 4 votes vote down vote up
private void drawTickMarks(final GraphicsContext CTX) {
    tickMarkCanvas.setCache(false);
    CTX.clearRect(0, 0, tickMarkCanvas.getWidth(), tickMarkCanvas.getHeight());
    CTX.setFill(gauge.getMajorTickMarkColor());

    List<Section> tickMarkSections         = gauge.getTickMarkSections();
    List<Section> tickLabelSections        = gauge.getTickLabelSections();
    Color         majorTickMarkColor       = gauge.getTickMarkColor();
    Color         tickLabelColor           = gauge.getTickLabelColor();
    boolean       smallRange               = Double.compare(gauge.getRange(), 10.0) <= 0;
    double        minValue                 = gauge.getMinValue();
    double        maxValue                 = gauge.getMaxValue();
    double        tmpStepSize              = smallRange ? stepSize / 10 : stepSize;
    Font          tickLabelFont            = Fonts.robotoRegular(0.1 * size);
    boolean       tickMarkSectionsVisible  = gauge.getTickMarkSectionsVisible();
    boolean       tickLabelSectionsVisible = gauge.getTickLabelSectionsVisible();
    double        offsetX                  = 0.18345865 * width;
    double        offsetY                  = 0.1 * height;
    double        innerPointX              = 0;
    double        innerPointY              = 0;
    double        outerPointX              = 0.07 * width;
    double        outerPointY              = 0.08 * height;
    double        textPointX               = Orientation.HORIZONTAL == orientation ? 0.55 * tickMarkCanvas.getWidth() : outerPointX + size * 0.05;
    double        textPointY               = 0.7 * tickMarkCanvas.getHeight();
    BigDecimal    minorTickSpaceBD         = BigDecimal.valueOf(gauge.getMinorTickSpace());
    BigDecimal    majorTickSpaceBD         = BigDecimal.valueOf(gauge.getMajorTickSpace());
    BigDecimal    counterBD                = BigDecimal.valueOf(gauge.getMinValue());
    double        counter                  = minValue;
    double        range                    = gauge.getRange();

    for (double i = 0 ; Double.compare(i, range) <= 0 ; i++) {
        if (Orientation.VERTICAL == orientation) {
            innerPointY = counter * tmpStepSize + offsetY;
            outerPointY = innerPointY;
            textPointY  = innerPointY;
        } else {
            innerPointX = counter * tmpStepSize + offsetX;
            outerPointX = innerPointX;
            textPointX  = innerPointX;
        }

        // Set the general tickmark color
        CTX.setStroke(gauge.getTickMarkColor());
        if (Double.compare(counterBD.remainder(majorTickSpaceBD).doubleValue(), 0.0) == 0) {
            // Draw major tick mark
            if (gauge.getMajorTickMarksVisible()) {
                CTX.setFill(tickMarkSectionsVisible ? Helper.getColorOfSection(tickMarkSections, counter, majorTickMarkColor) : majorTickMarkColor);
                CTX.setStroke(tickMarkSectionsVisible ? Helper.getColorOfSection(tickMarkSections, counter, majorTickMarkColor) : majorTickMarkColor);
                CTX.setLineWidth(1);
                CTX.strokeLine(innerPointX, innerPointY, outerPointX, outerPointY);
            }
            // Draw tick label text
            if (gauge.getTickLabelsVisible()) {
                CTX.save();
                CTX.translate(textPointX, textPointY);
                CTX.setFont(tickLabelFont);
                CTX.setTextAlign(Orientation.HORIZONTAL == orientation ? TextAlignment.CENTER : TextAlignment.LEFT);
                CTX.setTextBaseline(VPos.CENTER);
                CTX.setFill(tickLabelSectionsVisible ? Helper.getColorOfSection(tickLabelSections, counter, tickLabelColor) : tickLabelColor);
                if (Orientation.VERTICAL == orientation) {
                    CTX.fillText(Integer.toString((int) (maxValue - counter)), 0, 0);
                } else {
                    CTX.fillText(Integer.toString((int) counter), 0, 0);
                }
                CTX.restore();
            }
        }
        counterBD = counterBD.add(minorTickSpaceBD);
        counter   = counterBD.doubleValue();
        if (counter > maxValue) break;
    }

    tickMarkCanvas.setCache(true);
    tickMarkCanvas.setCacheHint(CacheHint.QUALITY);
}
 
Example 18
Source File: AbstractAxis.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
protected void clearAxisCanvas(final GraphicsContext gc, final double width, final double height) {
    gc.clearRect(0, 0, width, height);
}
 
Example 19
Source File: XYChart.java    From chart-fx with Apache License 2.0 4 votes vote down vote up
@Override
protected void redrawCanvas() {
    if (DEBUG && LOGGER.isDebugEnabled()) {
        LOGGER.debug("   xychart redrawCanvas() - pre");
    }
    setAutoNotification(false);
    FXUtils.assertJavaFxThread();
    final long now = System.nanoTime();
    final double diffMillisSinceLastUpdate = TimeUnit.NANOSECONDS.toMillis(now - lastCanvasUpdate);
    if (diffMillisSinceLastUpdate < XYChart.BURST_LIMIT_MS) {
        if (!callCanvasUpdateLater) {
            callCanvasUpdateLater = true;
            // repaint 20 ms later in case this was just a burst operation
            final KeyFrame kf1 = new KeyFrame(Duration.millis(20), e -> requestLayout());

            final Timeline timeline = new Timeline(kf1);
            Platform.runLater(timeline::play);
        }

        return;
    }
    if (DEBUG && LOGGER.isDebugEnabled()) {
        LOGGER.debug("   xychart redrawCanvas() - executing");
        LOGGER.debug("   xychart redrawCanvas() - canvas size = {}",
                String.format("%fx%f", canvas.getWidth(), canvas.getHeight()));
    }

    lastCanvasUpdate = now;
    callCanvasUpdateLater = false;

    final GraphicsContext gc = canvas.getGraphicsContext2D();
    gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());

    if (!gridRenderer.isDrawOnTop()) {
        gridRenderer.render(gc, this, 0, null);
    }

    int dataSetOffset = 0;
    for (final Renderer renderer : getRenderers()) {
        // check for and add required axes
        checkRendererForRequiredAxes(renderer);

        renderer.render(gc, this, dataSetOffset, getDatasets());
        dataSetOffset += getDatasets().size() + renderer.getDatasets().size();
    }

    if (gridRenderer.isDrawOnTop()) {
        gridRenderer.render(gc, this, 0, null);
    }
    setAutoNotification(true);
    if (DEBUG && LOGGER.isDebugEnabled()) {
        LOGGER.debug("   xychart redrawCanvas() - done");
    }
}
 
Example 20
Source File: Example3T.java    From Introduction-to-JavaFX-for-Game-Development with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void start(Stage theStage) 
{
    theStage.setTitle( "Timeline Example" );
    
    Group root = new Group();
    Scene theScene = new Scene( root );
    theStage.setScene( theScene );
    
    Canvas canvas = new Canvas( 512, 512 );
    root.getChildren().add( canvas );
    
    GraphicsContext gc = canvas.getGraphicsContext2D();
    
    Image earth = new Image( "earth.png" );
    Image sun   = new Image( "sun.png" );
    Image space = new Image( "space.png" );
    
    Timeline gameLoop = new Timeline();
    gameLoop.setCycleCount( Timeline.INDEFINITE );
    
    final long timeStart = System.currentTimeMillis();
    
    KeyFrame kf = new KeyFrame(
        Duration.seconds(0.017),                // 60 FPS
        new EventHandler<ActionEvent>()
        {
            public void handle(ActionEvent ae)
            {
                double t = (System.currentTimeMillis() - timeStart) / 1000.0; 
                            
                double x = 232 + 128 * Math.cos(t);
                double y = 232 + 128 * Math.sin(t);
                
                // Clear the canvas
                gc.clearRect(0, 0, 512,512);
                
                // background image clears canvas
                gc.drawImage( space, 0, 0 );
                gc.drawImage( earth, x, y );
                gc.drawImage( sun, 196, 196 );
            }
        });
    
    gameLoop.getKeyFrames().add( kf );
    gameLoop.play();
    
    theStage.show();
}