gpt4 book ai didi

java - 为什么在使用 ColorAdjust 应用效果后 Canvas 没有清除?

转载 作者:行者123 更新时间:2023-12-01 16:25:45 25 4
gpt4 key购买 nike

应用颜色调整效果并重新绘制 Canvas 后, Canvas 没有被清除,以便我可以用其他图像更新 Canvas ,图像重叠。

我有一个包含滚动 Pane 的边框 Pane 来调整大图像滚动 Pane 包含 Canvas

清除 Canvas 然后调整亮度并将效果应用到 Canvas 上的函数。

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ScrollPane;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {


Stage primaryStage;

// load the image
Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Gatto_europeo4.jpg/1024px-Gatto_europeo4.jpg");

// the container for the image as a javafx node
Canvas canvas = new Canvas(600,700);

GraphicsContext gc = canvas.getGraphicsContext2D();

@Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
primaryStage.setTitle("Test");

BorderPane root = new BorderPane();

// container for image layers
ScrollPane scrollPane = new ScrollPane();


// use scrollpane for image view in case the image is large
scrollPane.setContent(canvas);

gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());

changeBrightness(canvas,image);// function to add effect on canvas

// put scrollpane in scene
root.setCenter(scrollPane);
primaryStage.setScene(new Scene(root, 780.0, 620.0));
primaryStage.show();
}

private void changeBrightness(Canvas canvas, Image image) {
//clearing canvas before changing brightness working
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());

ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(0.3);
gc.setEffect(colorAdjust);

gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
//trying to clear canvas after changing brightness
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
gc.drawImage(image,0,0,canvas.getWidth()/2,canvas.getHeight()/2);
}


public static void main(String[] args) {
launch(args);
}
}

最佳答案

在调用之前,您必须删除对 Graphics 对象的影响 gc.clearRect(...)

代码如下:

package com.simple.test;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ScrollPane;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {


Stage primaryStage;

// load the image
Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Gatto_europeo4.jpg/1024px-Gatto_europeo4.jpg");

// the container for the image as a javafx node
Canvas canvas = new Canvas(600,700);

GraphicsContext gc = canvas.getGraphicsContext2D();

@Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
primaryStage.setTitle("Test");

BorderPane root = new BorderPane();

// container for image layers
ScrollPane scrollPane = new ScrollPane();


// use scrollpane for image view in case the image is large
scrollPane.setContent(canvas);

gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());

changeBrightness(canvas,image);// function to add effect on canvas

// put scrollpane in scene
root.setCenter(scrollPane);
primaryStage.setScene(new Scene(root, 780.0, 620.0));
primaryStage.show();
}

private void changeBrightness(Canvas canvas, Image image) {
//clearing canvas before changing brightness working
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());

ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(0.3);
gc.setEffect(colorAdjust);

gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
// Remove the effect
gc.setEffect(null);
//trying to clear canvas after changing brightness
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
// Now apply again
gc.setEffect(colorAdjust);
// Redraw
gc.drawImage(image,0,0,canvas.getWidth()/2,canvas.getHeight()/2);
// Remove the effect again
gc.setEffect(null);
}


public static void main(String[] args) {
launch(args);
}
}

您可以查看此内容以获取更多信息:
JavaFX GraphicsContext clearRect doesn't work with clip mask
Why won't gc.clearRect clear the canvas?

顺便说一句,猫很可爱。

关于java - 为什么在使用 ColorAdjust 应用效果后 Canvas 没有清除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62152784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com