Added the ability to display pictures
parent
91b0d947f2
commit
21867fb5da
|
@ -7,8 +7,7 @@ import meerkat.protobuf.BallotQuestionUIElementOuterClass;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.DataBufferByte;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.awt.image.WritableRaster;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@ -27,13 +26,15 @@ public class Main extends Application {
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private byte[] GetData(String filepath) throws IOException {
|
private byte[] GetData(String filepath) throws IOException {
|
||||||
File imgPath = new File(filepath);
|
byte[] imageInByte;
|
||||||
BufferedImage bufferedImage = ImageIO.read(imgPath);
|
BufferedImage originalImage = ImageIO.read(new File(filepath));
|
||||||
|
|
||||||
WritableRaster raster = bufferedImage .getRaster();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
|
ImageIO.write(originalImage, "png", baos);
|
||||||
|
baos.flush();
|
||||||
return ( data.getData() );
|
imageInByte = baos.toByteArray();
|
||||||
|
baos.close();
|
||||||
|
return imageInByte;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,9 +66,9 @@ public class Main extends Application {
|
||||||
.setAnswers(BallotQuestionUIElementOuterClass.UIAnswers.newBuilder()
|
.setAnswers(BallotQuestionUIElementOuterClass.UIAnswers.newBuilder()
|
||||||
.setAnswersType(2)
|
.setAnswersType(2)
|
||||||
.setAnswers(BallotQuestionUIElementOuterClass.ListOfAnswers.newBuilder()
|
.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/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/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/AntonioBanderass.png")))))
|
||||||
.setRandomizeListOrder(true).build();
|
.setRandomizeListOrder(true).build();
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
|
|
|
@ -1,19 +1,115 @@
|
||||||
package meerkat.voting.gui.select_candidate_by_picture;
|
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 javafx.stage.Stage;
|
||||||
import meerkat.protobuf.BallotQuestionUIElementOuterClass;
|
import meerkat.protobuf.BallotQuestionUIElementOuterClass;
|
||||||
|
|
||||||
/**
|
import javax.imageio.ImageIO;
|
||||||
* Created by Vladimir Elieze Tokarev on 10/3/2016.
|
import java.awt.image.BufferedImage;
|
||||||
* This object updates the voisual representations of the voters answers
|
import java.io.IOException;
|
||||||
*/
|
|
||||||
class PicturesAnswersUpdater {
|
|
||||||
private Stage currentStage;
|
|
||||||
|
|
||||||
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.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) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 2.9 KiB |
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
Binary file not shown.
Before Width: | Height: | Size: 7.0 KiB After Width: | Height: | Size: 46 KiB |
Binary file not shown.
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 19 KiB |
|
@ -123,9 +123,9 @@
|
||||||
</GridPane>
|
</GridPane>
|
||||||
<GridPane GridPane.rowIndex="1">
|
<GridPane GridPane.rowIndex="1">
|
||||||
<columnConstraints>
|
<columnConstraints>
|
||||||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="144.0" minWidth="10.0" prefWidth="60.0" />
|
<ColumnConstraints hgrow="SOMETIMES" maxWidth="144.0" minWidth="10.0" prefWidth="25.0" />
|
||||||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="469.0" minWidth="10.0" prefWidth="469.0" />
|
<ColumnConstraints hgrow="SOMETIMES" maxWidth="552.0" minWidth="10.0" prefWidth="548.0" />
|
||||||
<ColumnConstraints hgrow="SOMETIMES" maxWidth="192.0" minWidth="10.0" prefWidth="63.0" />
|
<ColumnConstraints hgrow="SOMETIMES" maxWidth="192.0" minWidth="10.0" prefWidth="25.0" />
|
||||||
</columnConstraints>
|
</columnConstraints>
|
||||||
<rowConstraints>
|
<rowConstraints>
|
||||||
<RowConstraints maxHeight="57.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
|
<RowConstraints maxHeight="57.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" />
|
||||||
|
@ -135,73 +135,20 @@
|
||||||
<children>
|
<children>
|
||||||
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
|
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
|
||||||
<center>
|
<center>
|
||||||
<GridPane BorderPane.alignment="CENTER">
|
<ScrollPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
|
||||||
<columnConstraints>
|
<content>
|
||||||
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
|
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="151.0" prefWidth="1000.0">
|
||||||
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
|
<children>
|
||||||
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
|
<Pane prefHeight="151.0" prefWidth="1000.0">
|
||||||
</columnConstraints>
|
<children>
|
||||||
<rowConstraints>
|
<GridPane fx:id="AnswersGridPane" prefHeight="151.0" prefWidth="1000.0">
|
||||||
<RowConstraints maxHeight="134.0" minHeight="10.0" prefHeight="134.0" vgrow="SOMETIMES" />
|
</GridPane>
|
||||||
<RowConstraints maxHeight="76.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" />
|
</children>
|
||||||
</rowConstraints>
|
</Pane>
|
||||||
<children>
|
</children>
|
||||||
<BorderPane fx:id="picture_0" prefHeight="200.0" prefWidth="200.0" >
|
</AnchorPane>
|
||||||
<center>
|
</content>
|
||||||
<ImageView BorderPane.alignment="CENTER">
|
</ScrollPane>
|
||||||
<image>
|
|
||||||
<Image url="@/images/profile.png" />
|
|
||||||
</image>
|
|
||||||
</ImageView>
|
|
||||||
</center>
|
|
||||||
</BorderPane>
|
|
||||||
<BorderPane fx:id="picture_1" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" >
|
|
||||||
<center>
|
|
||||||
<ImageView BorderPane.alignment="CENTER">
|
|
||||||
<image>
|
|
||||||
<Image url="@/images/profile.png" />
|
|
||||||
</image>
|
|
||||||
</ImageView>
|
|
||||||
</center>
|
|
||||||
</BorderPane>
|
|
||||||
<BorderPane fx:id="picture_2" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" >
|
|
||||||
<center>
|
|
||||||
<ImageView BorderPane.alignment="CENTER">
|
|
||||||
<image>
|
|
||||||
<Image url="@/images/profile.png" />
|
|
||||||
</image>
|
|
||||||
</ImageView>
|
|
||||||
</center>
|
|
||||||
</BorderPane>
|
|
||||||
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
|
|
||||||
<top>
|
|
||||||
<Label fx:id="name0" text="Name" BorderPane.alignment="CENTER" >
|
|
||||||
<font>
|
|
||||||
<Font size="15.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
</top>
|
|
||||||
</BorderPane>
|
|
||||||
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1">
|
|
||||||
<top>
|
|
||||||
<Label fx:id="name1" text="Name" BorderPane.alignment="CENTER">
|
|
||||||
<font>
|
|
||||||
<Font size="15.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
</top>
|
|
||||||
</BorderPane>
|
|
||||||
<BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
|
|
||||||
<top>
|
|
||||||
<Label fx:id="name2" text="Name" BorderPane.alignment="CENTER">
|
|
||||||
<font>
|
|
||||||
<Font size="15.0" />
|
|
||||||
</font>
|
|
||||||
</Label>
|
|
||||||
</top>
|
|
||||||
</BorderPane>
|
|
||||||
</children>
|
|
||||||
</GridPane>
|
|
||||||
</center>
|
</center>
|
||||||
</BorderPane>
|
</BorderPane>
|
||||||
</children>
|
</children>
|
||||||
|
|
Loading…
Reference in New Issue