Thứ Tư, 27 tháng 5, 2015

[JavaFX 8] Phần 8 (tt) : Hiệu ứng - Effect

Phần 8 (tt) : Hiệu ứng - Effect

8. ColorAdjust

ColorAdjust cho ra output là kết quả của quá trình chỉnh sửa một phần hay tất cả các giá trị hue, saturation, brightnesscontrast của input.
  • Hue : được chia thành màu và sắc, màu có thể hiểu như đỏ, xanh, vàng, tím,...Còn sắc thì hiểu như : tươi, lợt, nhạt, thâm, rực rỡ,...Thay đổi Hue thì màu sẽ thay đổi theo.
  • Saturation : là độ rực, khi tăng saturation thì màu sẽ bừng lên chói cả mắt, còn kéo thấp thi màu trở nên nhạt và thành trắng đen luôn nếu saturation = 0.
  • Brightness : là độ sáng tối, nếu brightness càng lớn thì input càng sáng, ngược lại brightness càng nhỏ thì input càng tối.
  • Contrast : là độ sắc nét, điều chỉnh độ sắc nét của input.

a.Các thuộc tính :

kiểu
Tên thuộc tính
Giá trị mặc định
Miêu tả
Effect
input
null
input của ColorAdjust
DoubleProperty
hue
0.0
thuộc tính hue có giá trị từ 0.0 -> 1.0. Giá trị 0, hue không thay đổi.
DoubleProperty
saturation
0.0
thuộc tính saturation có giá trị từ  -1.0 -> 1.0 . Giá trị 0, saturation không thay đổi.
DoubleProperty
brightness
0.0
thuộc tính brightness có giá trị từ -1.0 -> 1.0. Giá trị 0, brightness không thay đổi.
DoubleProperty
contrast
1.0
thuộc tính contrast có giá trị từ 0.25 -> 4. Giá trị 1, constrast không thay đổi.

b. Cách sử dụng :

// Tạo đối tượng ColorAdjust với giá trị thuộc tính mặc định
ColorAdjust colorAdjust  = new ColorAdjust();
// Tạo đối tượng ColorAdjust với hue = 0.5, saturation = 0.3, brightness = 0.7, contrast = 2
ColorAdjust colorAdjust  = new ColorAdjust(0.5,0.3,0.7,2);

  c. Một số phương thức của ColorAdjust:

kiểu trả về
Phương thức
Miêu tả
void
setInput(Effect  value)
Cung cấp input cho ColorAdjust effect, nếu bạn không cung cấp input thì node sẽ là input.
Effect
getInput ()
Trả về input dành cho ColorAdjust.
void
setHue(double value)
Thiết lập giá trị thuộc tính hue
double
getHue()
Trả về giá trị thuộc tính hue.
void
setSaturation(double value)
Thiết lập giá trị thuộc tính Saturation
double
getSaturation()
Trả về giá trị thuộc tính Saturation
void
setBrightness(double value)
Thiết lập giá trị thuộc tính Brightness
double
getBrightness()
Trả về giá trị thuộc tính Brightness
void
setContrast(double value)
Thiết lập giá trị thuộc tính Contrast
double
getContrast()
Trả về giá trị thuộc tính Contrast
Ví dụ : viết chương trình xử lý image sử dụng ColorAdjust như hình bên dưới :
Code :
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class exampleColorAdjust extends Application {

   //ColorAdjust Effect
   ColorAdjust adjust = new ColorAdjust();

   public void start(Stage primaryStage) {

       // tạo rectangle chứa image và hiệu ứng ColorAdjust
       Rectangle rectangle = new Rectangle(20, 20, 350, 200);
       Image image = new Image("graphics/background.jpg");
       rectangle.setFill(new ImagePattern(image));
       rectangle.setEffect(adjust);

       // tạo borderPane chứa label và silder dành cho Hue
       BorderPane hueBorderPane = createPane("Hue", 0.0, 1.0, 0);
       hueBorderPane.setLayoutX(20);
       hueBorderPane.setLayoutY(240);

       // tạo borderPane chứa label và silder dành cho Saturation
       BorderPane saturationBorderPane = createPane("Saturation", -1.0, 1.0, 0);
       saturationBorderPane.setLayoutX(20);
       saturationBorderPane.setLayoutY(260);

       // tạo borderPane chứa label và silder dành cho Saturation
       BorderPane brightnessBorderPane = createPane("Brightness", -1.0, 1.0, 0);
       brightnessBorderPane.setLayoutX(20);
       brightnessBorderPane.setLayoutY(280);

       // tạo borderPane chứa label và silder dành cho Contrast
       BorderPane contrastBorderPane = createPane("Contrast", 0.25, 4.0, 1);
       contrastBorderPane.setLayoutX(20);
       contrastBorderPane.setLayoutY(300);

       Group root = new Group();
       root.getChildren().add(rectangle);
       root.getChildren().add(hueBorderPane);
       root.getChildren().add(saturationBorderPane);
       root.getChildren().add(brightnessBorderPane);
       root.getChildren().add(contrastBorderPane);

       Scene scene = new Scene(root, 400, 330);
       primaryStage.setTitle("Hello World!");
       primaryStage.setScene(scene);
       primaryStage.show();

   }

   public BorderPane createPane(String labelname, double min, double max, double value) {
       Label label = new Label(labelname + " : ");
       Slider slider = new Slider(min, max, value);
       BorderPane borderPane = new BorderPane(slider);
       borderPane.setPrefWidth(350);
       borderPane.setLeft(label);

       // xử lý sự kiện khi giá trị value của slider thay đổi
       slider.valueProperty().addListener(new ChangeListener<Number>() {
           public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
               if (labelname.equals("Hue")) {
                   adjust.setHue(newValue.doubleValue());
               }
               if (labelname.equals("Saturation")) {
                   adjust.setSaturation(newValue.doubleValue());
               }
               if (labelname.equals("Brightness")) {
                   adjust.setBrightness(newValue.doubleValue());
               }
               if (labelname.equals("Contrast")) {
                   adjust.setContrast(newValue.doubleValue());
               }
           }
       });
       return borderPane;
   }

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

9. Reflection :

Reflection là hiệu ứng được sử dụng tạo ra bóng phản chiếu của input.

a.Các thuộc tính :

kiểu
Tên thuộc tính
Giá trị mặc định
Miêu tả
Effect
input
null
input của Reflection
DoubleProperty
topOffset
0.0
khoảng cách giữa bottom của input và top của reflection
DoubleProperty
fraction
0.75
tỷ lệ input được hiển thị trong reflection. Giá trị fraction từ 0.0 -> 1.0. Ví dụ, giá trị fraction là 0.5, chỉ 1/2 bottom của input được hiển thị trong reflection
DoubleProperty
topOpacity
0.5
độ trong suốt dành cho các pixel ở những dòng cao nhất (top) trong reflection. Giá trị topOpacity từ 0.0 -> 1.0
DoubleProperty
bottomOpacity
0.0
độ trong suốt dành cho các pixel ở dòng thấp nhất (bottom) trong reflection. Giá trị bottomOpacity từ 0.0 -> 1.0
b. Cách sử dụng :
// Tạo đối tượng Reflection với giá trị thuộc tính mặc định
Reflection reflection = new Reflection();
// Tạo đối tượng Reflection với topOffset = 20, fraction = 0.5, topOpacity = 0.7, bottomOpacity = 0.2
Reflection reflection = new Reflection(20,0.5,0.7,0.2);
  c. Một số phương thức của Reflection:
kiểu trả về
Phương thức
Miêu tả
void
setInput(Effect  value)
Cung cấp input cho Reflection effect, nếu bạn không cung cấp input thì node sẽ là input.
Effect
getInput ()
Trả về input dành cho Reflection.
void
setTopOffset(double value)
Thiết lập giá trị thuộc tính topOffset.
double
getTopOffset()
Trả về giá trị thuộc tính topOffset.
void
setFraction(double value)
Thiết lập giá trị thuộc tính fraction.
double
getFraction()
Trả về giá trị thuộc tính fraction.
void
setTopOpacity(double value)
Thiết lập giá trị thuộc tính topOpacity.
double
getTopOpacity()
Trả về giá trị thuộc tính topOpacity.
void
setBottomOpacity(double value)
Thiết lập giá trị thuộc tính bottomOpacity.
double
getBottomOpacity()
Trả về giá trị thuộc tính bottomOpacity.

10. SepiaTone :

SepiaTone là hiệu ứng làm nổi bật tông màu nâu đỏ, tương tự những hình ảnh cũ bị bay màu.
ảnh gốc
ảnh áp dụng hiệu ứng SepiaTone

a.Các thuộc tính :

kiểu
Tên thuộc tính
Giá trị mặc định
Miêu tả
Effect
input
null
input của SepiaTone
DoubleProperty
level
1.0
điều khiển cường độ tông màu nâu đỏ. Giá trị từ 0.0 -> 1.0

b. Cách sử dụng :

// Tạo đối tượng SepiaTone với giá trị thuộc tính mặc định
SepiaTone sepiaTone = new SepiaTone();
// Tạo đối tượng SepiaTone với level = 0.5
SepiaTone sepiaTone = new SepiaTone(0.5);

  c. Một số phương thức của SepiaTone:

kiểu trả về
Phương thức
Miêu tả
void
setInput(Effect  value)
Cung cấp input cho SepiaTone effect, nếu bạn không cung cấp input thì node sẽ là input.
Effect
getInput ()
Trả về input dành cho SepiaTone.
void
setLevel(double value)
Thiết lập giá trị thuộc tính level
double
getLevel()
Trả về giá trị thuộc tính level.