ImageProcessing.RGBHistogram_GUI.java Source code

Java tutorial

Introduction

Here is the source code for ImageProcessing.RGBHistogram_GUI.java

Source

/*
 * 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 ImageProcessing;

import java.awt.Color;
import java.awt.Component;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import org.jfree.chart.JFreeChart;

/**
 *
 * @author Ryan Fadholi
 */
public class RGBHistogram_GUI extends javax.swing.JFrame {

    /**
     * Creates new form RGBHistogram_GUI
     */
    public RGBHistogram_GUI() {
        this(null);
    }

    public RGBHistogram_GUI(Component callerComponent) {
        //callerComponent is used only to determine the position of this
        //frame when called.
        initComponents();
        this.setLocationRelativeTo(callerComponent);

        //Determine the chart width & height for displaying.
        int chartLabelHeight = sourceImageGreenHistogram.getHeight();
        int chartLabelWidth = sourceImageGreenHistogram.getWidth();

        //Get values for each color component from source image.
        double[] sourceRedValues = ImageProcessing.extractRedColor(Main.getSourceImage());
        double[] sourceGreenValues = ImageProcessing.extractGreenColor(Main.getSourceImage());
        double[] sourceBlueValues = ImageProcessing.extractBlueColor(Main.getSourceImage());

        //Get values for each color component from processed image.
        double[] processedRedValues = ImageProcessing.extractRedColor(Main.getProcessedImage());
        double[] processedGreenValues = ImageProcessing.extractGreenColor(Main.getProcessedImage());
        double[] processedBlueValues = ImageProcessing.extractBlueColor(Main.getProcessedImage());

        //Create charts for each color component from source image.
        JFreeChart sourceRedChart = ImageProcessing.createHistogram(sourceRedValues, Color.RED);
        JFreeChart sourceGreenChart = ImageProcessing.createHistogram(sourceGreenValues, Color.GREEN);
        JFreeChart sourceBlueChart = ImageProcessing.createHistogram(sourceBlueValues, Color.BLUE);

        //Create charts for each color component from processed image.
        JFreeChart processedRedChart = ImageProcessing.createHistogram(processedRedValues, Color.RED);
        JFreeChart processedGreenChart = ImageProcessing.createHistogram(processedGreenValues, Color.GREEN);
        JFreeChart processedBlueChart = ImageProcessing.createHistogram(processedBlueValues, Color.BLUE);

        //Convert each source image charts to a BufferedImage.
        BufferedImage sourceRedChartImage = sourceRedChart.createBufferedImage(chartLabelWidth, chartLabelHeight);
        BufferedImage sourceGreenChartImage = sourceGreenChart.createBufferedImage(chartLabelWidth,
                chartLabelHeight);
        BufferedImage sourceBlueChartImage = sourceBlueChart.createBufferedImage(chartLabelWidth, chartLabelHeight);

        //Convert each processsed image charts to a BufferedImage.
        BufferedImage processedRedChartImage = processedRedChart.createBufferedImage(chartLabelWidth,
                chartLabelHeight);
        BufferedImage processedGreenChartImage = processedGreenChart.createBufferedImage(chartLabelWidth,
                chartLabelHeight);
        BufferedImage processedBlueChartImage = processedBlueChart.createBufferedImage(chartLabelWidth,
                chartLabelHeight);

        //Display each source image charts to GUI.
        sourceImageRedHistogram.setIcon(new ImageIcon(sourceRedChartImage));
        sourceImageGreenHistogram.setIcon(new ImageIcon(sourceGreenChartImage));
        sourceImageBlueHistogram.setIcon(new ImageIcon(sourceBlueChartImage));

        //Display each source image charts to GUI.
        processedImageRedHistogram.setIcon(new ImageIcon(processedRedChartImage));
        processedImageGreenHistogram.setIcon(new ImageIcon(processedGreenChartImage));
        processedImageBlueHistogram.setIcon(new ImageIcon(processedBlueChartImage));

    }

    /**
     * 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() {

        sourceImageHistogram1 = new javax.swing.JLabel();
        sourceLabel = new javax.swing.JLabel();
        sourceImageRedHistogram = new javax.swing.JLabel();
        sourceImageGreenHistogram = new javax.swing.JLabel();
        sourceImageBlueHistogram = new javax.swing.JLabel();
        separator = new javax.swing.JSeparator();
        processedLabel = new javax.swing.JLabel();
        processedImageBlueHistogram = new javax.swing.JLabel();
        processedImageGreenHistogram = new javax.swing.JLabel();
        processedImageRedHistogram = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("RGB Histogram");

        sourceLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        sourceLabel.setText("Source Image Histogram");
        sourceLabel.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        separator.setBackground(new java.awt.Color(0, 0, 0));
        separator.setOrientation(javax.swing.SwingConstants.VERTICAL);

        processedLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        processedLabel.setText("Processed Image Histogram");
        processedLabel.setToolTipText("");
        processedLabel.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(sourceImageBlueHistogram, javax.swing.GroupLayout.PREFERRED_SIZE, 250,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(sourceImageGreenHistogram, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(sourceImageRedHistogram, javax.swing.GroupLayout.Alignment.TRAILING,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 250,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(sourceLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(processedLabel)
                                .addComponent(processedImageRedHistogram, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(processedImageGreenHistogram, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(processedImageBlueHistogram, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        250, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(sourceLabel)
                                .addComponent(processedLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(sourceImageRedHistogram,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(sourceImageGreenHistogram,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(sourceImageBlueHistogram,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 236,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(processedImageRedHistogram,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(processedImageGreenHistogram,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(processedImageBlueHistogram,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 75,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(16, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RGBHistogram_GUI.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RGBHistogram_GUI.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RGBHistogram_GUI.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RGBHistogram_GUI.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RGBHistogram_GUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel processedImageBlueHistogram;
    private javax.swing.JLabel processedImageGreenHistogram;
    private javax.swing.JLabel processedImageRedHistogram;
    private javax.swing.JLabel processedLabel;
    private javax.swing.JSeparator separator;
    private javax.swing.JLabel sourceImageBlueHistogram;
    private javax.swing.JLabel sourceImageGreenHistogram;
    private javax.swing.JLabel sourceImageHistogram1;
    private javax.swing.JLabel sourceImageRedHistogram;
    private javax.swing.JLabel sourceLabel;
    // End of variables declaration//GEN-END:variables
}