├── .gitignore
├── images
└── temp.txt
├── src
├── META-INF
│ └── MANIFEST.MF
└── sample
│ ├── Controller.java
│ ├── sample.fxml
│ ├── Browser.java
│ └── Main.java
├── Capture.iml
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 |
--------------------------------------------------------------------------------
/images/temp.txt:
--------------------------------------------------------------------------------
1 | Temp file because GitHub is stupid.
--------------------------------------------------------------------------------
/src/META-INF/MANIFEST.MF:
--------------------------------------------------------------------------------
1 | Manifest-Version: 1.0
2 | Main-Class: sample.Main
3 |
4 |
--------------------------------------------------------------------------------
/src/sample/Controller.java:
--------------------------------------------------------------------------------
1 | package sample;
2 |
3 | public class Controller {
4 | }
5 |
--------------------------------------------------------------------------------
/src/sample/sample.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
8 |
--------------------------------------------------------------------------------
/Capture.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/sample/Browser.java:
--------------------------------------------------------------------------------
1 | package sample;
2 | import javafx.geometry.HPos;
3 | import javafx.geometry.VPos;
4 | import javafx.scene.layout.Region;
5 | import javafx.scene.web.WebEngine;
6 | import javafx.scene.web.WebView;
7 |
8 | class Browser extends Region {
9 |
10 | private final WebView browser = new WebView();
11 |
12 | Browser(String url) {
13 | WebEngine webEngine = browser.getEngine();
14 | webEngine.load(url);
15 | getChildren().add(browser);
16 | }
17 |
18 | Boolean isPageLoaded(){
19 | return browser.getEngine().getDocument() != null;
20 | }
21 |
22 | @Override
23 | protected void layoutChildren() {
24 | double w = getWidth();
25 | double h = getHeight();
26 | layoutInArea(browser, 0, 0, w, h, 0, HPos.CENTER, VPos.CENTER);
27 | }
28 |
29 | @Override
30 | protected double computePrefWidth(double width) {
31 | return 1280;
32 | }
33 |
34 | @Override
35 | protected double computePrefHeight(double height) {
36 | return 720;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | # Overview
4 |
5 | Webpage Screenshot is a JavaFX program that lets you to enter the URL of a webpage and it will take a screenshot and
6 | save it as a .png image.
7 |
8 | ## To build a .jar file
9 |
10 | Follow these steps to build the .jar file in Intellij.
11 |
12 | * File > Save All.
13 | * Run driver or class with main method.
14 | * File > Project Structure.
15 | * Select Tab "Artifacts".
16 | * Click green plus button near top of window.
17 | * Select JAR from Add drop down menu. Select "From modules with dependencies"
18 | * Select main class.
19 | * The radio button should be selecting "extract to the target JAR." Press OK.
20 | * Check the box "Build on make"
21 | * Press apply and OK.
22 | * From the main menu, select the build dropdown.
23 | * Select the option build artifacts.
24 |
25 | ## Usage
26 |
27 | To use from the command line, pass in the file name (for the output image) and URL as the first and second argument
28 |
29 | ```
30 | java -jar Capture.jar homepage https://thenewboston.com/
31 | ```
32 |
33 | Also, you must have a folder called "images" in the same directory where the Capture.jar file is saved. This is where
34 | the final images will be saved.
--------------------------------------------------------------------------------
/src/sample/Main.java:
--------------------------------------------------------------------------------
1 | package sample;
2 | import javafx.application.Application;
3 | import javafx.application.Platform;
4 | import javafx.embed.swing.SwingFXUtils;
5 | import javafx.fxml.FXML;
6 | import javafx.scene.Scene;
7 | import javafx.scene.SnapshotParameters;
8 | import javafx.scene.image.WritableImage;
9 | import javafx.scene.layout.VBox;
10 | import javafx.stage.Stage;
11 | import javax.imageio.ImageIO;
12 | import java.io.File;
13 | import java.io.IOException;
14 | import java.util.Timer;
15 | import java.util.TimerTask;
16 |
17 |
18 | public class Main extends Application {
19 |
20 | private static String imageName, url;
21 | private Browser browser;
22 | private Timer timer = new java.util.Timer();
23 |
24 | public static void main(String[] args){
25 | imageName = args[0];
26 | url = args[1];
27 | System.setProperty("jsse.enableSNIExtension", "false");
28 | System.out.println("Creating screenshot for " + url);
29 | launch(args);
30 | }
31 |
32 | @Override
33 | public void start(Stage window) {
34 | window.setTitle(url);
35 |
36 | browser = new Browser(url);
37 | monitorPageStatus();
38 |
39 | VBox layout = new VBox();
40 | layout.getChildren().addAll(browser);
41 | Scene scene = new Scene(layout);
42 | window.setScene(scene);
43 | window.setOnCloseRequest(we -> System.exit(0));
44 | window.show();
45 | }
46 |
47 | private void monitorPageStatus(){
48 | timer.schedule(new TimerTask() {
49 | public void run() {
50 | Platform.runLater(() -> {
51 | if(browser.isPageLoaded()){
52 | System.out.println("Page now loaded, taking screenshot...");
53 | saveAsPng();
54 | cancel();
55 | }
56 | else
57 | System.out.println("Loading page...");
58 | });
59 | }
60 | }, 1000, 1000);
61 | }
62 |
63 | @FXML
64 | private void saveAsPng() {
65 | timer.schedule(new TimerTask() {
66 | public void run() {
67 | Platform.runLater(() -> {
68 | WritableImage image = browser.snapshot(new SnapshotParameters(), null);
69 | File file = new File("images/" + imageName + ".png");
70 | try {
71 | ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
72 | System.out.println("Screenshot saved as " + imageName + ".png");
73 | System.exit(0);
74 | } catch (IOException e) {
75 | e.printStackTrace();
76 | System.exit(0);
77 | }
78 | cancel();
79 | });
80 | }
81 | }, 5000);
82 | }
83 |
84 | }
85 |
--------------------------------------------------------------------------------