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 task5; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Paint; import java.awt.event.ActionEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.AbstractAction; import static javax.swing.Action.NAME; import static javax.swing.Action.SELECTED_KEY; import javax.swing.ImageIcon; import javax.swing.JCheckBox; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.DefaultDrawingSupplier; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.StandardXYBarPainter; import org.jfree.chart.renderer.xy.XYBarRenderer; import org.jfree.data.statistics.HistogramDataset; /** * * @author ebrarsahin */ public class deneme extends javax.swing.JFrame { /** * Creates new form deneme */ JFrame myframe; BufferedImage img; ArrayList<Integer> red; ArrayList<Integer> green; ArrayList<Integer> blue; private HistogramDataset dataset; private XYBarRenderer renderer; private double contrastR, contrastG, contrastB; Object[] r, g, b; public deneme() { initComponents(); red = new ArrayList<>(); green = new ArrayList<>(); blue = new ArrayList<>(); } /** * 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() { btn_chooseImage = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); btn_chooseImage.setText("choose image"); btn_chooseImage.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_chooseImageActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btn_chooseImage, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(btn_chooseImage, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)); pack(); }// </editor-fold>//GEN-END:initComponents private void btn_chooseImageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_chooseImageActionPerformed // TODO add your handling code here: String userDir = System.getProperty("user.home"); JFileChooser fileChooser = new JFileChooser(userDir + "/Desktop"); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); try { img = ImageIO.read(selectedFile); getRGBs(); } catch (IOException ex) { Logger.getLogger(deneme.class.getName()).log(Level.SEVERE, null, ex); } } r = red.toArray(); g = green.toArray(); b = blue.toArray(); // // if(red.indexOf(0)==-1 || red.indexOf(255)==-1){ // red.sort(null); // System.out.println("Before Streching... Min and Max Value..." ); // System.out.println(red.get(0)+" "+red.get(red.size()-1)); // contrastR= red.get(red.size()-1)- red.get(0); // int fmin=red.get(0); // int fmax=red.get(red.size()-1); // for (int i = 0; i < red.size(); i++) { // int temp2=0; // if(((int)r[i])<=fmin) // temp2=0; // else if(fmin<=((int)r[i]) && ((int)r[i])<= fmax){ // double temp=((((int)r[i])- fmin)/(contrastR)); // temp2=(int) Math.round((temp*255)); // } // else if(((int)r[i])>=fmax){ // temp2=255; // } // r[i]=temp2; // } // ArrayList<Integer> tempp = new ArrayList<>(); // for (Object r1 : r) { // tempp.add((int) r1); // } // tempp.sort(null); // System.out.println("After Stretching... Min and Max Value..."); // System.out.println(tempp.get(0) + " " + tempp.get(tempp.size() - 1)); // } // // // // // // if(green.indexOf(0)==-1 || green.indexOf(255)==-1){ // green.sort(null); // contrastG= green.get(green.size()-1)- green.get(0); // int fmin=green.get(0); // int fmax=green.get(green.size()-1); // for (int i = 0; i < green.size(); i++) { // int temp2=0; // if(((int)g[i])<=fmin) // temp2=0; // else if(fmin<=((int)g[i]) && ((int)g[i])<= fmax){ // double temp=((((int)g[i])- fmin)/(contrastG)); // temp2=(int) Math.round((temp*255)); // } // else if(((int)g[i])>=fmax){ // temp2=255; // } // g[i]=temp2; // } // } // if(blue.indexOf(0)==-1 || blue.indexOf(255)==-1){ // blue.sort(null); // contrastB= blue.get(blue.size()-1)- blue.get(0); // int fmin=blue.get(0); // int fmax=blue.get(blue.size()-1); // for (int i = 0; i < blue.size(); i++) { // int temp2=0; // if(((int)b[i])<=fmin) // temp2=0; // else if(fmin<=((int)b[i]) && ((int)b[i])<= fmax){ // double temp=((((int)b[i])- fmin)/(contrastB)); // temp2=(int) Math.round((temp*255)); // } // else if(((int)b[i])>=fmax){ // temp2=255; // } // b[i]=temp2; // } // } display(); }//GEN-LAST:event_btn_chooseImageActionPerformed 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(deneme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(deneme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(deneme.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(deneme.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 deneme().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btn_chooseImage; // End of variables declaration//GEN-END:variables private JFrame get_MYIMAGE() { JFrame frame = new JFrame("picture"); frame.setSize(img.getWidth(), img.getHeight()); JLabel l = new JLabel(); l.setSize(img.getWidth(), img.getHeight()); l.setIcon((new ImageIcon(img))); frame.add(l); frame.setVisible(true); return frame; } private void getRGBs() { for (int i = 0; i < img.getWidth(); i++) { for (int j = 0; j < img.getHeight(); j++) { Color c = new Color(img.getRGB(i, j)); red.add(c.getRed()); green.add(c.getGreen()); blue.add(c.getBlue()); } } } private ChartPanel createChartPanel() { dataset = new HistogramDataset(); final int w = img.getWidth(); final int h = img.getHeight(); JFreeChart chart = ChartFactory.createHistogram("Histogram", "Value", "Count", dataset, PlotOrientation.VERTICAL, true, true, false); XYPlot plot = (XYPlot) chart.getPlot(); renderer = (XYBarRenderer) plot.getRenderer(); renderer.setBarPainter(new StandardXYBarPainter()); double redd[] = new double[red.size()]; for (int i = 0; i < red.size(); i++) { redd[i] = (int) r[i]; } dataset.addSeries("Red", redd, 256); double greenn[] = new double[green.size()]; for (int i = 0; i < green.size(); i++) { greenn[i] = (int) g[i]; } dataset.addSeries("Green", greenn, 256); double bluee[] = new double[blue.size()]; for (int i = 0; i < blue.size(); i++) { bluee[i] = (int) b[i]; } dataset.addSeries("Blue", bluee, 256); Paint[] paintArray = { new Color(0x80ff0000, true), new Color(0x8000ff00, true), new Color(0x800000ff, true) }; plot.setDrawingSupplier( new DefaultDrawingSupplier(paintArray, DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE, DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE)); ChartPanel panel = new ChartPanel(chart); panel.setMouseWheelEnabled(true); return panel; } JPanel createControlPanel() { JPanel panel = new JPanel(); panel.add(new JCheckBox(new deneme.VisibleAction(0))); panel.add(new JCheckBox(new deneme.VisibleAction(1))); panel.add(new JCheckBox(new deneme.VisibleAction(2))); return panel; } class VisibleAction extends AbstractAction { private final int i; public VisibleAction(int i) { this.i = i; this.putValue(NAME, (String) dataset.getSeriesKey(i)); this.putValue(SELECTED_KEY, true); renderer.setSeriesVisible(i, true); } @Override public void actionPerformed(ActionEvent e) { renderer.setSeriesVisible(i, !renderer.getSeriesVisible(i)); } } private void display() { JFrame f = new JFrame("Histogram"); f.add(createChartPanel()); f.add(createControlPanel(), BorderLayout.SOUTH); f.add(new JLabel(new ImageIcon(img)), BorderLayout.WEST); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } }