We have tried to create an JavaFX Application to help our readers.This time we are playing with Images. Browse any image and apply lots of effects like Lights, Inner Outer shadows, Spacia, Zoom, Ppacity, Blur, Reflection etc.This post covers example with source code to create Image Editor.

This JavaFX Application is focused for those readers who have primary knowledge of JavaFX.

For different effects on image,libraries are imported.Objects for different effects are created like Reflection,Box Blur,Lighting,Inner Shadow,Drop Shadow.Once image has been browsed,any effect can be applied on that image with help of effect objects.Respective Effects are set on Action event of respective button.Apart from simple buttons,Sliders and events of sliders are also used to set respective effect on Image.

Here is output of the program:

Source code to create above program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package puretechy.javafxexamples;
 
import java.io.File;
 
import application.Main;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.effect.BoxBlur;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.InnerShadow;
import javafx.scene.effect.Light.Distant;
import javafx.scene.effect.Lighting;
import javafx.scene.effect.Reflection;
import javafx.scene.effect.SepiaTone;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
 
public class ImageEditorUsingFX  extends Application
{
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage primaryStage)
    {
 
       Image image = new Image(Main.class.getResourceAsStream(""));
       ImageView iv1 = new ImageView();
        iv1.setImage(image);
 
        primaryStage.setTitle("Image Editor Using JavaFX");
 
        Reflection r = new Reflection();
        r.setFraction(1);
 
        BoxBlur bb = new BoxBlur();
        bb.setWidth(5);
        bb.setHeight(5);
        bb.setIterations(3);
 
        Distant light = new Distant();
        light.setAzimuth(-135.0f);
        Lighting l = new Lighting();
        l.setLight(light);
        l.setSurfaceScale(5.0f);
 
        DropShadow ds = new DropShadow();
        ds.setOffsetY(30.0);
        ds.setOffsetX(3.0);
        ds.setColor(Color.BLACK);
 
        InnerShadow is = new InnerShadow();
        is.setOffsetX(20.0);
        is.setOffsetY(2.0);
        is.setColor(Color.RED);
 
 
        Button btnChooseImage = new Button();
        btnChooseImage.setText("Choose Image");
 
        btnChooseImage.setOnAction(new EventHandler()
                     {
                     @Override
                     public void handle(ActionEvent event)
                     {
                            FileChooser fileChooser = new FileChooser();
                         // FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
 
                         // fileChooser.getExtensionFilters().add(extFilter);
 
                          File file = fileChooser.showOpenDialog(primaryStage);
 
                         Image ima = new Image(file.toURI().toString());
                         iv1.setImage(ima);
 
                     }
                     }
        );
 
 
        Button btnLightEffect = new Button();
        btnLightEffect.setText("Light Effect");
 
        btnLightEffect.setOnAction(new EventHandler()
                     {
                     @Override
                     public void handle(ActionEvent event)
                     {
                           iv1.setEffect(l);
 
                     }
                     }
        );
 
 
        Button btnOuterShadow = new Button();
        btnOuterShadow.setText("Drop Shadow");
 
        btnOuterShadow.setOnAction(new EventHandler()
                     {
                     @Override
                     public void handle(ActionEvent event)
                     {
 
                           iv1.setEffect(ds);
 
                     }
                     }
        );
 
        Button btnInnerShadow = new Button();
        btnInnerShadow.setText("Inner Shadow");
 
        btnInnerShadow.setOnAction(new EventHandler()
                     {
                     @Override
                     public void handle(ActionEvent event)
                     {
 
                           iv1.setEffect(is);
                     }
                     }
        );
 
        Button btnBlur = new Button();
        btnBlur.setText("Blur");
 
        btnBlur.setOnAction(new EventHandler()
                     {
                     @Override
                     public void handle(ActionEvent event)
                     {
                           iv1.setEffect(bb);
                     }
                     }
        );
 
        Button btnReflection = new Button();
        btnReflection.setText("Reflection");
 
        btnReflection.setOnAction(new EventHandler()
                     {
                     @Override
                     public void handle(ActionEvent event)
                     {
                           iv1.setEffect(r);
                     }
                     }
        );
 
 
        VBox root = new VBox(5);
 
        HBox hbEffects=new HBox(5);
        HBox hbEffectsSlider=new HBox(5);
        HBox hbImage=new HBox();
 
        hbEffects.getChildren().add(btnChooseImage);
        hbEffects.getChildren().add(btnLightEffect);
        hbEffects.getChildren().add(btnOuterShadow);
        hbEffects.getChildren().add(btnInnerShadow);
        hbEffects.getChildren().add(btnBlur);
        hbEffects.getChildren().add(btnReflection);
        hbImage.getChildren().add(iv1);
 
        final Label opacityCaption = new Label("Opacity Level:");
 
        final Slider opacityLevel = new Slider(0, 1, 1);
        opacityLevel.valueProperty().addListener(new ChangeListener() {
            public void changed(ObservableValue<? extends Number> ov,
                Number old_val, Number new_val) {
                    iv1.setOpacity(new_val.doubleValue());
 
            }
        });
 
        final Label sepiaCaption = new Label("Sepia Tone:");
        final Slider sepiaTone = new Slider(0, 1, 1);
        final SepiaTone sepiaEffect = new SepiaTone();
 
        sepiaTone.valueProperty().addListener(new ChangeListener() {
            public void changed(ObservableValue<? extends Number> ov,
                Number old_val, Number new_val) {
                    sepiaEffect.setLevel(new_val.doubleValue());
 
                    iv1.setEffect(sepiaEffect);
            }
        });
 
        final Label scalingCaption = new Label("Zoom :");
        final Slider scaling = new Slider (0.5, 1, 1);
 
        scaling.valueProperty().addListener(new ChangeListener() {
            public void changed(ObservableValue<? extends Number> ov,
                Number old_val, Number new_val) {
              iv1.setScaleX(new_val.doubleValue());
              iv1.setScaleY(new_val.doubleValue());
 
            }
        });
 
 
        hbEffectsSlider.getChildren().add(opacityCaption);
        hbEffectsSlider.getChildren().add(opacityLevel);
        hbEffectsSlider.getChildren().add(sepiaCaption);
        hbEffectsSlider.getChildren().add(sepiaTone);
        hbEffectsSlider.getChildren().add(scalingCaption);
        hbEffectsSlider.getChildren().add(scaling);
 
        root.getChildren().add(hbEffects);
        root.getChildren().add(hbEffectsSlider);
        root.getChildren().add(hbImage);
 
        primaryStage.setScene(new Scene(root, 800, 500,Color.BLANCHEDALMOND));
        primaryStage.show();
    }
}
} 

We hope this JavaFX application helps you !

Let us know your views for Image Editor in comments section.

You can also Check Out other JavaFX Example of Cubic Curve.Sine wave is created with Cubic Curve.

Also checkout Free Udemy Coupon Codes and Save $$ !

About

3 thoughts on “Creating JavaFX application ? Check out Image Editor

  1. II says:

    This Code is not Running?

    1. Chandani says:

      Please let us know what error you are getting

  2. David Parker says:

    Help i m getting these error
    here is the picture
    http://imgur.com/a/j00V1

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website

%d bloggers like this: