Skip to content

Instantly share code, notes, and snippets.

@alvareztech
Created November 29, 2016 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvareztech/a4c1f6c74551a4f8d2642ac099479b86 to your computer and use it in GitHub Desktop.
Save alvareztech/a4c1f6c74551a4f8d2642ac099479b86 to your computer and use it in GitHub Desktop.
Aplicación contador de votos y PieChart JavaFX. (necesita imágenes)
// necesita imágenes
package tech.alvarez;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
private int nroVotosSaltena = 0;
private int nroVotosTucumana = 0;
private int nroVotosLaucha = 0;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Label tituloLabel = new Label("Qué prefieres?");
Image saltenaImage = new Image(getClass().getResourceAsStream("imagenes/saltena.jpg"));
Image tucumanaImage = new Image(getClass().getResourceAsStream("imagenes/tucumana.jpg"));
Image llauchaImage = new Image(getClass().getResourceAsStream("imagenes/llaucha.jpg"));
ImageView saltenaImageView = new ImageView(saltenaImage);
ImageView tucumanaImageView = new ImageView(tucumanaImage);
ImageView llauchaImageView = new ImageView(llauchaImage);
saltenaImageView.setFitHeight(80);
saltenaImageView.setFitWidth(80);
tucumanaImageView.setFitHeight(80);
tucumanaImageView.setFitWidth(80);
llauchaImageView.setFitHeight(80);
llauchaImageView.setFitWidth(80);
Button saltenaButton = new Button("", saltenaImageView);
Button tucumanaButton = new Button("", tucumanaImageView);
Button lauchaButton = new Button("", llauchaImageView);
saltenaButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
nroVotosSaltena++;
}
});
tucumanaButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
nroVotosTucumana++;
}
});
lauchaButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
nroVotosLaucha++;
}
});
Button finalizarButton = new Button("Finalizar");
HBox hBox = new HBox();
hBox.setSpacing(20);
hBox.getChildren().add(saltenaButton);
hBox.getChildren().add(tucumanaButton);
hBox.getChildren().add(lauchaButton);
VBox vBox = new VBox();
vBox.setSpacing(20);
vBox.setPadding(new Insets(20, 20, 20, 20));
vBox.getChildren().add(tituloLabel);
vBox.getChildren().add(hBox);
vBox.getChildren().add(finalizarButton);
finalizarButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("> salteña: " + nroVotosSaltena);
System.out.println("> tucumana: " + nroVotosTucumana);
System.out.println("> laucha: " + nroVotosLaucha);
ObservableList<PieChart.Data> datos = FXCollections.observableArrayList(
new PieChart.Data("Salteña", nroVotosSaltena),
new PieChart.Data("Tucumana", nroVotosTucumana),
new PieChart.Data("Laucha", nroVotosLaucha)
);
PieChart grafico = new PieChart(datos);
vBox.getChildren().add(grafico);
}
});
Scene scene = new Scene(vBox, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment