diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/Main.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/Main.java index 485af6f..0f24260 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/Main.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/Main.java @@ -7,8 +7,7 @@ import meerkat.protobuf.BallotQuestionUIElementOuterClass; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; -import java.awt.image.DataBufferByte; -import java.awt.image.WritableRaster; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -27,13 +26,15 @@ public class Main extends Application { * @throws IOException */ private byte[] GetData(String filepath) throws IOException { - File imgPath = new File(filepath); - BufferedImage bufferedImage = ImageIO.read(imgPath); + byte[] imageInByte; + BufferedImage originalImage = ImageIO.read(new File(filepath)); - WritableRaster raster = bufferedImage .getRaster(); - DataBufferByte data = (DataBufferByte) raster.getDataBuffer(); - - return ( data.getData() ); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ImageIO.write(originalImage, "png", baos); + baos.flush(); + imageInByte = baos.toByteArray(); + baos.close(); + return imageInByte; } /** @@ -65,9 +66,9 @@ public class Main extends Application { .setAnswers(BallotQuestionUIElementOuterClass.UIAnswers.newBuilder() .setAnswersType(2) .setAnswers(BallotQuestionUIElementOuterClass.ListOfAnswers.newBuilder() - .addAnswers(ByteString.copyFrom(GetData("F:\\vova\\meerkat\\meerkat-java\\voting-booth-gui\\src\\main\\resources\\images\\GeorgeBush.png"))) - .addAnswers(ByteString.copyFrom(GetData("F:\\vova\\meerkat\\meerkat-java\\voting-booth-gui\\src\\main\\resources\\images\\MichaelJackson.png"))) - .addAnswers(ByteString.copyFrom(GetData("F:\\vova\\meerkat\\meerkat-java\\voting-booth-gui\\src\\main\\resources\\images\\AntonioBanderass.jpg"))))) + .addAnswers(ByteString.copyFrom(GetData("/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/GeorgeBush.png"))) + .addAnswers(ByteString.copyFrom(GetData("/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/MichaelJackson.png"))) + .addAnswers(ByteString.copyFrom(GetData("/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/AntonioBanderass.png"))))) .setRandomizeListOrder(true).build(); return config; diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/select_candidate_by_picture/PicturesAnswersUpdater.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/select_candidate_by_picture/PicturesAnswersUpdater.java index c21853f..0bd17d3 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/select_candidate_by_picture/PicturesAnswersUpdater.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/select_candidate_by_picture/PicturesAnswersUpdater.java @@ -1,19 +1,115 @@ package meerkat.voting.gui.select_candidate_by_picture; +import com.google.protobuf.ByteString; +import javafx.embed.swing.SwingFXUtils; +import javafx.event.Event; +import javafx.event.EventHandler; +import javafx.geometry.Insets; +import javafx.scene.control.CheckBox; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.layout.GridPane; import javafx.stage.Stage; import meerkat.protobuf.BallotQuestionUIElementOuterClass; -/** - * Created by Vladimir Elieze Tokarev on 10/3/2016. - * This object updates the voisual representations of the voters answers - */ -class PicturesAnswersUpdater { - private Stage currentStage; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.IOException; - PicturesAnswersUpdater(Stage primaryStage){ + +/** + * Created by Vladimir Eliezer Tokarev on 10/3/2016. + * This object updates the visual representations of the voters binaryDatas + */ +class PicturesAnswersUpdater implements EventHandler{ + private Stage currentStage; + private int columIndex; + + PicturesAnswersUpdater(Stage primaryStage) { this.currentStage = primaryStage; + this.columIndex = 0; + // The lookup works only after the css have been randered + this.currentStage.getScene().getRoot().applyCss(); } - public void UpdateAnswers(BallotQuestionUIElementOuterClass.BallotQuestionUIElement question){ + /** + * Gets all the binaryDatas from the ballot ui question and puts them into the binaryDatas container + * + * @param question + */ + void UpdateAnswers(BallotQuestionUIElementOuterClass.BallotQuestionUIElement question) { + this.RemoveAllAnswers(); + for (ByteString bytesAnswer : question.getAnswers().getAnswers().getAnswersList()) { + try { + this.AddAnswer(bytesAnswer); + } catch (IOException e) { + e.printStackTrace(); + } + } + this.currentStage.show(); + } + + /** + * Gets the container of the binaryDatas + * + * @return GridPane object + */ + private GridPane GetAnswersContainer() { + return (GridPane) this.currentStage.getScene().lookup("#AnswersGridPane"); + } + + /** + * Remove all previous binaryDatas from the container + */ + private void RemoveAllAnswers() { + this.GetAnswersContainer().getChildren().removeAll(); + } + + /** + * Creates binaryData element which is grid pane with the binaryData and check box + * + * @param binaryData string binaryData to show to the voter + * @return GridPane which contains string and check box + */ + private GridPane GetAnswerElement(ByteString binaryData) throws IOException { + GridPane gridPane = new GridPane(); + + BufferedImage bufferedImage = ImageIO.read(binaryData.newInput()); + Image image = SwingFXUtils.toFXImage(bufferedImage, null); + + ImageView imageView = new ImageView(); + imageView.setImage(image); + + CheckBox checkBox = new CheckBox(); + checkBox.setOnAction(this); + + gridPane.add(imageView, 2 , 1); + gridPane.add(checkBox, 2, 2); + + gridPane.setPrefSize(100, 100); + gridPane.setPadding(new Insets(10)); + + return gridPane; + } + + /** + * Adds the GridPane binaryData to the panel + * + * @param binaryData the binaryData string to represent to the user + */ + private void AddAnswer(ByteString binaryData) throws IOException { + GridPane container = this.GetAnswersContainer(); + + GridPane newAnswer = this.GetAnswerElement(binaryData); + container.addColumn(1); + + this.columIndex++; + container.add(newAnswer, this.columIndex, 0); + this.currentStage.show(); + } + + @Override + public void handle(Event event) { + } } diff --git a/voting-booth-gui/src/main/resources/images/AntonioBanderass.jpg b/voting-booth-gui/src/main/resources/images/AntonioBanderass.jpg deleted file mode 100644 index 2a240ee..0000000 Binary files a/voting-booth-gui/src/main/resources/images/AntonioBanderass.jpg and /dev/null differ diff --git a/voting-booth-gui/src/main/resources/images/AntonioBanderass.png b/voting-booth-gui/src/main/resources/images/AntonioBanderass.png new file mode 100644 index 0000000..b0550de Binary files /dev/null and b/voting-booth-gui/src/main/resources/images/AntonioBanderass.png differ diff --git a/voting-booth-gui/src/main/resources/images/GeorgeBush.png b/voting-booth-gui/src/main/resources/images/GeorgeBush.png index 7209709..fcada38 100644 Binary files a/voting-booth-gui/src/main/resources/images/GeorgeBush.png and b/voting-booth-gui/src/main/resources/images/GeorgeBush.png differ diff --git a/voting-booth-gui/src/main/resources/images/MichaelJackson.png b/voting-booth-gui/src/main/resources/images/MichaelJackson.png index b652075..a514166 100644 Binary files a/voting-booth-gui/src/main/resources/images/MichaelJackson.png and b/voting-booth-gui/src/main/resources/images/MichaelJackson.png differ diff --git a/voting-booth-gui/src/main/resources/view/select_candidate_by_picture.fxml b/voting-booth-gui/src/main/resources/view/select_candidate_by_picture.fxml index f7389c4..6452ce0 100644 --- a/voting-booth-gui/src/main/resources/view/select_candidate_by_picture.fxml +++ b/voting-booth-gui/src/main/resources/view/select_candidate_by_picture.fxml @@ -123,9 +123,9 @@ - - - + + + @@ -135,73 +135,20 @@
- - - - - - - - - - - - -
- - - - - -
-
- -
- - - - - -
-
- -
- - - - - -
-
- - - - - - - - - - - - - - - -
-
+ + + + + + + + + + + + + +