Added the ability to display pictures
							parent
							
								
									91b0d947f2
								
							
						
					
					
						commit
						21867fb5da
					
				|  | @ -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; | ||||
|  |  | |||
|  | @ -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) { | ||||
| 
 | ||||
|     } | ||||
| } | ||||
|  |  | |||
										
											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.rowIndex="1"> | ||||
|               <columnConstraints> | ||||
|                 <ColumnConstraints hgrow="SOMETIMES" maxWidth="144.0" minWidth="10.0" prefWidth="60.0" /> | ||||
|                   <ColumnConstraints hgrow="SOMETIMES" maxWidth="469.0" minWidth="10.0" prefWidth="469.0" /> | ||||
|                 <ColumnConstraints hgrow="SOMETIMES" maxWidth="192.0" minWidth="10.0" prefWidth="63.0" /> | ||||
|                 <ColumnConstraints hgrow="SOMETIMES" maxWidth="144.0" minWidth="10.0" prefWidth="25.0" /> | ||||
|                   <ColumnConstraints hgrow="SOMETIMES" maxWidth="552.0" minWidth="10.0" prefWidth="548.0" /> | ||||
|                 <ColumnConstraints hgrow="SOMETIMES" maxWidth="192.0" minWidth="10.0" prefWidth="25.0" /> | ||||
|               </columnConstraints> | ||||
|               <rowConstraints> | ||||
|                 <RowConstraints maxHeight="57.0" minHeight="0.0" prefHeight="0.0" vgrow="SOMETIMES" /> | ||||
|  | @ -135,73 +135,20 @@ | |||
|                <children> | ||||
|                   <BorderPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1"> | ||||
|                      <center> | ||||
|                         <GridPane BorderPane.alignment="CENTER"> | ||||
|                           <columnConstraints> | ||||
|                             <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | ||||
|                             <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | ||||
|                               <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> | ||||
|                           </columnConstraints> | ||||
|                           <rowConstraints> | ||||
|                             <RowConstraints maxHeight="134.0" minHeight="10.0" prefHeight="134.0" vgrow="SOMETIMES" /> | ||||
|                             <RowConstraints maxHeight="76.0" minHeight="10.0" prefHeight="32.0" vgrow="SOMETIMES" /> | ||||
|                           </rowConstraints> | ||||
|                            <children> | ||||
|                               <BorderPane fx:id="picture_0" prefHeight="200.0" prefWidth="200.0" > | ||||
|                                  <center> | ||||
|                                     <ImageView  BorderPane.alignment="CENTER"> | ||||
|                                        <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> | ||||
|                         <ScrollPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER"> | ||||
|                           <content> | ||||
|                             <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="151.0" prefWidth="1000.0"> | ||||
|                                  <children> | ||||
|                                     <Pane prefHeight="151.0" prefWidth="1000.0"> | ||||
|                                        <children> | ||||
|                                           <GridPane fx:id="AnswersGridPane" prefHeight="151.0" prefWidth="1000.0"> | ||||
|                                           </GridPane> | ||||
|                                        </children> | ||||
|                                     </Pane> | ||||
|                                  </children> | ||||
|                               </AnchorPane> | ||||
|                           </content> | ||||
|                         </ScrollPane> | ||||
|                      </center> | ||||
|                   </BorderPane> | ||||
|                </children> | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue