當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JavaFX 類 ColorAdjust用法及代碼示例


ColorAdjust類是JavaFX的一部分。 ColorAdjust類允許per-pixel調整色調,飽和度,亮度和對比度。 ColorAdjust類繼承了Effect類。

該類的構造函數:

  1. ColorAdjust():創建ColorAdjust類的新對象
  2. ColorAdjust(double hue, double saturation, double brightness, double contrast):使用指定的色相,飽和度,亮度和對比度值創建一個新的ColorAdjust類對象。

常用方法:


方法 說明
getBrightness() 返回ColorAdjust效果的亮度值
setBrightness(double v) 設置ColorAdjust效果的亮度值
getHue() 返回ColorAdjust效果的色相值
setHue(double v) 設置ColorAdjust效果的色相值
getContrast() 返回ColorAdjust效果的Contrast值
setContrast(double v) 設置ColorAdjust效果的對比度值
getSaturation() 返回ColorAdjust效果的Saturation值
setSaturation(double v) 設置ColorAdjust效果的飽和度值
getInput() 返回屬性輸入的值
setInput(Effect v) 設置屬性輸入的值

以下示例程序旨在說明ColorAdjust類的用法:

  1. Java程序將色彩調整效果應用於具有指定色相,亮度,對比度和飽和度的圖像:在此程序中,將創建FileInputStream並將圖像作為文件的輸入。使用來自文件輸入流的輸入來創建名為image的圖像。從圖像中創建一個圖像視圖對象,並將其添加到VBox中。然後將VBox添加到場景,並將場景添加到舞台。使用函數setHue(),setBrightness(),setSaturation(),setContrast()創建ColorAdjust效果並設置色相,飽和度,對比度和亮度的值,並使用setEffect()函數將效果設置為圖像視圖。
    // Java Program to apply color Adjust effect 
    // to a image with specified hue, brightness,  
    // contrast and Saturation 
    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.layout.*; 
    import javafx.stage.Stage; 
    import javafx.scene.image.*; 
    import javafx.scene.effect.*; 
    import java.io.*; 
    import javafx.event.ActionEvent; 
    import javafx.event.EventHandler; 
    import javafx.scene.Group; 
      
    public class ColorAdjust_1 extends Application { 
      
        // launch the application 
        public void start(Stage stage) throws Exception 
        { 
      
            // set title for the stage 
            stage.setTitle("ColorAdjust example"); 
      
            // create a input stream 
            FileInputStream input = new FileInputStream("f:\\gfg.png"); 
      
            // create a image 
            Image image = new Image(input); 
      
            // create a image View 
            ImageView imageview = new ImageView(image); 
      
            // create a ColorAdjust effect 
            ColorAdjust color_adjust = new ColorAdjust(); 
      
            // set hue, saturation, brightness, and contrast 
            color_adjust.setHue(0.4); 
            color_adjust.setBrightness(0.6); 
            color_adjust.setContrast(0.8); 
            color_adjust.setSaturation(0.1); 
      
            // set effect 
            imageview.setEffect(color_adjust); 
      
            // create a VBox 
            VBox vbox = new VBox(imageview); 
      
            // create a scene 
            Scene scene = new Scene(vbox, 200, 200); 
      
            // set the scene 
            stage.setScene(scene); 
      
            stage.show(); 
        } 
      
        // Main Method 
        public static void main(String args[]) 
        { 
      
            // launch the application 
            launch(args); 
        } 
    }

    輸入圖片:

    輸出:

  2. Java程序將色彩調整效果應用於具有色相,亮度,對比度和飽和度的圖像作為來自用戶的輸入(使用文本字段):在此程序中,將創建FileInputStream並將圖像作為文件的輸入。使用來自文件輸入流的輸入來創建名為image的圖像。從圖像創建圖像視圖對象,並將其添加到VBox。然後將VBox添加到場景,並將場景添加到舞台。使用函數setHue(),setBrightness(),setSaturation(),setContrast()創建ColorAdjust效果並設置色相,飽和度,對比度和亮度的值,並使用setEffect()函數將效果設置為圖像視圖。我們將創建四個文本字段(色相,飽和度,對比度和亮度)和一個Button按鈕。用戶將提供必要的色相,飽和度,對比度和亮度值,並在按下按鈕時將這些值應用於圖像。創建一個EventHandler來處理按鈕事件。
    // Java Program to apply color Adjust effect 
    // to a image with hue, brightness, contrast 
    // and Saturation taken as input from from user 
    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.layout.*; 
    import javafx.stage.Stage; 
    import javafx.scene.image.*; 
    import javafx.scene.effect.*; 
    import java.io.*; 
    import javafx.event.ActionEvent; 
    import javafx.event.EventHandler; 
    import javafx.scene.Group; 
      
    public class ColorAdjust_2 extends Application { 
      
    // launch the application 
    public void start(Stage stage) throws Exception 
    { 
      
        // set title for the stage 
        stage.setTitle("ColorAdjust example"); 
      
        // textfields 
        TextField hue, saturation, brightness, contrast; 
      
        // create the textFields 
        hue = new TextField("Hue"); 
        saturation = new TextField("Saturation"); 
        brightness = new TextField("Brightness"); 
        contrast = new TextField("Contrast"); 
      
        // create a input stream 
        FileInputStream input = new FileInputStream("f:\\gfg.png"); 
      
        // create a image 
        Image image = new Image(input); 
      
        // create a image View 
        ImageView imageview = new ImageView(image); 
      
        // create a ColorAdjust effect 
        ColorAdjust color_adjust = new ColorAdjust(); 
      
        // create a button 
        Button button = new Button("apply"); 
      
        // action event 
        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() { 
            public void handle(ActionEvent e) 
            { 
                // set the hue, brightness, contrast and saturation 
                color_adjust.setHue(Double.parseDouble(hue.getText())); 
                  
                color_adjust.setBrightness(Double.parseDouble( 
                                         brightness.getText())); 
                                           
                color_adjust.setContrast(Double.parseDouble( 
                                        contrast.getText())); 
                                          
                color_adjust.setSaturation(Double.parseDouble( 
                                         saturation.getText())); 
            } 
        }; 
      
        // set on action of button 
        button.setOnAction(event); 
      
        // set effect 
        imageview.setEffect(color_adjust); 
      
        // create a VBox 
        VBox vbox = new VBox(imageview, hue, saturation,  
                        brightness, contrast, button); 
      
        // create a scene 
        Scene scene = new Scene(vbox, 200, 400); 
      
        // set the scene 
        stage.setScene(scene); 
      
        stage.show(); 
    } 
      
    // Main Method 
    public static void main(String args[]) 
    { 
      
        // launch the application 
        launch(args); 
    } 
    }

    輸入圖片:

    輸出:

注意:以上程序可能無法在在線IDE中運行。請使用離線編譯器。

參考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/ColorAdjust.html



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFX | ColorAdjust Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。