0

Until yesterday my JFrame was working perfectly, today i did a little edit to it but it doesn't work anymore. i tried to delete all the code that wasn't strictly bound with my JFrame but it still refuses to show anything, i'll paste my code here

This is my main file

public class ImageEditor extends JFrame{

    JFrame mainFrame = new JFrame("ImageEditor");
    JMenuBar menu = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");
    JMenuItem openButton = new JMenuItem("Open...");
    JMenuItem saveButton = new JMenuItem("Save");
    JMenuItem saveAsButton = new JMenuItem("Save as...");
    JMenuItem exitButton = new JMenuItem("Exit");
    JMenuItem rotatePositive = new JMenuItem("Rotate 45°");
    JMenuItem rotateNegative = new JMenuItem("Rotate -45°");
    JMenuItem resizeButton = new JMenuItem("Resize");
    JMenuItem adjustBrightnessButton = new JMenuItem("Adjust brightness");
    JMenuItem insertLabel = new JMenuItem("Add text...");
    JFileChooser fileChooser;
    JFileChooser fileSaver;
    JSlider brightnessSlider = new JSlider(0, 200, 100);
    ImagePanel photoViewer = new ImagePanel();

    public ImageEditor() {

        editMenu.setEnabled(false);
        mainFrame.setSize(1000,750);
        mainFrame.setResizable(true);
        photoViewer.setSize(1000,750);

        openButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        openButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
            fileChooser.setVisible(true);
            int returnValue = fileChooser.showOpenDialog(null);

            if (returnValue == JFileChooser.APPROVE_OPTION) {
                editMenu.setEnabled(true);
                fileChooser.setVisible(false);
                File[] selectedImages = fileChooser.getSelectedFiles();
                photoViewer.updateGraphics(selectedImages);
                photoViewer.repaint();
            }
        });

        exitButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, ActionEvent.CTRL_MASK));
        exitButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            System.exit(0);
        });

        saveButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        saveButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            fileSaver = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
            fileSaver.setVisible(true);
            fileSaver.setMultiSelectionEnabled(false);
             int returnValue = fileSaver.showOpenDialog(null);

            if (returnValue == JFileChooser.APPROVE_OPTION){
                String path = fileSaver.getSelectedFile().getAbsolutePath();
                System.err.println(path);
            }
        });

        rotatePositive.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ActionEvent.CTRL_MASK));
        rotatePositive.addActionListener((java.awt.event.ActionEvent evt) -> {
            photoViewer.positiveRotation();
        });

        rotateNegative.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK));
        rotateNegative.addActionListener((java.awt.event.ActionEvent evt) -> {
            photoViewer.negativeRotation();
        });

        insertLabel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK));
        insertLabel.addActionListener((java.awt.event.ActionEvent evt) -> {
            JFrame adder = new JFrame("Add text");
            adder.setVisible(true);
            adder.setAlwaysOnTop(true);
            adder.setBounds(500, 250, 250, 125);
            adder.setLayout(new GridLayout(2,2));
            JLabel labelText = new JLabel("Text: ");
            JTextField label = new JTextField();
            JButton cancelButton = new JButton("Cancel");
            JButton confirmButton = new JButton("OK");
            adder.add(labelText);
            adder.add(label);
            adder.add(cancelButton);
            adder.add(confirmButton);
            confirmButton.addActionListener((java.awt.event.ActionEvent evt2) -> {
                adder.setVisible(false);
                mainFrame.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        photoViewer.label=label.getText();
                        photoViewer.lx=e.getX();
                        photoViewer.ly=e.getY();
                        photoViewer.repaint();
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
                    }
                });

            });

        });

        resizeButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ActionEvent.CTRL_MASK));
        resizeButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            JFrame resizer = new JFrame("Resize");
            resizer.setVisible(true);
            resizer.setAlwaysOnTop(true);
            resizer.setBounds(500, 250, 250, 125);
            resizer.setLayout(new GridLayout(3,2));
            JLabel widthLabel = new JLabel("Width: ");
            JLabel heightLabel = new JLabel("Height: ");
            JFormattedTextField widthField = new JFormattedTextField();
            JFormattedTextField heightField = new JFormattedTextField();
            JButton cancelButton = new JButton("Cancel");
            JButton confirmButton = new JButton("OK");
            widthField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
            heightField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
            resizer.add(widthLabel);
            resizer.add(widthField);
            resizer.add(heightLabel);
            resizer.add(heightField);
            resizer.add(cancelButton);
            resizer.add(confirmButton);

            cancelButton.addActionListener((java.awt.event.ActionEvent evt2) -> {resizer.setVisible(false);});
            confirmButton.addActionListener((java.awt.event.ActionEvent evt2) -> {photoViewer.selectedImg = photoViewer.resize(photoViewer.selectedImg, Integer.parseInt(widthField.getText().replace(".", "")), Integer.parseInt(heightField.getText().replace(".", "")));});  //WORKING

        });

        adjustBrightnessButton.setEnabled(false);

        //adjustBrightnessButton.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, ActionEvent.CTRL_MASK));
        /*adjustBrightnessButton.addActionListener((java.awt.event.ActionEvent evt) -> {
            JFrame slider = new JFrame("Brightness");
            slider.setSize(300,100);
            slider.setAlwaysOnTop(true);
            slider.add(brightnessSlider);
            slider.setVisible(true);
            brightnessSlider.addMouseListener(new MouseListener(){

                @Override
                public void mouseClicked(MouseEvent e){}

                @Override
                public void mousePressed(MouseEvent e){}

                @Override
                public void mouseReleased(MouseEvent e) {
                    photoViewer.adjustBrightness(Float.valueOf((brightnessSlider.getValue()))/100);     //Less memory usage
                }

                @Override
                public void mouseEntered(MouseEvent e){}

                @Override
                public void mouseExited(MouseEvent e) {}
            });
            brightnessSlider.addChangeListener((ChangeEvent e) -> {
                photoViewer.adjustBrightness(Float.valueOf((brightnessSlider.getValue()))/100);         //Real time update, but VERY HEAVY
            });
        });*/

        menu.add(fileMenu);
        menu.add(editMenu);

        fileMenu.add(openButton);
        fileMenu.add(saveButton);
        fileMenu.add(saveAsButton);
        fileMenu.add(exitButton);

        editMenu.add(resizeButton);
        editMenu.add(rotatePositive);
        editMenu.add(rotateNegative);
        editMenu.add(adjustBrightnessButton);
        editMenu.add(insertLabel);

        menu.setVisible(true);
        mainFrame.setJMenuBar(menu);

        mainFrame.setVisible(true);
        mainFrame.add(photoViewer);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
        ImageEditor img = new ImageEditor();
    }
}

And here is my second file, the photoViewer

public class ImagePanel extends JPanel{

    public ArrayList<File> selectedFiles = new ArrayList<>();
    BufferedImage selectedImg;
    public int rotation = 0;
    boolean flag=true;
    String label="";
    int lx,ly;

    @Override
    public void paintComponent(Graphics g){

        super.paintComponent(g);

        AffineTransform at = new AffineTransform();

        at.translate(getWidth() / 2, getHeight() / 2);

        at.rotate(Math.toRadians(45*rotation));

        if(flag && !selectedFiles.isEmpty()){
            selectedImg = loadImage(selectedFiles.get(0));      //WORKING
            flag = false;
        }

        if(!selectedFiles.isEmpty())
            at.translate(-selectedImg.getWidth() / 2, -selectedImg.getHeight() / 2);

        Graphics2D g2d = (Graphics2D) g;

        if(!label.isEmpty()){
            System.err.println("PHOTODONE");
            g2d.drawString("vdsavdfvdfsvfdsvdfs", 0, 0);        //PROVARE A UTILIZZARE JFRAME.CREATEIMAGE PER COPIARE L'IMMAGINE E SCRIVERE SU QUELLA
            label="";
            repaint();
        }

        g2d.drawImage(selectedImg, at, null);

    }    
    public BufferedImage loadImage(File fileName){

        BufferedImage bfImg = null;

        try{
            bfImg = ImageIO.read(fileName);
            if(bfImg.getWidth() > 800)
                bfImg = resize(bfImg, 800, bfImg.getHeight());
            if(bfImg.getHeight() > 750)
                bfImg = resize(bfImg, bfImg.getWidth(), 750);
        }
        catch(IOException e){
            System.err.println("Error loading image");
        }
        return bfImg;
    }
    public void updateGraphics(File[] files){
        selectedFiles = new ArrayList<>(Arrays.asList(files));
    }
    public void positiveRotation(){
        if(rotation < 8){
            rotation++;
            repaint();
        }                               //BOTH WORKING
        else{
            rotation=1;
            repaint();
        }
    }
    public void negativeRotation(){
        if(rotation > -8){
            rotation--;
            repaint();
        }
        else{
            rotation=-1;
            repaint();
        }
    }
    /*public void adjustBrightness(Float sliderValue){        
        RescaleOp rop = new RescaleOp(sliderValue, 0, null);
        rop.filter(selectedImg, selectedImg);                   //WORKING
        repaint();
    }*/
    public BufferedImage resize(BufferedImage img, int width, int height) { 
        Image tmp = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage dimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = dimg.createGraphics();
        g2d.drawImage(tmp, 0, 0, null);
        g2d.dispose();

        return dimg;
    }
}

The edit i did today was change some things in the main file (and from the photoViewer, since they are bound)

I changed this

selectedImages=fileChooser.getSelectedFile().getAbsolutePath()
photoViewer.updateGraphics(selectedImages);

To this

File[] selectedImages = fileChooser.getSelectedFiles();
photoViewer.updateGraphics(selectedImages);

Then i modified both files to make sure that the functions will accept a FIle array, but if i delete all unrelated code, comment the photoViewer callings, comment ALL the photoViewer file, it still won't work. What is happening?

ADDON

I figured out how to make NetBeans show the stack trace, here it is

    java.lang.NullPointerException
    at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.mouseWheelMoved(BasicScrollPaneUI.java:866)
    at org.netbeans.modules.editor.lib2.view.DocumentViewOp.mouseWheelMoved(DocumentViewOp.java:1523)
    at org.netbeans.modules.editor.lib2.view.DocumentViewOp$MouseWheelDelegator.mouseWheelMoved(DocumentViewOp.java:1657)
    at java.awt.AWTEventMulticaster.mouseWheelMoved(AWTEventMulticaster.java:532)
    at java.awt.Component.processMouseWheelEvent(Component.java:6623)
    at java.awt.Component.processEvent(Component.java:6307)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventToSelf(Container.java:2318)
    at java.awt.Component.dispatchMouseWheelToAncestor(Component.java:5044)
    at java.awt.Component.dispatchEventImpl(Component.java:4778)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4554)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159)
[catch] 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)
TheRev
  • 124
  • 13
  • Less code please. Please create and post a [mcve] -- the minimal code necessary to compile, run and reproduce the problem, and nothing else. – Hovercraft Full Of Eels Jan 05 '18 at 12:17
  • 1
    Not a solution for you current problem, but you should really consider using some kind of version control (e.g. git) as it will help you with "yesterday it worked - problems". If you are using NetBeans as your IDE you might be able to restore your code with the help of the "History" button. – sknt Jan 05 '18 at 12:18
  • @Skynet makes a good point, and most modern IDE's have this capability. I know that Eclipse can do this as well. – Hovercraft Full Of Eels Jan 05 '18 at 12:19
  • It is called [Local History](https://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2FgettingStarted%2Fqs-55.htm) in Eclipse. – Malte Hartwig Jan 05 '18 at 12:22
  • @Skynet Thanks, i was able to revert to yesterday, but i still can't see anything, but i've noticed something: NetBeans keeps giving me an Unexpected Error: Null pointer exception AFTER i've terminated the build, but i can't seem to find this null pointer... – TheRev Jan 05 '18 at 12:22
  • Please look at [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Hovercraft Full Of Eels Jan 05 '18 at 12:30
  • ... and if you're getting a NullPointerException (NPE) that information is critical to your problem and to your question. You should post the exception stacktrace and indicate which line(s) throw the exception (as the stacktrace will show you). – Hovercraft Full Of Eels Jan 05 '18 at 12:32
  • Please check out [some similar questions](https://www.google.com/search?q=site%3Ahttps%3A%2F%2Fstackoverflow.com%2F+java+netbeans+unexpected+exception+nullpointerexception). Maybe one can help you. – Hovercraft Full Of Eels Jan 05 '18 at 12:39
  • First of all: start your GUI on the EDT. Use `SwingUtilities.invokeLater(() -> new ImageEditor(); );` in your main method. – Hovercraft Full Of Eels Jan 05 '18 at 12:50

0 Answers0