Created
July 10, 2014 18:46
-
-
Save mansueli/a86d8f7e38164c3912cd to your computer and use it in GitHub Desktop.
Erasing drawing on Image -> http://stackoverflow.com/questions/24681018/drawing-user-input-on-image-javafx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.event.EventHandler; | |
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.Pane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.paint.CycleMethod; | |
import javafx.scene.paint.LinearGradient; | |
import javafx.scene.paint.Stop; | |
import javafx.scene.shape.Rectangle; | |
import javafx.stage.Stage; | |
public class EraseImageonCanvas extends Application { | |
private Pane root = new Pane(); | |
private void setCanvas(Canvas canvas, Image img) { | |
GraphicsContext gc = canvas.getGraphicsContext2D(); | |
gc.drawImage(img, 0, 0,canvas.getWidth(), canvas.getHeight()); | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
primaryStage.setTitle("Erasing the Image"); | |
Rectangle rect = new Rectangle(400, 400); | |
drawBackground(rect); | |
root.getChildren().add(rect); | |
final Canvas canvas = new Canvas(200, 200); | |
canvas.setTranslateX(100); | |
canvas.setTranslateY(100); | |
//For local images use | |
//image = new Image(getClass().getResource(#Path#).openStream()); | |
final Image image = new Image( | |
"http://kyllo.com.br/wp-content/uploads/2013/12/Faroeste-Cabloco.jpg" | |
); | |
setCanvas(canvas,image); | |
final GraphicsContext gc = canvas.getGraphicsContext2D(); | |
// Clear away portions as the user drags the mouse | |
canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent e) { | |
gc.clearRect(e.getX() - 2, e.getY() - 2, 5, 5); | |
} | |
}); | |
// Reset the Canvas when the user double-clicks | |
canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { | |
@Override | |
public void handle(MouseEvent t) { | |
if (t.getClickCount() >1) { | |
setCanvas(canvas, image); | |
} | |
} | |
}); | |
// Add the Canvas to the Scene, and show the Stage | |
root.getChildren().add(canvas); | |
primaryStage.setScene(new Scene(root, 400, 400)); | |
primaryStage.show(); | |
} | |
//Draws the background with a RadialGradient | |
private void drawBackground(Rectangle rect) { | |
rect.setFill(new LinearGradient(0, 0, 1, 1, true, | |
CycleMethod.REFLECT, | |
new Stop(0, Color.RED), | |
new Stop(1, Color.YELLOW))); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment