Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication6;
- import java.awt.image.BufferedImage;
- import java.awt.image.ColorConvertOp;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JFileChooser;
- /**
- *
- * @author tusharjoshi
- */
- public class MainWindow extends javax.swing.JFrame {
- /**
- * Creates new form MainWindow
- */
- public MainWindow() {
- initComponents();
- }
- /**
- * 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() {
- imageLabel = new javax.swing.JLabel();
- chooseButton = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- chooseButton.setText("Choose");
- chooseButton.setActionCommand("Choose");
- chooseButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- chooseButtonActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(19, 19, 19)
- .addComponent(imageLabel)
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(303, Short.MAX_VALUE)
- .addComponent(chooseButton)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(imageLabel)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 259, Short.MAX_VALUE)
- .addComponent(chooseButton)
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setDialogTitle("Open image?");
- fileChooser.showOpenDialog(this);
- try {
- //writes new file
- BufferedImage image = ImageIO.read(fileChooser.getSelectedFile());
- ImageIcon icon = new ImageIcon(image);
- imageLabel.setIcon(icon);
- BufferedImage image2 = new BufferedImage(image.getWidth(),
- image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
- // convert colored image to grayscale
- ColorConvertOp grayScale = new ColorConvertOp(image.getColorModel().getColorSpace(),image2.getColorModel().getColorSpace(),null);
- grayScale.filter(image,image2);
- imageLabel.setIcon(new ImageIcon(image2));
- }
- catch (IOException f) {
- System.out.println("Saving failed! Could not save image.");
- }
- }
- /**
- * @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(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(MainWindow.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 MainWindow().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton chooseButton;
- private javax.swing.JLabel imageLabel;
- // End of variables declaration
- }
Add Comment
Please, Sign In to add comment