向 JFrame 按钮添加操作以编辑先前选择的图片时出错
Errors when adding an action to a JFrame button to edit a picture previously selected
我正尝试在 Java 中为一个学校项目制作一个 ImageEditor。到目前为止,我设法使用 JFrame 制作了带有两个子菜单的菜单:打开 - 我可以在其中搜索图片并在主菜单中查看它 window;并清除 - 我清除主要 window 的地方。这部分工作正常。最近我还添加了 Jframe 4 按钮,以便编辑图片并将其设为灰度,棕褐色调,负片或 red/green/blue。
我仍然卡在第一个按钮上,我需要用灰色调制作图片。我找到了算法,但我不知道如何摆脱这个 errors.The 代码如下所示:
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
}
点击灰度按钮时出现的错误:
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[=11=]0(ImageEditor.java:21)
at myPackage.ImageEditor.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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
从你的函数中删除这一行
throw new UnsupportedOperationException("Not supported yet.");
我正尝试在 Java 中为一个学校项目制作一个 ImageEditor。到目前为止,我设法使用 JFrame 制作了带有两个子菜单的菜单:打开 - 我可以在其中搜索图片并在主菜单中查看它 window;并清除 - 我清除主要 window 的地方。这部分工作正常。最近我还添加了 Jframe 4 按钮,以便编辑图片并将其设为灰度,棕褐色调,负片或 red/green/blue。
我仍然卡在第一个按钮上,我需要用灰色调制作图片。我找到了算法,但我不知道如何摆脱这个 errors.The 代码如下所示:
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
}
点击灰度按钮时出现的错误:
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[=11=]0(ImageEditor.java:21)
at myPackage.ImageEditor.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.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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)
从你的函数中删除这一行
throw new UnsupportedOperationException("Not supported yet.");