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

The following examples show how to use javafx.scene.canvas.GraphicsContext#setGlobalAlpha() . 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: FireworksSample.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
public void draw(GraphicsContext context) {
    final double x = Math.round(posX);
    final double y = Math.round(posY);
    final double xVel = (x - lastPosX) * -5;
    final double yVel = (y - lastPosY) * -5;
    // set the opacity for all drawing of this particle
    context.setGlobalAlpha(Math.random() * this.alpha);
    // draw particle
    context.setFill(color);
    context.fillOval(x-size, y-size, size+size, size+size);
    // draw the arrow triangle from where we were to where we are now
    if (hasTail) {
        context.setFill(Color.rgb(255,255,255,0.3));
        context.fillPolygon(new double[]{posX + 1.5,posX + xVel,posX - 1.5}, 
                new double[]{posY,posY + yVel,posY}, 3);
    }
}
 
Example 3
Source File: FireworksSample.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
public void draw(GraphicsContext context) {
    final double x = Math.round(posX);
    final double y = Math.round(posY);
    final double xVel = (x - lastPosX) * -5;
    final double yVel = (y - lastPosY) * -5;
    // set the opacity for all drawing of this particle
    context.setGlobalAlpha(Math.random() * this.alpha);
    // draw particle
    context.setFill(color);
    context.fillOval(x-size, y-size, size+size, size+size);
    // draw the arrow triangle from where we were to where we are now
    if (hasTail) {
        context.setFill(Color.rgb(255,255,255,0.3));
        context.fillPolygon(new double[]{posX + 1.5,posX + xVel,posX - 1.5}, 
                new double[]{posY,posY + yVel,posY}, 3);
    }
}
 
Example 4
Source File: BlinkControlHandler.java    From arma-dialog-creator with MIT License 6 votes vote down vote up
/**
 Will manipulate {@link GraphicsContext#getGlobalAlpha()} based on an internal clock.
 */
public void paint(@NotNull GraphicsContext gc) {
	long now = System.currentTimeMillis();
	long timePast = now - lastPaint;
	lastPaint = now;

	if (!blinkDurationSet) {
		return;
	}

	if (blinkIn) {
		blinkDurationPast += timePast;
		if (blinkDurationPast >= blinkDuration) {
			blinkIn = false;
			blinkDurationPast = (long) blinkDuration;
		}
	} else {
		blinkDurationPast -= timePast;
		if (blinkDurationPast <= 0) {
			blinkIn = true;
			blinkDurationPast = 0;
		}
	}
	gc.setGlobalAlpha(blinkDurationPast / blinkDuration);
}
 
Example 5
Source File: ParticlesClockApp.java    From FXTutorials with MIT License 5 votes vote down vote up
void render(GraphicsContext g) {
    Point2D center = new Point2D(350, 200);
    double alpha = 1 - new Point2D(x, y).distance(center) / 500;

    g.setGlobalAlpha(alpha);
    g.fillOval(x, y, 2, 2);
}
 
Example 6
Source File: WTKTiledMap.java    From javafx-TKMapEditor with GNU General Public License v3.0 5 votes vote down vote up
public void draw(GraphicsContext gContext2D) {
	if (isLoadFinished) {
		// 绘制多图层地图
		int length = mapLayerList.size();
		int tileWidth = WTKMap.getInstance().getTileWidth();
		int tileHeight = WTKMap.getInstance().getTileHeight();
		if (length > 0) {
			for (int i = length - 1; i >= 0; i--) {
				WTKLayer mapLayer = mapLayerList.get(i);
				if (mapLayer.isVisible()) {
					WTKMapTile[][] tiles = mapLayer.getMapTiles();
					gContext2D.setGlobalAlpha(mapLayer.getAlpha());
					if (tiles != null) {
						for (int y = 0; y < tiles.length; y++) {
							for (int x = 0; x < tiles[0].length; x++) {
								if (tiles[y][x] != null) {
									AltasResource resource = WAltasResourceManager.getInstance().getResourceById(
											tiles[y][x].getAltasId());
									if (resource != null) {
										Image image = resource.getImage();
										int index = tiles[y][x].getAltasIndex();
										if (index != -1) {
											int cellX = (int) (image.getWidth() / tileWidth);
											int col = index % cellX;
											int row = index / cellX;
											gContext2D.drawImage(image, col * tileWidth, row * tileHeight,
													tileWidth, tileHeight, getX() + x * tileWidth, getY() + y
															* tileHeight, tileWidth, tileHeight);
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
 
Example 7
Source File: ColorArrayValueEditor.java    From arma-dialog-creator with MIT License 4 votes vote down vote up
public ArrayEditorPopup(@Nullable Color initialColor) {
	VBox root = new VBox(5);
	root.setPadding(new Insets(10));
	getContent().add(root);

	root.setStyle("-fx-background-color:-fx-background");
	root.setBorder(new Border(
					new BorderStroke(Color.GRAY, BorderStrokeStyle.SOLID,
							CornerRadii.EMPTY, BorderStroke.THIN
					)
			)
	);

	Canvas canvas = new Canvas(128, 32);
	StackPane stackPaneCanvas = new StackPane(canvas);
	stackPaneCanvas.setBorder(root.getBorder());
	stackPaneCanvas.setMaxWidth(canvas.getWidth());
	stackPaneCanvas.setAlignment(Pos.CENTER_LEFT);

	HBox paneHeader = new HBox(5, stackPaneCanvas, tfAsArray);
	root.getChildren().add(paneHeader);
	HBox.setHgrow(tfAsArray, Priority.ALWAYS);

	tfAsArray.setEditable(false);

	GraphicsContext gc = canvas.getGraphicsContext2D();


	ValueListener<Double> valListener = (observer, oldValue, newValue) -> {
		Color c = getCurrentColor();
		if (c != null) {

			if (c.getOpacity() < 1) {
				//draw a grid to show theres transparency
				gc.setGlobalAlpha(1);
				gc.setFill(Color.WHITE);
				Region.paintCheckerboard(
						gc, 0, 0, (int) canvas.getWidth(), (int) canvas.getHeight(), Color.GRAY, Color.WHITE,
						5);
			}

			gc.setGlobalAlpha(c.getOpacity());
			gc.setFill(c);
			gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());

			tfAsArray.setText(SVColor.toStringF(c.getRed(), c.getGreen(), c.getBlue(), c.getOpacity()));
		}
	};
	r.getValueObserver().addListener(valListener);
	g.getValueObserver().addListener(valListener);
	b.getValueObserver().addListener(valListener);
	a.getValueObserver().addListener(valListener);

	if (initialColor != null) {
		r.getValueObserver().updateValue(getRounded(initialColor.getRed()));
		g.getValueObserver().updateValue(getRounded(initialColor.getGreen()));
		b.getValueObserver().updateValue(getRounded(initialColor.getBlue()));
		a.getValueObserver().updateValue(getRounded(initialColor.getOpacity()));
	}


	//r
	root.getChildren().add(getColorEditor("r", r));
	//g
	root.getChildren().add(getColorEditor("g", g));
	//b
	root.getChildren().add(getColorEditor("b", b));
	//a
	root.getChildren().add(getColorEditor("a", a));

	//footer
	root.getChildren().add(new Separator(Orientation.HORIZONTAL));
	GenericResponseFooter footer = new GenericResponseFooter(true, true, false,
			null,
			cancelEvent -> {
				cancelled = true;
				ArrayEditorPopup.this.hide();
			},
			okEvent -> {
				if (!hasInvalid()) {
					return;
				}
				cancelled = false;
				ArrayEditorPopup.this.hide();
			}
	);
	ResourceBundle bundle = Lang.ApplicationBundle();
	footer.getBtnOk().setText(bundle.getString("ValueEditors.ColorArrayEditor.use"));
	root.getChildren().add(footer);

	setAutoHide(true);

	setHideOnEscape(true); //when push esc key, hide it

	valListener.valueUpdated(r.getValueObserver(), null, null);
}
 
Example 8
Source File: CircuitBoard.java    From CircuitSim with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void paint(GraphicsContext graphics, LinkWires highlightLinkWires) {
	CircuitState currentState = new CircuitState(this.currentState);
	
	components.forEach(component -> {
		if(moveElements == null || !moveElements.contains(component)) {
			paintComponent(graphics, currentState, component);
		}
	});
	
	for(LinkWires linkWires : links) {
		for(Wire wire : linkWires.getWires()) {
			paintWire(graphics, currentState, wire, linkWires == highlightLinkWires);
		}
	}
	
	if(badLinks != null) {
		for(LinkWires badLink : badLinks) {
			Stream.concat(badLink.getPorts().stream(),
			              badLink.getInvalidPorts().stream()).forEach(port -> {
				graphics.setFill(Color.BLACK);
				graphics.fillText(String.valueOf(port.getPort().getLink().getBitSize()),
				                  port.getScreenX() + 11,
				                  port.getScreenY() + 21);
				
				graphics.setStroke(Color.ORANGE);
				graphics.setFill(Color.ORANGE);
				graphics.strokeOval(port.getScreenX() - 2, port.getScreenY() - 2, 10, 10);
				graphics.fillText(String.valueOf(port.getPort().getLink().getBitSize()),
				                  port.getScreenX() + 10,
				                  port.getScreenY() + 20);
			});
		}
	}
	
	if(moveElements != null) {
		graphics.save();
		graphics.setGlobalAlpha(0.5);
		
		for(GuiElement element : moveElements) {
			if(element instanceof ComponentPeer<?>) {
				paintComponent(graphics, currentState, (ComponentPeer<?>)element);
			} else if(element instanceof Wire) {
				paintWire(graphics, currentState, (Wire)element, false);
			}
		}
		
		MoveComputeResult result = this.moveResult;
		if(result != null) {
			graphics.setFill(Color.RED);
			result.wiresToRemove.forEach(wire -> wire.paint(graphics));
			
			graphics.setFill(Color.BLACK);
			result.wiresToAdd.forEach(wire -> wire.paint(graphics));
		}
		
		graphics.restore();
	}
}
 
Example 9
Source File: Particle.java    From FXTutorials with MIT License 4 votes vote down vote up
public void draw(GraphicsContext g) {
    g.setFill(color);

    g.setGlobalAlpha(life);
    g.fillOval(getX(), getY(), 1, 1);
}