Thursday, January 3, 2013

Implement javafx.concurrent.Task to draw random pixel on WritableImage

Implement javafx.concurrent.Task to draw random pixel on WritableImage
Implement javafx.concurrent.Task to draw random pixel on WritableImage

package javafxpixel;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

 * @web
public class JavaFXPixel extends Application {
    final int TASK_MAX = 10000;
    Task task = new Task<Void>() {

        protected Void call() throws Exception {

            for (int i=1; i <= TASK_MAX; i++) {
                updateProgress(i, TASK_MAX);
                Platform.runLater(new Runnable(){

                    public void run() {
                try {
                } catch (InterruptedException interrupted) {
            return null;

    WritableImage myWritableImage;
    PixelWriter myPixelWriter;
    final int IMG_WIDTH = 255;
    final int IMG_HEIGHT = 255;
    ProgressBar progressBar;
    Button startButton;
    public void start(Stage primaryStage) {
        ImageView myImage = new ImageView();        
        progressBar = new ProgressBar();
        startButton = new Button("Start");
        VBox root = new VBox();
        root.getChildren().addAll(myImage, progressBar, startButton);
        Scene scene = new Scene(root, 300, 300);
        startButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                new Thread(task).start();

    public static void main(String[] args) {
    private void prepareMyWritableImage(){
        myWritableImage = new WritableImage(IMG_WIDTH, IMG_HEIGHT);
        myPixelWriter = myWritableImage.getPixelWriter();
        //fill with background blue
        for(int x = 0; x < IMG_WIDTH; x++){
            for(int y = 0; y < IMG_HEIGHT; y++){
                myPixelWriter.setColor(x, y, Color.GRAY);
    private void drawSomethingOnMyWritableImage(){
        int x = (int)(Math.random() * IMG_WIDTH);
        int y = (int)(Math.random() * IMG_HEIGHT);
        double r = Math.random();
        double g = Math.random();
        double b = Math.random();
        myPixelWriter.setColor(x, y, new Color(r, g, b, 1.0));


  1. I'm following your posts since months. You do a great job showing solutions to common problems. Well, there is something i'm struggeling with and it would be great if you could maybe show me how to solve the problem: For my JavaFX app, i want to add Hyperlinks, rotated by 90 or 270 degrees into a VBox so that it looks like the left and right dock areas of IntelliJ Idea. But i've got a lot of trouble to layout this components.