Sunday, December 30, 2012

Take a snapshot of Node with JavaFX

Last post demonstrate how to "Take a snapshot of Scene", we can take a snapshot of any node also.

Example:
WritableImage snapImage = hBox_Image.snapshot(new SnapshotParameters(), null);


Take a snapshot of Node with JavaFX


package testjavafx;

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.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFX extends Application {
    
    PixelReader pixelReader;
    int width, height;
    
    WritableImage writableImage, writableImageHue, writableImageSaturation, writableImageBrightness;
    PixelWriter pixelWriter, pixelWriterHue, pixelWriterSaturation, pixelWriterBrightness;
    ImageView destImageView, imageViewHue, imageViewSaturation, imageViewBrightness;
    
    Slider sliderHue, sliderSaturation, sliderBrightness;
    double adjHue, adjSaturation, adjBrightness;
    
    HBox hBox_Image;
    
    @Override
    public void start(Stage primaryStage) {
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        
        pixelReader = image.getPixelReader();
        width = (int)image.getWidth();
        height = (int)image.getHeight();
        
        //Copy from source to destination pixel by pixel
        writableImage = new WritableImage(width, height);
        pixelWriter = writableImage.getPixelWriter();
        
        writableImageHue = new WritableImage(width, height);
        pixelWriterHue = writableImageHue.getPixelWriter();
        
        writableImageSaturation = new WritableImage(width, height);
        pixelWriterSaturation = writableImageSaturation.getPixelWriter();
        
        writableImageBrightness = new WritableImage(width, height);
        pixelWriterBrightness = writableImageBrightness.getPixelWriter();
        
        destImageView = new ImageView();
        
        imageViewHue = new ImageView();
        imageViewSaturation = new ImageView();
        imageViewBrightness = new ImageView();
        
        hBox_Image = new HBox();
        hBox_Image.getChildren().addAll(imageView, destImageView);
        
        //Control box for Hue
        sliderHue = SliderBuilder.create()
                .prefWidth(300)
                .min(-360)
                .max(360)
                .majorTickUnit(30)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderHue.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Hue = new HBox();
        hBox_Hue.getChildren().addAll(imageViewHue, sliderHue);
        
        //Control box for Saturation
        sliderSaturation = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderSaturation.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Saturation = new HBox();
        hBox_Saturation.getChildren().addAll(imageViewSaturation, sliderSaturation);
        
        //Control box for Blue
        sliderBrightness = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderBrightness.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Brightness = new HBox();
        hBox_Brightness.getChildren().addAll(imageViewBrightness, sliderBrightness);
        
        Button buttonSave = new Button("Snapshot");
        
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(hBox_Image, 
                hBox_Hue, hBox_Saturation, hBox_Brightness, buttonSave);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        final Scene scene = new Scene(root, 350, 350);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
        
        updateImage();
        
        buttonSave.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                //Get snapshot image
                WritableImage snapImage = hBox_Image.snapshot(new SnapshotParameters(), null);
                
                //display in new window
                ImageView snapView = new ImageView();
                snapView.setImage(snapImage);
                
                StackPane snapLayout = new StackPane();
                snapLayout.getChildren().add(snapView);
                
                Scene snapScene = new Scene(snapLayout, snapImage.getWidth(), snapImage.getHeight());

                Stage snapStage = new Stage();
                snapStage.setTitle("Snapshot");
                snapStage.setScene(snapScene);
 
                snapStage.show();
            }
        });
    }
    
    ChangeListener<Number> sliderChangeListener
            = new ChangeListener<Number>(){

        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            updateImage();
        }
    };
    
    private void updateImage(){
        adjHue = sliderHue.valueProperty().doubleValue();
        adjSaturation = sliderSaturation.valueProperty().doubleValue();
        adjBrightness = sliderBrightness.valueProperty().doubleValue();
        
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color color = pixelReader.getColor(x, y);
                pixelWriter.setColor(x, y, color);
                
                double hue = color.getHue() + adjHue;
                if(hue > 360.0){
                    hue = hue - 360;
                }else if(hue < 0.0){
                    hue = hue + 360.0;
                }
                
                double saturation = color.getSaturation() + adjSaturation;
                if(saturation > 1.0){
                    saturation = 1.0;
                }else if(saturation < 0.0){
                    saturation = 0.0;
                }
                
                double brightness = color.getBrightness() + adjBrightness;
                if(brightness > 1.0){
                    brightness = 1.0;
                }else if(brightness < 0.0){
                    brightness = 0.0;
                }
                
                double opacity = color.getOpacity();
                
                pixelWriterHue.setColor(x, y, new Color(hue/360.0, hue/360.0, hue/360.0, opacity));
                pixelWriterSaturation.setColor(x, y, new Color(saturation, saturation, saturation, opacity));
                pixelWriterBrightness.setColor(x, y, new Color(brightness, brightness, brightness, opacity));
                
                Color newColor = Color.hsb(hue, saturation, brightness, opacity);
                pixelWriter.setColor(x, y, newColor);

            }
        }
        
        imageViewHue.setImage(writableImageHue);
        imageViewSaturation.setImage(writableImageSaturation);
        imageViewBrightness.setImage(writableImageBrightness);
        destImageView.setImage(writableImage);
    }

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



Take a snapshot of Scene with JavaFX

JavaFX 2.2 introduce a snapshot() method that enables you to take a picture of scene; simple call scene.snapshot(null), a WritableImage will be returned.

Take a snapshot of Scene with JavaFX


package testjavafx;

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.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFX extends Application {
    
    PixelReader pixelReader;
    int width, height;
    
    WritableImage writableImage, writableImageHue, writableImageSaturation, writableImageBrightness;
    PixelWriter pixelWriter, pixelWriterHue, pixelWriterSaturation, pixelWriterBrightness;
    ImageView destImageView, imageViewHue, imageViewSaturation, imageViewBrightness;
    
    Slider sliderHue, sliderSaturation, sliderBrightness;
    double adjHue, adjSaturation, adjBrightness;
    
    @Override
    public void start(Stage primaryStage) {
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        
        pixelReader = image.getPixelReader();
        width = (int)image.getWidth();
        height = (int)image.getHeight();
        
        //Copy from source to destination pixel by pixel
        writableImage = new WritableImage(width, height);
        pixelWriter = writableImage.getPixelWriter();
        
        writableImageHue = new WritableImage(width, height);
        pixelWriterHue = writableImageHue.getPixelWriter();
        
        writableImageSaturation = new WritableImage(width, height);
        pixelWriterSaturation = writableImageSaturation.getPixelWriter();
        
        writableImageBrightness = new WritableImage(width, height);
        pixelWriterBrightness = writableImageBrightness.getPixelWriter();
        
        destImageView = new ImageView();
        imageViewHue = new ImageView();
        imageViewSaturation = new ImageView();
        imageViewBrightness = new ImageView();
        
        HBox hBox_Image = new HBox();
        hBox_Image.getChildren().addAll(imageView, destImageView);
        
        //Control box for Hue
        sliderHue = SliderBuilder.create()
                .prefWidth(300)
                .min(-360)
                .max(360)
                .majorTickUnit(30)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderHue.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Hue = new HBox();
        hBox_Hue.getChildren().addAll(imageViewHue, sliderHue);
        
        //Control box for Saturation
        sliderSaturation = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderSaturation.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Saturation = new HBox();
        hBox_Saturation.getChildren().addAll(imageViewSaturation, sliderSaturation);
        
        //Control box for Blue
        sliderBrightness = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderBrightness.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Brightness = new HBox();
        hBox_Brightness.getChildren().addAll(imageViewBrightness, sliderBrightness);
        
        Button buttonSave = new Button("Snapshot");
        
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(hBox_Image, 
                hBox_Hue, hBox_Saturation, hBox_Brightness, buttonSave);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        final Scene scene = new Scene(root, 350, 350);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
        
        updateImage();
        
        buttonSave.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                //Get snapshot image
                WritableImage snapImage = scene.snapshot(null);
                
                //display in new window
                ImageView snapView = new ImageView();
                snapView.setImage(snapImage);
                
                StackPane snapLayout = new StackPane();
                snapLayout.getChildren().add(snapView);
                
                Scene snapScene = new Scene(snapLayout, 350, 350);

                Stage snapStage = new Stage();
                snapStage.setTitle("Snapshot");
                snapStage.setScene(snapScene);
 
                snapStage.show();
            }
        });
    }
    
    ChangeListener<Number> sliderChangeListener
            = new ChangeListener<Number>(){

        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            updateImage();
        }
    };
    
    private void updateImage(){
        adjHue = sliderHue.valueProperty().doubleValue();
        adjSaturation = sliderSaturation.valueProperty().doubleValue();
        adjBrightness = sliderBrightness.valueProperty().doubleValue();
        
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color color = pixelReader.getColor(x, y);
                pixelWriter.setColor(x, y, color);
                
                double hue = color.getHue() + adjHue;
                if(hue > 360.0){
                    hue = hue - 360;
                }else if(hue < 0.0){
                    hue = hue + 360.0;
                }
                
                double saturation = color.getSaturation() + adjSaturation;
                if(saturation > 1.0){
                    saturation = 1.0;
                }else if(saturation < 0.0){
                    saturation = 0.0;
                }
                
                double brightness = color.getBrightness() + adjBrightness;
                if(brightness > 1.0){
                    brightness = 1.0;
                }else if(brightness < 0.0){
                    brightness = 0.0;
                }
                
                double opacity = color.getOpacity();
                
                pixelWriterHue.setColor(x, y, new Color(hue/360.0, hue/360.0, hue/360.0, opacity));
                pixelWriterSaturation.setColor(x, y, new Color(saturation, saturation, saturation, opacity));
                pixelWriterBrightness.setColor(x, y, new Color(brightness, brightness, brightness, opacity));
                
                Color newColor = Color.hsb(hue, saturation, brightness, opacity);
                pixelWriter.setColor(x, y, newColor);

            }
        }
        
        imageViewHue.setImage(writableImageHue);
        imageViewSaturation.setImage(writableImageSaturation);
        imageViewBrightness.setImage(writableImageBrightness);
        destImageView.setImage(writableImage);
    }

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


Related:
- Take a snapshot of Node with JavaFX

Saturday, December 29, 2012

Open new window in JavaFX 2

The example demonstrate how to open new window in JavaFX 2, once button clicked.

Open new window in JavaFX 2


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testjavafxwindow;

import javafx.application.Application;
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.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFXWindow extends Application {
    
    @Override
    public void start(final Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Open a New Window");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                
                Label secondLabel = new Label("Hello");
                
                StackPane secondaryLayout = new StackPane();
                secondaryLayout.getChildren().add(secondLabel);
                
                Scene secondScene = new Scene(secondaryLayout, 200, 100);

                Stage secondStage = new Stage();
                secondStage.setTitle("Second Stage");
                secondStage.setScene(secondScene);
                
                //Set position of second window, related to primary window.
                secondStage.setX(primaryStage.getX() + 250);
                secondStage.setY(primaryStage.getY() + 100);
 
                secondStage.show();
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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


Save WritableImage to file

To save WritableImage to file, e can use the code:


                    File file = new File("test.png");
                    RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
                    ImageIO.write(
                            renderedImage, 
                            "png",
                            file);


Where we render the image by calling SwingFXUtils.fromFXImage() and write to file by calling ImageIO.write().

Complete example:

Modify the code in last post "Create and adjust Color using hue, saturation, brightness". Add a button to save the image once button clicked.

package testjavafx;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFX extends Application {
    
    PixelReader pixelReader;
    int width, height;
    
    WritableImage writableImage, writableImageHue, writableImageSaturation, writableImageBrightness;
    PixelWriter pixelWriter, pixelWriterHue, pixelWriterSaturation, pixelWriterBrightness;
    ImageView destImageView, imageViewHue, imageViewSaturation, imageViewBrightness;
    
    Slider sliderHue, sliderSaturation, sliderBrightness;
    double adjHue, adjSaturation, adjBrightness;
    
    @Override
    public void start(Stage primaryStage) {
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        
        pixelReader = image.getPixelReader();
        width = (int)image.getWidth();
        height = (int)image.getHeight();
        
        //Copy from source to destination pixel by pixel
        writableImage = new WritableImage(width, height);
        pixelWriter = writableImage.getPixelWriter();
        
        writableImageHue = new WritableImage(width, height);
        pixelWriterHue = writableImageHue.getPixelWriter();
        
        writableImageSaturation = new WritableImage(width, height);
        pixelWriterSaturation = writableImageSaturation.getPixelWriter();
        
        writableImageBrightness = new WritableImage(width, height);
        pixelWriterBrightness = writableImageBrightness.getPixelWriter();
        
        destImageView = new ImageView();
        imageViewHue = new ImageView();
        imageViewSaturation = new ImageView();
        imageViewBrightness = new ImageView();
        
        HBox hBox_Image = new HBox();
        hBox_Image.getChildren().addAll(imageView, destImageView);
        
        //Control box for Hue
        sliderHue = SliderBuilder.create()
                .prefWidth(300)
                .min(-360)
                .max(360)
                .majorTickUnit(30)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderHue.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Hue = new HBox();
        hBox_Hue.getChildren().addAll(imageViewHue, sliderHue);
        
        //Control box for Saturation
        sliderSaturation = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderSaturation.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Saturation = new HBox();
        hBox_Saturation.getChildren().addAll(imageViewSaturation, sliderSaturation);
        
        //Control box for Blue
        sliderBrightness = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderBrightness.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Brightness = new HBox();
        hBox_Brightness.getChildren().addAll(imageViewBrightness, sliderBrightness);
        
        Button buttonSave = new Button("Save");
        buttonSave.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                try {
                    File file = new File("test.png");
                    RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
                    ImageIO.write(
                            renderedImage, 
                            "png",
                            file);
                } catch (IOException ex) {
                    Logger.getLogger(TestJavaFX.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(hBox_Image, 
                hBox_Hue, hBox_Saturation, hBox_Brightness, buttonSave);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        Scene scene = new Scene(root, 350, 400);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
        
        updateImage();
    }
    
    ChangeListener<Number> sliderChangeListener
            = new ChangeListener<Number>(){

        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            updateImage();
        }
    };
    
    private void updateImage(){
        adjHue = sliderHue.valueProperty().doubleValue();
        adjSaturation = sliderSaturation.valueProperty().doubleValue();
        adjBrightness = sliderBrightness.valueProperty().doubleValue();
        
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color color = pixelReader.getColor(x, y);
                pixelWriter.setColor(x, y, color);
                
                double hue = color.getHue() + adjHue;
                if(hue > 360.0){
                    hue = hue - 360;
                }else if(hue < 0.0){
                    hue = hue + 360.0;
                }
                
                double saturation = color.getSaturation() + adjSaturation;
                if(saturation > 1.0){
                    saturation = 1.0;
                }else if(saturation < 0.0){
                    saturation = 0.0;
                }
                
                double brightness = color.getBrightness() + adjBrightness;
                if(brightness > 1.0){
                    brightness = 1.0;
                }else if(brightness < 0.0){
                    brightness = 0.0;
                }
                
                double opacity = color.getOpacity();
                
                pixelWriterHue.setColor(x, y, new Color(hue/360.0, hue/360.0, hue/360.0, opacity));
                pixelWriterSaturation.setColor(x, y, new Color(saturation, saturation, saturation, opacity));
                pixelWriterBrightness.setColor(x, y, new Color(brightness, brightness, brightness, opacity));
                
                Color newColor = Color.hsb(hue, saturation, brightness, opacity);
                pixelWriter.setColor(x, y, newColor);

            }
        }
        
        imageViewHue.setImage(writableImageHue);
        imageViewSaturation.setImage(writableImageSaturation);
        imageViewBrightness.setImage(writableImageBrightness);
        destImageView.setImage(writableImage);
    }

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



Cannot load "platform/lib/nbexec.dll". The specified module could not be found.(126)

Recently, I have been prompted to update NetBeans IDE. After update and re-start, NetBeans cannot start, caused by Cannot load "platform/lib/nbexec.dll". The specified module could not be found.(126)!

Cannot load "platform/lib/nbexec.dll". The specified module could not be found.(126)


To solve it, re-start NetBeans IDE as administrator to complete update. Right click NetBeans icon, select Run as administrator.

Run as administrator


Public Updates for Java SE 6 will end at 19 February 2013

The last publicly available release of Oracle JDK 6 is to be released in February, 2013. This means that after 19 February 2013, all new security updates, patches and fixes for Java SE 6 and Java SE 5 will only be available through Oracle Support and will thus require a commercial license with Oracle.

Source and details: https://blogs.oracle.com/java/entry/end_of_public_updates_for

Thursday, December 27, 2012

Create and adjust Color using hue, saturation, brightness

To create Color using hue, saturation, brightness, we can call static method Color.hsb(double hue, double saturation, double brightness) or Color.hsb(double hue, double saturation, double brightness, double opacity). Such that we can adjust color of a image in hue, saturation, brightness.

Create and adjust Color using hue, saturation, brightness


package testjavafx;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFX extends Application {
    
    PixelReader pixelReader;
    int width, height;
    
    WritableImage writableImage, writableImageHue, writableImageSaturation, writableImageBrightness;
    PixelWriter pixelWriter, pixelWriterHue, pixelWriterSaturation, pixelWriterBrightness;
    ImageView destImageView, imageViewHue, imageViewSaturation, imageViewBrightness;
    
    Slider sliderHue, sliderSaturation, sliderBrightness;
    double adjHue, adjSaturation, adjBrightness;
    
    @Override
    public void start(Stage primaryStage) {
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        
        pixelReader = image.getPixelReader();
        width = (int)image.getWidth();
        height = (int)image.getHeight();
        
        //Copy from source to destination pixel by pixel
        writableImage = new WritableImage(width, height);
        pixelWriter = writableImage.getPixelWriter();
        
        writableImageHue = new WritableImage(width, height);
        pixelWriterHue = writableImageHue.getPixelWriter();
        
        writableImageSaturation = new WritableImage(width, height);
        pixelWriterSaturation = writableImageSaturation.getPixelWriter();
        
        writableImageBrightness = new WritableImage(width, height);
        pixelWriterBrightness = writableImageBrightness.getPixelWriter();
        
        destImageView = new ImageView();
        imageViewHue = new ImageView();
        imageViewSaturation = new ImageView();
        imageViewBrightness = new ImageView();
        
        HBox hBox_Image = new HBox();
        hBox_Image.getChildren().addAll(imageView, destImageView);
        
        //Control box for Hue
        sliderHue = SliderBuilder.create()
                .prefWidth(300)
                .min(-360)
                .max(360)
                .majorTickUnit(30)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderHue.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Hue = new HBox();
        hBox_Hue.getChildren().addAll(imageViewHue, sliderHue);
        
        //Control box for Saturation
        sliderSaturation = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderSaturation.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Saturation = new HBox();
        hBox_Saturation.getChildren().addAll(imageViewSaturation, sliderSaturation);
        
        //Control box for Blue
        sliderBrightness = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderBrightness.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Brightness = new HBox();
        hBox_Brightness.getChildren().addAll(imageViewBrightness, sliderBrightness);
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(hBox_Image, 
                hBox_Hue, hBox_Saturation, hBox_Brightness);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        Scene scene = new Scene(root, 350, 330);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
        
        updateImage();
    }
    
    ChangeListener<Number> sliderChangeListener
            = new ChangeListener<Number>(){

        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            updateImage();
        }
    };
    
    private void updateImage(){
        adjHue = sliderHue.valueProperty().doubleValue();
        adjSaturation = sliderSaturation.valueProperty().doubleValue();
        adjBrightness = sliderBrightness.valueProperty().doubleValue();
        
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color color = pixelReader.getColor(x, y);
                pixelWriter.setColor(x, y, color);
                
                double hue = color.getHue() + adjHue;
                if(hue > 360.0){
                    hue = hue - 360;
                }else if(hue < 0.0){
                    hue = hue + 360.0;
                }
                
                double saturation = color.getSaturation() + adjSaturation;
                if(saturation > 1.0){
                    saturation = 1.0;
                }else if(saturation < 0.0){
                    saturation = 0.0;
                }
                
                double brightness = color.getBrightness() + adjBrightness;
                if(brightness > 1.0){
                    brightness = 1.0;
                }else if(brightness < 0.0){
                    brightness = 0.0;
                }
                
                double opacity = color.getOpacity();
                
                pixelWriterHue.setColor(x, y, new Color(hue/360.0, hue/360.0, hue/360.0, opacity));
                pixelWriterSaturation.setColor(x, y, new Color(saturation, saturation, saturation, opacity));
                pixelWriterBrightness.setColor(x, y, new Color(brightness, brightness, brightness, opacity));
                
                Color newColor = Color.hsb(hue, saturation, brightness, opacity);
                pixelWriter.setColor(x, y, newColor);

            }
        }
        
        imageViewHue.setImage(writableImageHue);
        imageViewSaturation.setImage(writableImageSaturation);
        imageViewBrightness.setImage(writableImageBrightness);
        destImageView.setImage(writableImage);
    }

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




Related: Set effect on ImageView with ColorAdjust


Monday, December 24, 2012

Adjust color components of red, green and blue of image

Last article describe how to "Retrieve color components from image". Here it's demonstrated how to adjust color components of image.

Adjust color components of red, green and blue of image
Adjust color components of red, green and blue of image


package testjavafx;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFX extends Application {
    
    PixelReader pixelReader;
    int width, height;
    
    WritableImage writableImage, writableImageRed, writableImageGreen, writableImageBlue;
    PixelWriter pixelWriter, pixelWriterRed, pixelWriterGreen, pixelWriterBlue;
    ImageView destImageView, imageViewRed, imageViewGreen, imageViewBlue;
    
    Slider sliderRed, sliderGreen, sliderBlue;
    double adjRed, adjGreen, adjBlue;
    
    @Override
    public void start(Stage primaryStage) {
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        
        pixelReader = image.getPixelReader();
        width = (int)image.getWidth();
        height = (int)image.getHeight();
        
        //Copy from source to destination pixel by pixel
        writableImage = new WritableImage(width, height);
        pixelWriter = writableImage.getPixelWriter();
        
        writableImageRed = new WritableImage(width, height);
        pixelWriterRed = writableImageRed.getPixelWriter();
        
        writableImageGreen = new WritableImage(width, height);
        pixelWriterGreen = writableImageGreen.getPixelWriter();
        
        writableImageBlue = new WritableImage(width, height);
        pixelWriterBlue = writableImageBlue.getPixelWriter();
        
        destImageView = new ImageView();
        imageViewRed = new ImageView();
        imageViewGreen = new ImageView();
        imageViewBlue = new ImageView();
        
        HBox hBox_Image = new HBox();
        hBox_Image.getChildren().addAll(imageView, destImageView);
        
        //Control box for Red
        sliderRed = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderRed.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Red = new HBox();
        hBox_Red.getChildren().addAll(imageViewRed, sliderRed);
        
        //Control box for Green
        sliderGreen = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderGreen.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Green = new HBox();
        hBox_Green.getChildren().addAll(imageViewGreen, sliderGreen);
        
        //Control box for Blue
        sliderBlue = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderBlue.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Blue = new HBox();
        hBox_Blue.getChildren().addAll(imageViewBlue, sliderBlue);
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(hBox_Image, 
                hBox_Red, hBox_Green, hBox_Blue);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        Scene scene = new Scene(root, 350, 330);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
        
        updateImage();
    }
    
    ChangeListener<Number> sliderChangeListener
            = new ChangeListener<Number>(){

        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            updateImage();
        }
    };
    
    private void updateImage(){
        adjRed = sliderRed.valueProperty().doubleValue();
        adjGreen = sliderGreen.valueProperty().doubleValue();
        adjBlue = sliderBlue.valueProperty().doubleValue();
        
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color color = pixelReader.getColor(x, y);
                pixelWriter.setColor(x, y, color);
                
                double red = color.getRed() + adjRed;
                if(red > 1.0){
                    red = 1.0;
                }else if(red < 0.0){
                    red = 0.0;
                }
                
                double green = color.getGreen() + adjGreen;
                if(green > 1.0){
                    green = 1.0;
                }else if(green < 0.0){
                    green = 0.0;
                }
                
                double blue = color.getBlue() + adjBlue;
                if(blue > 1.0){
                    blue = 1.0;
                }else if(blue < 0.0){
                    blue = 0.0;
                }
                
                double opacity = color.getOpacity();
                
                pixelWriterRed.setColor(x, y, new Color(red, 0.0, 0.0, opacity));
                pixelWriterGreen.setColor(x, y, new Color(0.0, green, 0.0, opacity));
                pixelWriterBlue.setColor(x, y, new Color(0.0, 0.0, blue, opacity));
                pixelWriter.setColor(x, y, new Color(red, green, blue, opacity));

            }
        }
        
        imageViewRed.setImage(writableImageRed);
        imageViewGreen.setImage(writableImageGreen);
        imageViewBlue.setImage(writableImageBlue);
        destImageView.setImage(writableImage);
    }

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



Friday, December 21, 2012

Retrieve color components from image

Retrieve color components from image
Retrieve color components from image


To retrieve various components from a Color object, call it's methods getRed(), getGreen(), getBlue(), getBrightness(), getHue(), getSaturation() and getOpacity()...

package testjavafx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFX extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        
        PixelReader pixelReader = image.getPixelReader();
        
        int width = (int)image.getWidth();
        int height = (int)image.getHeight();
        
        //Copy from source to destination pixel by pixel
        WritableImage writableImage 
                = new WritableImage(width, height);
        PixelWriter pixelWriter = writableImage.getPixelWriter();
        
        WritableImage writableImageRed 
                = new WritableImage(width, height);
        PixelWriter pixelWriterRed = writableImageRed.getPixelWriter();
        
        WritableImage writableImageGreen 
                = new WritableImage(width, height);
        PixelWriter pixelWriterGreen = writableImageGreen.getPixelWriter();
        
        WritableImage writableImageBlue 
                = new WritableImage(width, height);
        PixelWriter pixelWriterBlue = writableImageBlue.getPixelWriter();
        
        WritableImage writableImageBrightness 
                = new WritableImage(width, height);
        PixelWriter pixelWriterBrightness = writableImageBrightness.getPixelWriter();
        
        WritableImage writableImageHue 
                = new WritableImage(width, height);
        PixelWriter pixelWriterHue = writableImageHue.getPixelWriter();
        
        WritableImage writableImageSaturation 
                = new WritableImage(width, height);
        PixelWriter pixelWriterSaturation = writableImageSaturation.getPixelWriter();
        
        WritableImage writableImageOpacity 
                = new WritableImage(width, height);
        PixelWriter pixelWriterOpacity = writableImageOpacity.getPixelWriter();
        
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color color = pixelReader.getColor(x, y);
                pixelWriter.setColor(x, y, color);
                
                double red = color.getRed();
                double green = color.getGreen();
                double blue = color.getBlue();
                double brightness = color.getBrightness();
                double hue = color.getHue()/360.0;  //getHue() return 0.0-360.0
                double saturation = color.getSaturation();
                double opacity = color.getOpacity();
                
                pixelWriterRed.setColor(x, y, new Color(red, 0.0, 0.0, 1.0));
                pixelWriterGreen.setColor(x, y, new Color(0.0, green, 0.0, 1.0));
                pixelWriterBlue.setColor(x, y, new Color(0.0, 0.0, blue, 1.0));
                
                pixelWriterBrightness.setColor(x, y, new Color(brightness, brightness, brightness, 1.0));
                pixelWriterHue.setColor(x, y, new Color(hue, hue, hue, 1.0));
                pixelWriterSaturation.setColor(x, y, new Color(saturation, saturation, saturation, 1.0));
                pixelWriterOpacity.setColor(x, y, new Color(opacity, opacity, opacity, 1.0));
            }
        }
        
        ImageView destImageView = new ImageView();
        destImageView.setImage(writableImage);
        ImageView imageViewRed = new ImageView();
        imageViewRed.setImage(writableImageRed);
        ImageView imageViewGreen = new ImageView();
        imageViewGreen.setImage(writableImageGreen);
        ImageView imageViewBlue = new ImageView();
        imageViewBlue.setImage(writableImageBlue);
        
        ImageView imageViewBrightness = new ImageView();
        imageViewBrightness.setImage(writableImageBrightness);
        ImageView imageViewHue = new ImageView();
        imageViewHue.setImage(writableImageHue);
        ImageView imageViewSaturation = new ImageView();
        imageViewSaturation.setImage(writableImageSaturation);
        ImageView imageViewOpacity = new ImageView();
        imageViewOpacity.setImage(writableImageOpacity);
        
        HBox hBox = new HBox();
        hBox.getChildren().addAll(imageView, destImageView, 
                imageViewRed, imageViewGreen, imageViewBlue,
                imageViewBrightness, imageViewHue, imageViewSaturation, imageViewOpacity);

        StackPane root = new StackPane();
        root.getChildren().add(hBox);
        Scene scene = new Scene(root, 400, 100);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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


Next:
- Adjust color components of red, green and blue of image

JavaFX example: Copy image pixel-by-pixel using PixelReader and PixelWriter

javafx.scene.image.PixelReader and javafx.scene.image.PixelWriter defines methods for retrieving the pixel data from an Image or other surface containing pixels, and writing the pixel data of a WritableImage or other surface containing writable pixels.

Example to copy image pixel-by-pixel using PixelReader and PixelWriter.

Copy image pixel-by-pixel using PixelReader and PixelWriter
Copy image pixel-by-pixel using PixelReader and PixelWriter


package testjavafx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFX extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        
        PixelReader pixelReader = image.getPixelReader();
        
        int width = (int)image.getWidth();
        int height = (int)image.getHeight();
        
        //Copy from source to destination pixel by pixel
        WritableImage writableImage 
                = new WritableImage(width, height);
        PixelWriter pixelWriter = writableImage.getPixelWriter();
        
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color color = pixelReader.getColor(x, y);
                pixelWriter.setColor(x, y, color);
            }
        }
        
        ImageView destImageView = new ImageView();
        destImageView.setImage(writableImage);
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(imageView, destImageView);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        Scene scene = new Scene(root, 300, 300);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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



Thursday, December 20, 2012

JavaFX example, get PixelReader for Image

Interface javafx.scene.image.PixelReader defines methods for retrieving the pixel data from an Image or other surface containing pixels. To get PixelReader, call getPixelReader() method of the associated Image object.

package testjavafx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFX extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        Text text = new Text();
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        
        PixelReader pixelReader = image.getPixelReader();
        text.setText("PixelFormat: " + pixelReader.getPixelFormat());
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(imageView, text);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        Scene scene = new Scene(root, 500, 300);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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


JavaFX example, get PixelReader for Image
JavaFX example, get PixelReader for Image


Load and display image from internet, on JavaFX ImageView

Load and display image from internet, on JavaFX ImageView
Load and display image from internet, on JavaFX ImageView

package testjavafx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class TestJavaFX extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);

        StackPane root = new StackPane();
        root.getChildren().add(imageView);
        Scene scene = new Scene(root, 300, 300);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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


Wednesday, December 5, 2012

IntelliJ IDEA - a intelligent IDE for Java and other technologies



IntelliJ IDEA is a intelligent IDE for Java and other technologies with an exceptional out-of-the-box feature set

Link: http://www.jetbrains.com/idea/

  • IntelliJ IDEA Ultimate is the full-featured commercial IDE — with a complete set of tools and integrations with the most important modern technologies and frameworks, such as Spring and Hibernate — a must-have for effective Web and Java EE development.
  • IntelliJ IDEA Community Edition is the open source version of IntelliJ IDEA, a premier IDE for Java, Groovy and other programming languages.

IntelliJ IDEA 12 is Available for Download, include ANDROID UI DESIGNER and JAVA 8 supported now. What's New in IntelliJ IDEA 12?

Saturday, December 1, 2012

Translate and scale with AffineTransform

Translate and scale with AffineTransform
Translate and scale with AffineTransform


package javaswing;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaTestSwing {
    
    static JFrameWin jFrameWindow;
    
    public static class MyComponent extends JComponent{

        @Override
        protected void paintComponent(Graphics g) {
            try {
                //prepare a original Image source
                Image image = ImageIO.read(this.getClass().getResource("duke.png"));

                int w = image.getWidth(null);
                int h = image.getHeight(null);
                g.drawImage(image, 0, 0, w, h, null);

                //translate and scale with AffineTransform
                AffineTransform affineTransform = new AffineTransform();
                affineTransform.translate(0, h);
                Double sx = 2.0;
                Double sy = 2.0;
                affineTransform.scale(sx, sy);
                ((Graphics2D)g).drawImage(image, affineTransform, null);
                
            } catch (IOException ex) {
                Logger.getLogger(JavaTestSwing.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
    
    public static class JFrameWin extends JFrame{
        public JFrameWin(){
            this.setTitle("java-buddy.blogspot.com");
            this.setSize(300, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
            MyComponent myComponent = new MyComponent();
            this.add(myComponent);
        }
    }

    public static void main(String[] args){
        Runnable doSwingLater = new Runnable(){
            
            @Override
            public void run() {
                jFrameWindow = new JFrameWin();
                jFrameWindow.setVisible(true);
            }
        };
        
        SwingUtilities.invokeLater(doSwingLater);
        
    }

}


Draw Image on translated and scaled Graphics

Draw Image on translated and scaled Graphics
Draw Image on translated and scaled Graphics


package javaswing;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaTestSwing {
    
    static JFrameWin jFrameWindow;
    
    public static class MyComponent extends JComponent{

        @Override
        protected void paintComponent(Graphics g) {
            try {
                //prepare a original Image source
                Image image = ImageIO.read(this.getClass().getResource("duke.png"));

                int w = image.getWidth(null);
                int h = image.getHeight(null);
                g.drawImage(image, 0, 0, w, h, null);

                //translate and scale
                g.translate(0, h);
                Double sx = 2.0;
                Double sy = 2.0;
                ((Graphics2D)g).scale(sx, sy);
                g.drawImage(image, 0, 0, null);
                
            } catch (IOException ex) {
                Logger.getLogger(JavaTestSwing.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
    
    public static class JFrameWin extends JFrame{
        public JFrameWin(){
            this.setTitle("java-buddy.blogspot.com");
            this.setSize(300, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
            MyComponent myComponent = new MyComponent();
            this.add(myComponent);
        }
    }

    public static void main(String[] args){
        Runnable doSwingLater = new Runnable(){
            
            @Override
            public void run() {
                jFrameWindow = new JFrameWin();
                jFrameWindow.setVisible(true);
            }
        };
        
        SwingUtilities.invokeLater(doSwingLater);
        
    }

}