TEMA:
Programa En Java Que Pueda Tabular O Graficar El Tiempo De Consumo Dedicado Al Uso De Las Redes Sociales.
ESTUDIANTE:
Chinchay Yajahuanca Karol Josef
DOCENTE:
Ing. García Córdova Eddy Javier
ASIGNATURA:
Programación Visual I
CICLO:
III
SECCION:
B
PIURA – 2017 2017
Actividad de Responsabilidad Social.
E laboración de un programa en J ava que pueda tabular o graficar el tiempo de consumo dedicado al uso de las redes sociales. Solución:
en el si guiente programa que mostrare a continuación atraves de capturas de “
pantalla ; he realizado un gráfico estadístico (tipo pastel) en el que puedo ingresar ”
las horas de uso que se desee dar uso a una red social y luego al hacer cli c en el botón actualizar la gráfica se dividirá en partes según las horas que se le indique. Aquí El Ejemplo (capturas de pantalla):
Imagen N º 01: Programa al terminar de ser elaborado.
Imagen N º 02: Programa al ser ejecutado sin horas asignadas.
Imagen N º 02: Programa al ser ejecutado con la siguiente cantidad de horas por cada
red social: Facebook, 20; WhatsApp, 30; Twitter, 10; Instagram, 5.
Líneas De Código Para Cada Uno De Los Botones Empleados.
package actividad_numero_11_rs; import java.awt.BorderLayout; import java.util.Set; import javafx.application.Platform; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.embed.swing.JFXPanel; import javafx.geometry.Side; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.chart.PieChart; import javafx.scene.chart.PieChart.Data; import javafx.scene.control.Label; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; /** * * @author Usuario */ public class frm_grafica_del_tiempo_en_redes_sociales extends javax.swing.JFrame { /** * Contenedor para el grafico */ private final JFXPanel jfxPanel = new JFXPanel();
/** * Para almacenar los datos del grafico */ private ObservableList data2d = FXCollections.observableArrayList(); /** * Grafico 2d */ private PieChart pieChart = new PieChart(); /** * Creates new form Grafico */ public frm_grafica_del_tiempo_en_redes_sociales() { initComponents(); setTitle("Uso de Redes Sociales"); setLocationRelativeTo(null); //crea la escena createScene(); //añade grafico a Swing jPanel1.setLayout(new BorderLayout()); jPanel1.add(jfxPanel,BorderLayout.CENTER); } private void createScene(){ Platform.runLater(() -> { pieChart.setTitle("");//titulo del grafico pieChart.setLegendSide(Side.LEFT);//Posicion de leyenda
pieChart.setData(getChartData()); updateColors(); jfxPanel.setScene(new Scene(pieChart)); }); } private ObservableList getChartData() { data2d.addAll(new PieChart.Data("Facebook", 55), new PieChart.Data("Twitter", 77), new PieChart.Data("Instagram", 97), new PieChart.Data("WhatSapp", 107)); return data2d; } /** * Actualiza colores de la torta y su leyenda */ private void updateColors(){ //colores para cada seccion de la torta Color[] colors = { Color.web("#04B404"), Color.web("#FF8000"),
Color.web("#3498DB"),Color.web("#8E44AD") }; int i = 0; //cambia colores de cada seccion de la torta for (PieChart.Data data : data2d) { String hex = String.format( "#%02X%02X%02X", (int)( colors[i].getRed() * 255 ), (int)( colors[i].getGreen() * 255 ),
(int)( colors[i].getBlue() * 255 ) ); data.getNode().setStyle( "-fx-pie-color: "+hex+";"); i++; } //cambia colores de la leyenda Set items;items = pieChart.lookupAll("Label.chart-legend-item"); i = 0; for (Node item : items) { Label label = (Label) item; final Rectangle rectangle = new Rectangle(20, 20, colors[i]); label.setGraphic(rectangle); i++; } } /** * Actualiza valores del gráfico * @param Facebook valores para Facebook * @param Twitter valores para Twitter * @param Instagram valores para Instagram * @param whatsapp valores para whatsapp */ private void setChartData(int Facebook, int Twitter, int Instagram,int whatsapp){ Platform.runLater(() -> { data2d.clear(); data2d.addAll(new PieChart.Data("Facebook", Facebook),
new PieChart.Data("Twitter", Twitter), new PieChart.Data("Instagram", Instagram), new PieChart.Data("WhatSapp", Instagram)); updateColors(); });
}
/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */
/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ Botón Actualizar: setChartData( Integer.valueOf(jSpinner1.getValue().toString()),Integer.valueOf(jSpinner2.getVal ue().toString()),Integer.valueOf(jSpinner3.getValue().toString()),Integer.valueOf(j Spinner4.getValue().toString()));
Botón Cerrar: dispose();