I am trying to make a ImageEditor in Java, for a school project. Until now I managed to make with JFrame the menu with two submenus: open - where i can search for a picture and see it in the main window; and clear - where i clear the main window. This part works okey. Lately I added also with Jframe 4 buttons, in order to edit the picture and make it grayscale, in sepia tones, negative or red/green/blue.
I am still stuck at the first button where I need to make the picture in grey tones. I found the algorithm but i have no idea how can i get rid of this errors.The code looks like this:
package myPackage;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class ImageEditor extends javax.swing.JFrame {
JFileChooser img = new JFileChooser();
static BufferedImage image;
public ImageEditor() {
initComponents();
}
private ImageEditor(BufferedImage image) {
this.image = image;
}
private static void write(BufferedImage image, String jpg, JFileChooser img) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private static void write(BufferedImage image, String jpg, File f) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jSP = new javax.swing.JScrollPane();
jB_Greyscale = new javax.swing.JButton();
jB_Sepia = new javax.swing.JButton();
jB_RGB = new javax.swing.JButton();
jB_Negative = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMI_Open = new javax.swing.JMenuItem();
jMI_Clear = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jB_Greyscale.setText("Black & White");
jB_Greyscale.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jB_GreyscaleActionPerformed(evt);
}
});
jB_Sepia.setText("Sepia");
jB_RGB.setText("Red/Green/Blue");
jB_Negative.setText("Negative");
jMenu1.setText("File");
jMI_Open.setText("Open");
jMI_Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMI_OpenActionPerformed(evt);
}
});
jMenu1.add(jMI_Open);
jMI_Clear.setText("Clear");
jMI_Clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMI_ClearActionPerformed(evt);
}
});
jMenu1.add(jMI_Clear);
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)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jB_Greyscale, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jB_Sepia, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jB_RGB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jB_Negative, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSP, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jSP)
.addGroup(layout.createSequentialGroup()
.addComponent(jB_Greyscale)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB_Sepia)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB_RGB)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jB_Negative)
.addGap(0, 169, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
//Create a label
JLabel jlab = new JLabel();
private void jMI_OpenActionPerformed(java.awt.event.ActionEvent evt) {
//Creez file chooser
JFileChooser jfc = new JFileChooser();
//Selectez poza si o afisez
if(jfc.showOpenDialog(jMenu1) == JFileChooser.APPROVE_OPTION){
//Select the path
java.io.File f = jfc.getSelectedFile();
//Set icon
jlab.setIcon(new ImageIcon(f.toString()));
//Alignment
jlab.setHorizontalAlignment(JLabel.CENTER);
//Add label for jScrollPane
jSP.getViewport().add(jlab);
//Save image for futher edit
img = jfc;
}
}
private void jMI_ClearActionPerformed(java.awt.event.ActionEvent evt) {
//Clear image from jlabel jlab
jlab.setIcon(null);
}
private void jB_GreyscaleActionPerformed(java.awt.event.ActionEvent evt) {
//Get image dimension
ImageEditor.write(image,"jpg",img);
int width = image.getWidth();
int height = image.getHeight();
int p = 0, a, r, g, b, avg, y, x;
//Convert to grayscale
for(y = 0; y < height; y++){
for(x = 0; x < width; x++)
p = image.getRGB(x,y);
a = (p >> 24)&0xff;
r = (p >> 16)&0xff;
g = (p >> 8)&0xff;
b = p >> 0xff;
//Calculate the average
avg = (r+g+b)/3;
//Replace RGB values with the average
p = (a << 24) | (avg << 16) | (avg << 8) | avg;
image.setRGB(x, y, p);
}
File f = new File("C:\\Users\\Iulia\\Grayscale.jpg");
ImageEditor.write(image, "jpg", f);
}
public static void main(String args[]) {
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(ImageEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ImageEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ImageEditor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ImageEditor.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 ImageEditor().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jB_Greyscale;
private javax.swing.JButton jB_Negative;
private javax.swing.JButton jB_RGB;
private javax.swing.JButton jB_Sepia;
private javax.swing.JMenuItem jMI_Clear;
private javax.swing.JMenuItem jMI_Open;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jSP;
// End of variables declaration
}
And the error i get when i click on the grayscale button:
Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.
at myPackage.ImageEditor.write(ImageEditor.java:33)
at myPackage.ImageEditor.jB_GreyscaleActionPerformed(ImageEditor.java:155)
at myPackage.ImageEditor.access$000(ImageEditor.java:21)
at myPackage.ImageEditor$1.actionPerformed(ImageEditor.java:59)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Delete this line from your functions
throw new UnsupportedOperationException("Not supported yet.");