我制作了自己的包来存储我编程任务的草稿,然后我做了最后的草稿。我将最终草稿复制并粘贴到我教授给出的实际包中,但它根本不会编译并给我所有这些错误。我试过做项目>干净,但没有帮助。我不明白文件如何在一个地方工作而不是另一个地方。
这是我的代码
package gui;
import filters.EdgeDetectFilter;
import filters.EdgeHighlightFilter;
import filters.Filter;
import filters.FlipHorizontalFilter;
import filters.FlipVerticalFilter;
import filters.GrayscaleFilter;
import filters.SharpenFilter;
import filters.SoftenFilter;
import image.PixelImage;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
//import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
public class SnapShopGUI {
private final JFrame myFrame;
private final JPanel myMenuPanel;
private final JPanel myTopPanel;
private final JPanel myBottomPanel;
private final JPanel myImagePanel;
private JButton myOpen;
private JButton mySave;
private JButton myClose;
private JFileChooser myFileChooser;
private PixelImage myImage;
private JLabel myLabel;
// private String currentDirectory;
private ArrayList<Filter> myFilters;
private ArrayList<JButton> myFilterButtons;
/*
* Initializes all fields.
*/
public SnapShopGUI() {
myFrame = new JFrame("Test Layout");
myFrame.getContentPane().setLayout(new BorderLayout());
myMenuPanel = new JPanel(new BorderLayout());
myTopPanel = new JPanel(new GridLayout(0, 1, 6, 6));
myTopPanel.setBorder(new EmptyBorder(10, 10, 3, 10));
myBottomPanel = new JPanel(new GridLayout(0, 1, 6, 6));
myBottomPanel.setBorder(new EmptyBorder(3, 10, 10, 10));
myImagePanel = new JPanel(new BorderLayout());
myLabel = new JLabel();
// final ImageIcon icon = new ImageIcon(getClass().getResource("seahawks_logo.gif"));
// myLabel.setIcon(icon);
myFileChooser = new JFileChooser();
// myFileChooser.setPreferredSize(new Dimension(500, 400));
myFileChooser.setPreferredSize(new Dimension(600, 400));
myFilterButtons = new ArrayList<JButton>();
}
//
public void start() {
myImagePanel.setBackground(Color.WHITE);
myImagePanel.add(myLabel, BorderLayout.NORTH);
createFilters();
// BOTTOM MENU STUFF
myBottomPanel.add(createOpenButton());
myBottomPanel.add(createSaveButton());
myBottomPanel.add(createCloseButton());
myMenuPanel.add(myTopPanel, BorderLayout.NORTH);
myMenuPanel.add(myBottomPanel, BorderLayout.SOUTH);
myFrame.add(myMenuPanel, BorderLayout.WEST);
myFrame.add(myImagePanel);
myFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
myFrame.setLocationRelativeTo(null); // put this last to center window
}
public void createFilters() {
myFilters = new ArrayList<Filter>();
myFilters.add(new EdgeDetectFilter());
myFilters.add(new EdgeHighlightFilter());
myFilters.add(new FlipHorizontalFilter());
myFilters.add(new FlipVerticalFilter());
myFilters.add(new GrayscaleFilter());
myFilters.add(new SharpenFilter());
myFilters.add(new SoftenFilter());
for (Filter filter : myFilters) {
myTopPanel.add(createFilterButton(filter));
}
}
public JButton createFilterButton(final Filter theFilter) {
JButton fButton = new JButton(theFilter.getDescription());
// fButton.setPreferredSize(new Dimension(120, 30));
fButton.setEnabled(false);
myFilterButtons.add(fButton);
class FilterActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
theFilter.filter(myImage);
myLabel.setIcon(new ImageIcon(myImage));
}
}
fButton.addActionListener(new FilterActionListener());
return fButton;
}
//
public JButton createOpenButton() {
ImageIcon openButtonIcon = new ImageIcon(getClass().getResource("open.gif"));
myOpen = new JButton("Open...", openButtonIcon);
class OpenActionListener implements ActionListener {
@Override
public void actionPerformed(final ActionEvent e) {
int returnVal = myFileChooser.showOpenDialog(myFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = myFileChooser.getSelectedFile();
try {
myImage = PixelImage.load(file);
myLabel.setIcon(new ImageIcon(myImage));
myFrame.pack();
} catch (final IOException fileErr) {
JOptionPane.showMessageDialog(myFrame,
"The selected file did not contain an image!",
"Error!",
JOptionPane.ERROR_MESSAGE);
}
myFrame.setMinimumSize(myFrame.getSize());
for (JButton button : myFilterButtons) {
button.setEnabled(true);
}
mySave.setEnabled(true);
myClose.setEnabled(true);
}
}
}
myOpen.addActionListener(new OpenActionListener());
return myOpen;
}
public JButton createSaveButton() {
final ImageIcon saveButtonIcon = new ImageIcon(getClass().getResource("save.gif"));
mySave = new JButton("Save As...", saveButtonIcon);
mySave.setEnabled(false);
class SaveActionListener implements ActionListener {
@Override
public void actionPerformed(final ActionEvent e) {
myFileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
int returnVal = myFileChooser.showSaveDialog(myFrame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
myImage.save(myFileChooser.getSelectedFile());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
mySave.addActionListener(new SaveActionListener());
return mySave;
}
public JButton createCloseButton() {
final ImageIcon closeButtonIcon = new ImageIcon(getClass().getResource("close.gif"));
myClose = new JButton("Close", closeButtonIcon);
myClose.setEnabled(false);
class CloseActionListener implements ActionListener {
@Override
public void actionPerformed(final ActionEvent e) {
myLabel.setIcon(null);
for (JButton button : myFilterButtons) {
button.setEnabled(false);
}
myClose.setEnabled(false);
mySave.setEnabled(false);
myFrame.setMinimumSize(myMenuPanel.getSize());
myFrame.pack();
}
}
myClose.addActionListener(new CloseActionListener());
return myClose;
}
}
这些是我得到的错误。我没有把它们放在我的草稿包上,但在我教授提供的包裹上,我得到了这些错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at gui.SnapShopGUI.createOpenButton(SnapShopGUI.java:188)
at gui.SnapShopGUI.start(SnapShopGUI.java:133)
at gui.SnapShopMain$1.run(SnapShopMain.java:36)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)