Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package app.gui; import app.utils.BufferedImageHelper; import java.awt.Color; import java.awt.Dimension; import java.awt.image.BufferedImage; import java.util.logging.Logger; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import static app.utils.ImageUtilities.saveHistogramToFile; /** * * @author agnieszka.kluska */ public class Histogram extends javax.swing.JFrame { XYPlot xyplot; XYItemRenderer renderer; boolean autoUpdate = false; ChartPanel chartpanel; BufferedImage image; private JFreeChart chart; /** * Creates new form Histogram */ public Histogram(BufferedImage image) { initComponents(); this.image = image; MakeChart(); } /** * 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"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jBtnRed = new javax.swing.JButton(); jBtnGreen = new javax.swing.JButton(); jBtnBlue = new javax.swing.JButton(); jBtnGrey = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jmiSave = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jBtnRed.setText("Red"); jBtnRed.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnRedActionPerformed(evt); } }); jBtnGreen.setText("Green"); jBtnGreen.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnGreenActionPerformed(evt); } }); jBtnBlue.setText("Blue"); jBtnBlue.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnBlueActionPerformed(evt); } }); jBtnGrey.setText("I"); jBtnGrey.setToolTipText(""); jBtnGrey.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnGreyActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jBtnRed, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jBtnGreen, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jBtnBlue, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jBtnGrey, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(56, 56, 56))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jBtnRed).addComponent(jBtnGreen).addComponent(jBtnBlue) .addComponent(jBtnGrey))); jPanel2.setPreferredSize(new java.awt.Dimension(0, 0)); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE)); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 250, Short.MAX_VALUE)); jPanel2.setLayout(new java.awt.BorderLayout()); jMenu1.setText("File"); jmiSave.setText("Save"); jmiSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jmiSaveActionPerformed(evt); } }); jMenu1.add(jmiSave); jMenuBar1.add(jMenu1); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))); add(jPanel2, java.awt.BorderLayout.CENTER); pack(); }// </editor-fold> private void jBtnRedActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: renderer.setSeriesVisible(0, !renderer.getSeriesVisible(0)); } private void jBtnGreenActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: renderer.setSeriesVisible(1, !renderer.getSeriesVisible(1)); } private void jBtnBlueActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: renderer.setSeriesVisible(2, !renderer.getSeriesVisible(2)); } private void jBtnGreyActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: renderer.setSeriesVisible(3, !renderer.getSeriesVisible(3)); } private void jmiSaveActionPerformed(java.awt.event.ActionEvent evt) { saveHistogramToFile(chart); } private void MakeChart() { if (this.image.getType() != BufferedImage.TYPE_BYTE_BINARY) { XYDataset xydataset = createDataset(); chart = createChart(xydataset); } else { this.remove(this.jPanel1); DefaultCategoryDataset dataset = createbinaryDataset(); chart = createChart(dataset); } saveHistogramToFile(chart); ChartPanel chartpanelTmp = new ChartPanel(chart); chartpanelTmp.setPreferredSize(new Dimension(200, 200)); if (this.chartpanel != null && this.jPanel2.getComponents().length > 0) { this.jPanel2.remove(this.chartpanel); } this.chartpanel = chartpanelTmp; this.jPanel2.add(this.chartpanel); this.jPanel2.revalidate(); } private static final Logger LOG = Logger.getLogger(Histogram.class.getName()); private DefaultCategoryDataset createbinaryDataset() { int max = this.image.getSampleModel().getNumBands(); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int y = 0; y < max; y++) { double[] values = BufferedImageHelper.getHistogram(this.image, y); for (int x = 0; x < values.length; x++) { dataset.setValue(values[x], "", Integer.toString(x)); } } return dataset; } private XYDataset createDataset() { int max = this.image.getSampleModel().getNumBands(); XYSeriesCollection seriescollection = new XYSeriesCollection(); for (int y = 0; y < max; y++) { XYSeries series = new XYSeries(y); double[] values = BufferedImageHelper.getHistogram(this.image, y); for (int x = 0; x < values.length; x++) { series.add(x, values[x]); } seriescollection.addSeries(series); } if (max == 3) { XYSeries series = new XYSeries(3); double[] values = BufferedImageHelper.getLuminanceHistogram(this.image); for (int x = 0; x < values.length; x++) { series.add(x, values[x]); } seriescollection.addSeries(series); } return seriescollection; } private JFreeChart createChart(DefaultCategoryDataset dataset) { JFreeChart jfreechart = ChartFactory.createBarChart(null, null, null, dataset, PlotOrientation.VERTICAL, false, false, false); final CategoryPlot plot = jfreechart.getCategoryPlot(); BarRenderer barRenderer = (BarRenderer) plot.getRenderer(); return jfreechart; } private JFreeChart createChart(XYDataset xydataset) { JFreeChart jfreechart = ChartFactory.createXYAreaChart(null, null, null, xydataset, PlotOrientation.VERTICAL, false, true, false); jfreechart.setBackgroundPaint(Color.white); xyplot = (XYPlot) jfreechart.getPlot(); xyplot.setBackgroundPaint(Color.lightGray); xyplot.setForegroundAlpha(0.65F); xyplot.setDomainGridlinePaint(Color.white); xyplot.setRangeGridlinePaint(Color.white); ValueAxis valueaxis = xyplot.getDomainAxis(); valueaxis.setTickMarkPaint(Color.black); valueaxis.setLowerMargin(0.0D); valueaxis.setUpperMargin(0.0D); renderer = xyplot.getRenderer(); if (xydataset.getSeriesCount() == 4) { renderer.setSeriesPaint(0, Color.red); renderer.setSeriesPaint(1, Color.green); renderer.setSeriesPaint(2, Color.blue); renderer.setSeriesVisible(0, true); renderer.setSeriesVisible(1, true); renderer.setSeriesVisible(2, true); renderer.setSeriesPaint(3, Color.BLACK); renderer.setSeriesVisible(3, true); } else { this.remove(this.jPanel1); renderer.setSeriesPaint(0, Color.BLACK); renderer.setSeriesVisible(0, true); } ValueAxis valueaxis1 = xyplot.getRangeAxis(); valueaxis1.setTickMarkPaint(Color.black); return jfreechart; } // Variables declaration - do not modify private javax.swing.JButton jBtnRed; private javax.swing.JButton jBtnGreen; private javax.swing.JButton jBtnBlue; private javax.swing.JButton jBtnGrey; private javax.swing.JMenu jMenu1; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jmiSave; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; // End of variables declaration }