1

I did some research on how to color specific parts of an image using JavaFX, but I can't seem to find any information about this. I want to be able to color the different sections (each with a different color) of the following image in JavaFX:

image with different sections

Can it be done easily?

EDIT: This picture is much clearer in showing what I'm trying to do:

image with different sections

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
n00b1990
  • 1,189
  • 5
  • 17
  • 25
  • Short answer: yes it can be done easily. There are several ways to do this. Which is appropriate depends a lot on why you need this. – James_D May 30 '14 at 11:34
  • Well, I have an image that has a round object, and I want to be able to color this image between the line boundaries with something like `image.setFill(Color.RED, region3)` – n00b1990 May 30 '14 at 13:11

1 Answers1

2

You can use Group and Rectangles with different colors and positions for this.

Here is example:

enter image description here

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();

        Rectangle rect1 = new Rectangle(400,100);
        Rectangle rect2 = new Rectangle(400,125);
        Rectangle rect3 = new Rectangle(400,175);
        Rectangle rect4 = new Rectangle(200,250);

        rect1.setFill(Color.BLUE);
        rect2.setFill(Color.RED);
        rect3.setFill(Color.YELLOW);
        rect4.setFill(Color.GREEN);

        rect2.setLayoutY(100);
        rect3.setLayoutY(225);
        rect4.setLayoutX(200);
        rect4.setLayoutY(150);

        root.getChildren().addAll(rect1,rect2,rect3,rect4);


        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }
}
Ostap Maliuvanchuk
  • 1,125
  • 2
  • 12
  • 32
  • 2
    Could you provide an example of how to do this? If not, your answer should be provided as a comment instead of a full answer. – Charles Caldwell May 30 '14 at 11:43
  • Can you also do the same thing with a given image? So without creating the image in the code? And what about a circle divided in three regions? I am searching for a way in which to automatically detect the lines in a picture and setting these lines as region boundaries. – n00b1990 May 30 '14 at 13:06
  • If you want just edit image, you don't need javafx for that. – Ostap Maliuvanchuk May 30 '14 at 13:12
  • I have an applicaton made in JavaFX, and this image is loaded (see my question, I made some edits). Depending on some values, I want the application to independently change the colors in the regions. – n00b1990 May 30 '14 at 13:23
  • Yes, but you could still use Java to do that. JavaFX is just way to show image. – Ostap Maliuvanchuk May 30 '14 at 13:33
  • Ok, so how would you do such a thing? Can the coloring be done real time in a JavaFX GUI without using JavaFX code to color a region? The behavior of the image is supposed to be handled by a controller, and the controller will probably contain a reference to the image. – n00b1990 May 30 '14 at 13:41
  • @user979349 Could you tell me how to do this? Thank you for your help? – n00b1990 Jun 01 '14 at 17:47
  • Here is two options to edit image: 1. get image object from imageView and change it with PixelWriter and PixelReader. Look here for more information http://docs.oracle.com/javafx/2/image_ops/jfxpub-image_ops.htm 2. Convert javafx image object to BufferedImage using http://docs.oracle.com/javafx/2/api/javafx/embed/swing/SwingFXUtils.html, then edit image and convert it back to javafx image. – Ostap Maliuvanchuk Jun 04 '14 at 19:10