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);
        
    }

}


Thursday, November 29, 2012

Draw partial scaled image using Graphics.drawImage()

Last post demonstrate a simple way to draw scaled image using Graphics.drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) method. We can also use drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) method to draw part of the image.

drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)

Where:
img - the specified image to be drawn. This method does nothing if img is null.
dx1 - the x coordinate of the first corner of the destination rectangle.
dy1 - the y coordinate of the first corner of the destination rectangle.
dx2 - the x coordinate of the second corner of the destination rectangle.
dy2 - the y coordinate of the second corner of the destination rectangle.
sx1 - the x coordinate of the first corner of the source rectangle.
sy1 - the y coordinate of the first corner of the source rectangle.
sx2 - the x coordinate of the second corner of the source rectangle.
sy2 - the y coordinate of the second corner of the source rectangle.
observer - object to be notified as more of the image is scaled and converted.

Draw partial scaled image using Graphics.drawImage()
Draw partial scaled image using Graphics.drawImage()


package javaswing;

import java.awt.Graphics;
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);

                g.drawImage(image,
                        0,          //dx1
                        h,          //dy1
                        w,          //dx2
                        h + h,      //dy2
                        0,          //sx1
                        0,          //sy1
                        w/2,        //sx2
                        h/2,        //sy2
                        null);      //ImageObserver
                
            } 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 scaled image using Graphics.drawImage()

Draw scaled image using Graphics.drawImage()


package javaswing;

import java.awt.Graphics;
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);
                
                int scale = 2;
                g.drawImage(image, 0, h, w*scale, h*scale, 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);
        
    }

}



We can also call drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) method to draw part of the image.

Wednesday, November 28, 2012

Example of using createCompatibleImage()

Example of using createCompatibleImage() method to create Compatible BufferedImage and Translucent Compatible BufferedImage.

Example of using createCompatibleImage()
Example of using createCompatibleImage()


package javaswing;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
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"));

                //Get current GraphicsConfiguration
                GraphicsConfiguration graphicsConfiguration 
                        = GraphicsEnvironment
                        .getLocalGraphicsEnvironment()
                        .getDefaultScreenDevice()
                        .getDefaultConfiguration();
                
                //Create a Compatible BufferedImage
                BufferedImage bufferedImage 
                        = graphicsConfiguration.createCompatibleImage(
                        image.getWidth(null), 
                        image.getHeight(null));
                //Copy from original Image to new Compatible BufferedImage
                Graphics tempGraphics = bufferedImage.getGraphics();
                tempGraphics.drawImage(image, 0, 0, null);
                tempGraphics.dispose();
                
                //Create a Compatible BufferedImage for translucent image
                BufferedImage bufferedImage_translucent
                        = graphicsConfiguration.createCompatibleImage(
                        image.getWidth(null), 
                        image.getHeight(null),
                        Transparency.TRANSLUCENT);
                //Copy from original Image to new Compatible BufferedImage
                Graphics tempGraphics_translucent 
                        = bufferedImage_translucent.getGraphics();
                tempGraphics_translucent.drawImage(image, 0, 0, null);
                tempGraphics_translucent.dispose();
                
                g.drawImage(bufferedImage, 0, 0, null);
                g.drawImage(bufferedImage_translucent, 0, bufferedImage.getHeight(), 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);
        
    }

}


Tuesday, November 27, 2012

Example of using Graphics.copyArea()

The method copyArea(int x, int y, int width, int height, int dx, int dy) copies an area of the component by a distance specified by dx and dy. From the point specified by x and y, this method copies downwards and to the right. To copy an area of the component to the left or upwards, specify a negative value for dx or dy. If a portion of the source rectangle lies outside the bounds of the component, or is obscured by another window or component, copyArea will be unable to copy the associated pixels. The area that is omitted can be refreshed by calling the component's paint method.

Parameters:
x - the x coordinate of the source rectangle.
y - the y coordinate of the source rectangle.
width - the width of the source rectangle.
height - the height of the source rectangle.
dx - the horizontal distance to copy the pixels.
dy - the vertical distance to copy the pixels.

Graphics.copyArea() example


package javaswing;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
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 {
                BufferedImage bufferedImage = ImageIO.read(this.getClass().getResource("duke.png"));
                g.drawImage(bufferedImage, 0, 0, null);
                
                int source_x = 0;
                int source_y = 0;
                int source_width = bufferedImage.getWidth();
                int source_height = bufferedImage.getHeight();
                int dx = bufferedImage.getWidth();
                int dy = 0;
                
                //copy horizontally
                for(int i = 0; i < 3; i++){
                    g.copyArea(
                        source_x,
                        source_y,
                        source_width,
                        source_height,
                        dx,
                        dy);
                    dx += source_width;
                }
                
                //copy vertically
                source_x = 0;
                source_y = 0;
                source_width = bufferedImage.getWidth() * 4;
                source_height = bufferedImage.getHeight();
                dx = 0;
                dy = bufferedImage.getHeight();
                g.copyArea(
                        source_x,
                        source_y,
                        source_width,
                        source_height,
                        dx,
                        dy);
                
            } 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);
        
    }

}


Friday, November 23, 2012

How to Get Started (FAST!) with JavaFX 2 and Scene Builder


Scene Builder

“How to Get Started (FAST!) with JavaFX 2 and Scene Builder.” Heckler, who has development experience in numerous environments, shows developers how to develop a JavaFX application using Scene Builder “in less time than it takes to drink a cup of coffee, while learning your way around in the process”.

Read it HERE.

Wednesday, November 14, 2012

Java 7 Concurrency Cookbook



Java remains the global standard for developing various applications and enterprise software, and the launch of Java 7 brings with it exciting new capabilities for concurrent programming by way of the concurrency utilities enhancement. This allows developers to make the most of their applications with parallel task performance. "Java 7 Concurrency Cookbook" covers all elements of the Java concurrency API, providing essential recipes for taking advantage of the exciting new capabilities.

On your computer, you can listen to music while you edit a Word document and read your emails, all at once! This is because your operating system allows the concurrency of tasks, much like the Java platform which offers various classes to execute concurrent tasks inside a Java program. "Java 7 Concurrency Cookbook" covers the most important features of the Java concurrency API, with special emphasis on the new capabilities of version 7.

With each version, Java increases the available functionality to facilitate development of concurrent programs. This book covers the most important and useful mechanisms included in version 7 of the Java concurrency API, so you will be able to use them directly in your applications.

"Java 7 Concurrency Cookbook" includes recipes to enable you to achieve everything from the basic management of threads and tasks, to the new Fork /Join framework, through synchronization mechanisms between tasks, different types of concurrent tasks that Java can execute, data structures that must be used in concurrent applications and the classes of the library that can be customized.

With the step-by-step examples in this book you’ll be able to apply the most important and useful features of the Java 7 concurrency API.

Approach
"Java 7 Concurrency Cookbook" is a practical Cookbook packed with real-world solutions. Intermediate–advanced level Java developers will learn from task-based recipes to use Java’s concurrent API to program thread safe solutions.

Who this book is for
If you are a Java developer who wants to take your knowledge of concurrent programming and multithreading further, as well as discover the new concurrency features of Java 7, then "Java 7 Concurrency Cookbook" is for you.

You should already be comfortable with general Java development practices and a basic grasp of threads would be an advantage.

Thursday, November 8, 2012

Draw Shape example: drawOval() and Ellipse2D.Double()

drawOval() and Ellipse2D.Double()


package javaswing;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
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) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            
            float x1 = width/4;
            float y1 = height/4;
            float x2 = width/4 + width/2;
            float y2 = height/4 + height/2;
            Color color1 = Color.RED;
            Color color2 = Color.BLUE;

            g.drawOval(0, 0, width/2, height/2);
            Ellipse2D.Double ellipse2D
                    = new Ellipse2D.Double(width/2, height/2, width/2, height/2);
            graphics2D.draw(ellipse2D);

        }
    }
    
    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);
        
    }

}


Sunday, November 4, 2012

Draw Shape example: GradientPaint

GradientPaint
GradientPaint

package javaswing;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
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) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            
            float x1 = width/4;
            float y1 = height/4;
            float x2 = width/4 + width/2;
            float y2 = height/4 + height/2;
            Color color1 = Color.RED;
            Color color2 = Color.BLUE;
            GradientPaint gradientPaint 
                    = new GradientPaint(x1, y1, color1, x2, y2, color2);
            
            graphics2D.setPaint(gradientPaint); 
            Rectangle2D.Double rectangle 
                    = new Rectangle2D.Double(width/4, height/4, width/2, height/2);
            graphics2D.fill(rectangle);
            
        }
    }
    
    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);
        
    }

}


Saturday, November 3, 2012

Draw Shape example: Ellipse2D and Rectangle2D

Ellipse2D and Rectangle2D
Ellipse2D and Rectangle2D


package javaswing;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
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) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            
            Ellipse2D.Double ellipse 
                    = new Ellipse2D.Double(0, 0, width, height);
            Rectangle2D.Double rectangle 
                    = new Rectangle2D.Double(width/4, height/4, width/2, height/2);
            
            graphics2D.draw(ellipse);
            graphics2D.fill(rectangle);
            
        }
    }
    
    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);
        
    }

}


Thursday, November 1, 2012

Draw Shape example

Draw Shape example


package javaswing;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
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) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            Shape shape = new java.awt.geom.Ellipse2D.Float(
                    0, 
                    0, 
                    width, 
                    height);
            
            graphics2D.draw(shape);
        }
    }
    
    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);
        
    }

}


Tuesday, October 23, 2012

Translate image using Graphics2D.translate()

Example to draw bufferedImage on translated Graphics2D:

draw bufferedImage on translated Graphics2D


package javaswing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Box;
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{
        
        BufferedImage bufferedImage = null;
        Dimension myDimension = new Dimension(50, 50);

        public MyComponent() {
            try {
                bufferedImage = ImageIO.read(this.getClass().getResource("duke.png"));
                int imageWidth = bufferedImage.getWidth();
                int imageHeight = bufferedImage.getHeight();
                myDimension = new Dimension(imageWidth * 2, imageHeight * 2);
            } catch (IOException ex) {
                Logger.getLogger(JavaTestSwing.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        

        @Override
        public Dimension getPreferredSize() {
            return myDimension;
        }

        @Override
        public Dimension getMaximumSize() {
            return myDimension;
        }

        @Override
        public Dimension getMinimumSize() {
            return myDimension;
        }

        @Override
        protected void paintComponent(Graphics g) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D;
            
            //2X
            graphics2D = (Graphics2D)g.create();
            graphics2D.drawImage(bufferedImage, 0, 0, null);
            
            //1X
            graphics2D = (Graphics2D)g.create();
            graphics2D.translate(30, 0);
            graphics2D.drawImage(bufferedImage, 0, 0, null);
            
            //0.5X
            graphics2D = (Graphics2D)g.create();
            graphics2D.translate(60, 0);
            graphics2D.drawImage(bufferedImage, 0, 0, null);
            
            //dispose Graphics2D object
            graphics2D.dispose();
        }
    }
    
    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();
             
            Box horizontalBox = Box.createHorizontalBox();

            horizontalBox.add(myComponent);
            this.add(horizontalBox);
        }
    }
    

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

}