如何在 JavaFX 中對文字新增多種效果組合?


可以使用setEffect()方法將某個效果新增到 JavaFX 中的任意節點物件。此方法接受Effect類的物件,並將其新增到當前節點。

混合效果是將兩個輸入混合在一起的。**javafx.scene.effect.Blend**類表示混合效果。若要將混合效果新增到文字節點 -

  • 透過將 x、y 座標(位置)和文字字串作為引數傳遞給建構函式,例項化 Text 類。

  • 設定字型、筆觸等所需屬性。

  • 例項化 Blend 類。

  • 使用 setMode() 方法設定混合模式。

  • 透過應用效果或者改變顏色建立兩個不同的輸入。

  • 使用 setBottomInput() 和 setTopInput() 方法將輸入設定為 blend。

  • 使用 setEffect() 方法將已建立的效果設定為文字節點。

  • 最後,將已建立的文字節點新增到組物件。

例項

import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Blend;
import javafx.scene.effect.BlendMode;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.InnerShadow;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class CombiningEffects extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Creating a text object
      String str = "Tutorialspoint";
      Text text = new Text(30.0, 100.0, str);
      //Setting the font
      Font font = Font.font("Brush Script MT", FontWeight.BOLD, 110);
      text.setFont(font);
      //Setting color of the text
      text.setFill(Color.BLUEVIOLET);
      //Creating the drop shadow effect
      DropShadow dropShadow = new DropShadow();
      dropShadow.setOffsetY(10);
      text.setEffect(dropShadow);
      //Creating the inner shadow effect
      InnerShadow innerShadow = new InnerShadow();
      innerShadow.setOffsetX(8.0);
      innerShadow.setOffsetY(8.0);
      //Creating the blend
      Blend blend = new Blend();
      blend.setMode(BlendMode.ADD);
      //Setting both the shadow effects to the blend
      blend.setBottomInput(dropShadow);
      blend.setBottomInput(innerShadow);
      //Setting the effect to the text
      text.setEffect(blend);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Blending Effects");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新於: 16-May-2020

489 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.