An ImageInput can be used to create an image as the input.
The button has the ImageInput effect applied. Usually you will use css to add a background image. If the image is clicked, the text changes color.
package ex57;
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.ImageInput;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Ex57 extends Application {
    @Override
    public void start(Stage stage) {
        Group root = new Group();
        
        String hillary = "Hillary";
        String clinton = "Clinton";
        
        Image image = new Image(hillary + clinton + ".png");
        ImageInput imageInput = new ImageInput(image);
        Button button = new Button();
        button.setEffect(imageInput);
        
        Label label = new Label(hillary + " " + clinton);
        label.setLayoutX(400); label.setLayoutY(100);
        label.setFont(Font.font("Georgia", 32));
        
        button.setOnAction(e-> {
            Random rand = new Random();
            int r = rand.nextInt(256); // 0 to 255
            int g = rand.nextInt(256); // 0 to 255
            int b = rand.nextInt(256); // 0 to 255
            label.setTextFill(Color.rgb(r, g, b));
        });
        
        root.getChildren().addAll(button, label);
        Scene scene = new Scene(root, 650, 500, Color.YELLOWGREEN);
        stage.setTitle("Example 57. ImageInput");
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        Application.launch(args);
    }
}
This is the output:
 
No comments:
Post a Comment