Sunday, May 5, 2013

Draw scaled image on JavaFX Canvas, with GraphicsContext.drawImage(image, x, y, w, h)

The method GraphicsContext.drawImage(image, x, y, w, h) draws an image into the given destination rectangle of the canvas. The Image is scaled to fit into the destination rectagnle. Alternatively, the method GraphicsContext.drawImage(image, x, y) draw unscaled image on canvas.

This example show how to draw scaled image on JavaFX canvas, on rectangle assigned by mouse pressed and released events.

Draw scaled image on JavaFX Canvas
Draw scaled image on JavaFX Canvas


package javafx_drawoncanvas;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_DrawOnCanvas extends Application {
    
    final static int CANVAS_WIDTH = 400;
    final static int CANVAS_HEIGHT = 400;

    double x0, y0, x1, y1;
    Image image;

    @Override
    public void start(final Stage primaryStage) {

        final Canvas canvas = new Canvas(CANVAS_WIDTH, CANVAS_HEIGHT);
        final GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
        initDraw(graphicsContext);
        
        canvas.addEventHandler(MouseEvent.MOUSE_PRESSED, 
                new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {
                
                x0 = event.getX();
                y0 = event.getY();

            }
        });
        
        canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, 
                new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {

            }
        });

        canvas.addEventHandler(MouseEvent.MOUSE_RELEASED, 
                new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent event) {
                
                x1 = event.getX();
                y1 = event.getY();
                
                double x = (x0 > x1) ? x1 : x0;
                double y = (y0 > y1) ? y1 : y0;
                double w = (x0 > x1) ? x0-x1 : x1-x0;
                double h = (y0 > y1) ? y0-y1 : y1-y0;
                
                graphicsContext.drawImage(image, x, y, w, h);
            }
        });

        Group root = new Group();
        VBox vBox = new VBox();
        vBox.getChildren().addAll(canvas);
        root.getChildren().add(vBox);
        Scene scene = new Scene(root, 400, 425);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
    private void initDraw(GraphicsContext gc){

        double canvasWidth = gc.getCanvas().getWidth();
        double canvasHeight = gc.getCanvas().getHeight();
        
        gc.setFill(Color.LIGHTGRAY);
        gc.setStroke(Color.BLACK);
        gc.setLineWidth(5);

        gc.fill();
        gc.strokeRect(
                0,              //x of the upper left corner
                0,              //y of the upper left corner
                canvasWidth,    //width of the rectangle
                canvasHeight);  //height of the rectangle

        gc.setLineWidth(1);
        
        image = new Image(getClass().getResourceAsStream("duke_44x80.png"));
    }
    
}


No comments:

Post a Comment