gui more or less done

TODO: error handling, text scroll
This commit is contained in:
Patrice Matz 2018-07-31 11:01:20 +02:00
parent 0db36e9cf9
commit e78e8c3901
3 changed files with 226 additions and 31 deletions

View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.TextFlow?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<center>
<HBox fillHeight="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<ImageView id="image1" fitHeight="400.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true" />
<ImageView id="image2" fitHeight="400.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true" />
</children>
</HBox>
</center>
<bottom>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<VBox prefHeight="200.0" prefWidth="150.0">
<children>
<Button id="picture" mnemonicParsing="false" prefHeight="31.0" prefWidth="105.0" text="picture">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
</Button>
<Button id="replacements" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="31.0" prefWidth="105.0" text="replacement">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
</Button>
</children>
</VBox>
<VBox prefHeight="200.0" prefWidth="150.0">
<children>
<TextField id="rows" prefHeight="31.0" prefWidth="87.0" promptText="Rows">
<VBox.margin>
<Insets top="5.0" />
</VBox.margin>
</TextField>
<TextField id="cols" promptText="Columns">
<VBox.margin>
<Insets top="5.0" />
</VBox.margin>
</TextField>
</children>
</VBox>
<VBox prefHeight="200.0" prefWidth="200.0">
<children>
<ProgressBar id="progress" minHeight="-Infinity" minWidth="-Infinity" prefHeight="31.0" prefWidth="180.0" progress="0.0">
<VBox.margin>
<Insets top="5.0" />
</VBox.margin>
<padding>
<Insets left="20.0" />
</padding>
</ProgressBar>
<Button id="run" mnemonicParsing="false" prefHeight="32.0" prefWidth="160.0" text="Run replacement">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
</Button>
<Button id="save" mnemonicParsing="false" prefHeight="32.0" prefWidth="160.0" text="Save File">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
</Button>
</children>
</VBox>
<TextFlow id="log" prefHeight="200.0" prefWidth="200.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<HBox.margin>
<Insets left="85.0" />
</HBox.margin>
</TextFlow>
</children>
</HBox>
</bottom>
</BorderPane>

View File

@ -1,47 +1,152 @@
package com.company;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.ImageView;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.awt.*;
import java.util.ArrayList;
public class Main extends Application {
private ImageView image1;
private ImageView image2;
private Button picture;
private Button replacements;
private TextField row;
private TextField col;
private Button run;
private Button save;
private ProgressBar progress;
private TextFlow log;
private int rows = 150;
private int cols = 150;
private int chunkWidth;
private int chunkHeight;
private File file;
private ArrayList<String> repPaths;
private BufferedImage combined;
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.setTitle("MosaikJF");
primaryStage.setScene(new Scene(root));
primaryStage.show();
image1 = (javafx.scene.image.ImageView)primaryStage.getScene().lookup("#image1");
image2 = (javafx.scene.image.ImageView)primaryStage.getScene().lookup("#image2");
picture = (Button)primaryStage.getScene().lookup("#picture");
replacements = (Button)primaryStage.getScene().lookup("#replacements");
row = (TextField)primaryStage.getScene().lookup("#rows");
col = (TextField)primaryStage.getScene().lookup("#cols");
run = (Button)primaryStage.getScene().lookup("#run");
save = (Button)primaryStage.getScene().lookup("#save");
progress = (ProgressBar)primaryStage.getScene().lookup("#progress");
log = (TextFlow)primaryStage.getScene().lookup("#log");
hanleEvents(primaryStage);
}
public static void main(String[] args) throws IOException {
private void hanleEvents(Stage primaryStage) throws Exception{
picture.setOnAction(e -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Image");
file = fileChooser.showOpenDialog(primaryStage);
FileInputStream fis;
log.getChildren().add(new Text("loading image\n"));
try {
fis = new FileInputStream(file);
try {
BufferedImage image = ImageIO.read(fis);
image1.setImage(SwingFXUtils.toFXImage(image, null));
log.getChildren().add(new Text("loaded image\n"));
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
});
replacements.setOnAction(e -> {
DirectoryChooser fileChooser = new DirectoryChooser();
fileChooser.setTitle("Coose replacement images");
File tempfile = fileChooser.showDialog(primaryStage);
try {
repPaths = getAllImages(tempfile);
} catch (IOException e1) {
e1.printStackTrace();
}
});
run.setOnAction(e -> {
if(file == null || repPaths == null){
}
else{
try {
cols = Integer.parseInt(col.getText());
rows = Integer.parseInt(row.getText());
main();
image2.setImage(SwingFXUtils.toFXImage(combined, null));
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
save.setOnAction(e -> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Image");
File file = fileChooser.showSaveDialog(primaryStage);
try {
ImageIO.write(combined, "PNG", file);
} catch (IOException e1) {
e1.printStackTrace();
}
});
}
public void main() throws IOException {
File file = new File("C:\\Users\\Elliot\\Desktop\\test.jpg");
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file
ArrayList<String> repPaths = getAllImages(new File("C:\\Users\\Elliot\\Desktop\\test1\\"));
int rows = 150;
int cols = 150;
int chunkWidth = image.getWidth() / cols; // determines the chunk width and height
int chunkHeight = image.getHeight() / rows;
chunkWidth = image.getWidth() / cols; // determines the chunk width and height
chunkHeight = image.getHeight() / rows;
int count = 0;
int repCount = repPaths.size();
chunk[] chunks = new chunk[rows * cols];
chunk[] replacements = new chunk[repCount];
BufferedImage combined = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
combined = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = combined.getGraphics();
//split image into chunks
@ -59,7 +164,8 @@ public class Main extends Application {
chunks[count-1].average();
}
}
System.out.println("splitting done");
log.getChildren().add(new Text("splitting done \n"));
//System.out.println("splitting done");
//load replacement images into array
for (int i = 0; i < repCount; i++) {
// read file from path
@ -78,9 +184,12 @@ public class Main extends Application {
// fill array of chunks with read images
replacements[i] = new chunk(dimg);
System.out.println((i*100)/(repCount));
log.getChildren().add(new Text(Integer.toString((i*100)/(repCount)) + "\n"));
//System.out.println((i*100)/(repCount));
progress.setProgress((i*100)/(repCount));
}
System.out.println("images loaded");
log.getChildren().add(new Text("images loaded \n"));
//System.out.println("images loaded");
//for each chunk, calculate the euclidean distance to every possible replacement
for (int i = 0; i < cols*rows; i++) {
@ -99,8 +208,9 @@ public class Main extends Application {
g.drawImage(replacements[minEuclid].getImg(), ((i%cols)*chunkWidth), ((i/cols)*chunkHeight), null);
}
System.out.println("done");
ImageIO.write(combined, "PNG", new File("C:\\Users\\Elliot\\Desktop\\","combined.png"));
log.getChildren().add(new Text("replacement done \n"));
//System.out.println("done");
}

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import com.gluonhq.charm.glisten.control.ProgressBar?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.BorderPane?>
@ -10,13 +10,12 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.TextFlow?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<center>
<HBox fillHeight="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="800.0" BorderPane.alignment="CENTER">
<children>
<ImageView fitHeight="400.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fitHeight="400.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true" />
<ImageView id="image1" fitHeight="400.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true" />
<ImageView id="image2" fitHeight="400.0" fitWidth="400.0" pickOnBounds="true" preserveRatio="true" />
</children>
</HBox>
</center>
@ -25,12 +24,12 @@
<children>
<VBox prefHeight="200.0" prefWidth="150.0">
<children>
<Button mnemonicParsing="false" prefHeight="31.0" prefWidth="105.0" text="picture">
<Button id="picture" mnemonicParsing="false" prefHeight="31.0" prefWidth="105.0" text="picture">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
</Button>
<Button maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="31.0" prefWidth="105.0" text="replacement">
<Button id="replacements" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="31.0" prefWidth="105.0" text="replacement">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
@ -39,12 +38,12 @@
</VBox>
<VBox prefHeight="200.0" prefWidth="150.0">
<children>
<TextField prefHeight="31.0" prefWidth="87.0" promptText="Rows">
<TextField id="rows" prefHeight="31.0" prefWidth="87.0" promptText="Rows">
<VBox.margin>
<Insets top="5.0" />
</VBox.margin>
</TextField>
<TextField promptText="Columns">
<TextField id="cols" promptText="Columns">
<VBox.margin>
<Insets top="5.0" />
</VBox.margin>
@ -53,7 +52,7 @@
</VBox>
<VBox prefHeight="200.0" prefWidth="200.0">
<children>
<ProgressBar minHeight="-Infinity" minWidth="-Infinity" prefHeight="31.0" prefWidth="180.0" progress="0.0">
<ProgressBar id="progress" minHeight="-Infinity" minWidth="-Infinity" prefHeight="31.0" prefWidth="180.0" progress="0.0">
<VBox.margin>
<Insets top="5.0" />
</VBox.margin>
@ -61,19 +60,19 @@
<Insets left="20.0" />
</padding>
</ProgressBar>
<Button mnemonicParsing="false" prefHeight="32.0" prefWidth="160.0" text="Run replacement">
<Button id="run" mnemonicParsing="false" prefHeight="32.0" prefWidth="160.0" text="Run replacement">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
</Button>
<Button mnemonicParsing="false" prefHeight="32.0" prefWidth="160.0" text="Save File">
<Button id="save" mnemonicParsing="false" prefHeight="32.0" prefWidth="160.0" text="Save File">
<VBox.margin>
<Insets left="20.0" top="5.0" />
</VBox.margin>
</Button>
</children>
</VBox>
<TextFlow prefHeight="200.0" prefWidth="200.0">
<TextFlow id="log" prefHeight="200.0" prefWidth="200.0">
<opaqueInsets>
<Insets />
</opaqueInsets>