librejack

Blackjack clone written in java
git clone https://github.com/michaelneuper/librejack.git
README | Files | Log | Refs | LICENSE

commit be2853a9e4e69dd1b39855bc87bec39f0e9da244
parent 7719e4a041648a4b6495a49a81574c5906b02dcc
Author: Michael Neuper <michael@michaelneuper.com>
Date:   Fri, 12 May 2023 19:40:28 +0200

Create StatisticsChart

Diffstat:
Asrc/view/StatisticsChart.form | 18++++++++++++++++++
Asrc/view/StatisticsChart.java | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 127 insertions(+), 0 deletions(-)

diff --git a/src/view/StatisticsChart.form b/src/view/StatisticsChart.form @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JPanelFormInfo"> + <AuxValues> + <AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/> + <AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/> + <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/> + <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/> + <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/> + <AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,1,23,0,0,1,-109"/> + </AuxValues> + + <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/> +</Form> diff --git a/src/view/StatisticsChart.java b/src/view/StatisticsChart.java @@ -0,0 +1,109 @@ +package view; + +import java.awt.Color; +import java.io.IOException; +import javax.swing.BorderFactory; +import model.Statistics; +import org.jfree.chart.ChartFactory; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PiePlot; +import org.jfree.data.general.DefaultPieDataset; +import org.jfree.data.general.PieDataset; +import org.jfree.util.Rotation; + +/** + * The StatisticsChart class represents a panel that displays a pie chart based + * on the statistics of a game. It uses the JFreeChart library to create and + * display the chart. + * + * @author Michael Neuper + * <a href="mailto:michael@michaelneuper.com">michael@michaelneuper.com</a> + */ +public class StatisticsChart extends javax.swing.JPanel { + + Statistics stats; + PieDataset dataset; + JFreeChart chart; + ChartPanel chartPanel; + + /** + * Creates new form StatisticsChart + */ + public StatisticsChart() { + initComponents(); + createChart(); + } + + /** + * Creates the chart panel and adds it to the panel. + */ + private void createChart() { + dataset = createDataset(); + chart = createChart(dataset, ""); + chartPanel = new ChartPanel(chart); + chartPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); + chartPanel.setBackground(Color.DARK_GRAY); + this.add(chartPanel); + } + + /** + * Creates a dataset based on the game statistics. + * + * @return the dataset for the chart + */ + private PieDataset createDataset() { + try { + stats = Statistics.load(); + } catch (IOException e) { + System.err.println(e);; + } + DefaultPieDataset result = new DefaultPieDataset(); + result.setValue("Wins", stats.getWins()); + result.setValue("Losses", stats.getLosses()); + result.setValue("Pushes", stats.getPushes()); + + return result; + } + + /** + * Creates a pie chart based on the specified dataset and title. + * + * @param dataset dataset to use for the chart + * @param title title to display on the chart + * @return the chart object + */ + private JFreeChart createChart(PieDataset dataset, String title) { + + JFreeChart chart = ChartFactory.createPieChart( + title, // chart title + dataset, // data + true, // include legend + true, + false + ); + + PiePlot plot = (PiePlot) chart.getPlot(); + plot.setStartAngle(290); + plot.setDirection(Rotation.CLOCKWISE); + plot.setForegroundAlpha(0.5f); + + return chart; + } + + /** + * 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. + */ + @SuppressWarnings("unchecked") + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + setLayout(new java.awt.BorderLayout()); + }// </editor-fold>//GEN-END:initComponents + + + // Variables declaration - do not modify//GEN-BEGIN:variables + // End of variables declaration//GEN-END:variables +}