Skip to content

Instantly share code, notes, and snippets.

@tarrsalah
Last active December 17, 2015 12:09
Show Gist options
  • Save tarrsalah/5608005 to your computer and use it in GitHub Desktop.
Save tarrsalah/5608005 to your computer and use it in GitHub Desktop.
Images and Snapshots 2 in 1.
/**
* Created with IntelliJ IDEA.
* User: tarrsalah
* Date: 5/19/13
* Time: 2:25 PM
* To change this template use File | Settings | File Templates.
*/
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.layout.BorderPaneBuilder;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineToBuilder;
import javafx.scene.shape.MoveToBuilder;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathBuilder;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
/**
* ListToImage.java (UTF-8)
*
* May 18, 2013
*
* @author tarrsalah.org
*/
public class PathDrawing extends Application {
private Group pathFirstGroup;
private Group pathSecondGroup;
private Button prinButton;
private BufferedImage bufferedImage;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
bufferedImage = new BufferedImage(200, 100, BufferedImage.TYPE_INT_ARGB);
final File file = new File("/home/tarrsalah/paths.png");
final Path pathOne;
final Path pathTwo;
pathOne = PathBuilder.create()
.stroke(Color.BLACK)
.strokeWidth(1)
.elements(MoveToBuilder.create()
.x(20)
.y(20)
.build(),
LineToBuilder.create()
.x(200)
.y(100)
.build()
)
.build();
pathTwo= PathBuilder.create()
.stroke(Color.RED)
.strokeWidth(1)
.elements(MoveToBuilder.create()
.x(20)
.y(100)
.build(),
LineToBuilder.create()
.x(200)
.y(20)
.build())
.build();
pathFirstGroup= new Group(pathOne);
pathSecondGroup= new Group(pathTwo);
prinButton = ButtonBuilder.create()
.text("print")
.onAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
writeThePicture(file);
}
})
.build();
Scene scene = SceneBuilder.create()
.root(
BorderPaneBuilder.create()
.center(
VBoxBuilder.create()
.children(pathFirstGroup,pathSecondGroup)
.spacing(20)
.build()
)
.bottom(prinButton)
.padding(new Insets(20))
.build())
.width(600)
.height(600)
.build();
stage.setScene(scene);
stage.show();
}
private void writeThePicture(File file) {
// the trick
BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);
SnapshotParameters snapshotParameter = new SnapshotParameters();
BufferedImage imageOne=SwingFXUtils.fromFXImage(pathFirstGroup.snapshot(snapshotParameter, null), null);
BufferedImage imageTwo=SwingFXUtils.fromFXImage(pathSecondGroup.snapshot(snapshotParameter, null), null);
Graphics2D graphics = (Graphics2D) image.getGraphics();
try {
graphics.setBackground(java.awt.Color.WHITE);
graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
graphics.drawRect(0,0,400,400);
graphics.drawImage(imageTwo, 0, 0, null);
graphics.drawImage(imageOne, 0, 0, null);
ImageIO.write(image, "png", file);
if (file.exists()) {
System.out.println(file.getAbsolutePath());
}
else {
System.out.println("No, there is no motherfucker image here !!");
}
} catch (IOException ex) {
Logger.getLogger(PathDrawing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@tarrsalah
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment