-1

i have made an application that loads image from specific directory, loads all images in stack, when i am clicking on next button next image loads. i have also added JSlider that change Brightness of Loaded Image but it doesn't work. i don't know why but i am not getting exact problem.

my code :

public class PictureEditor extends JFrame 
{ 
    private static final long serialVersionUID = 6676383931562999417L; 

    String[] validpicturetypes = {"png", "jpg", "jpeg", "gif"}; 
    Stack<File> pictures ;
    JLabel label = new JLabel(); 
    BufferedImage a = null; 
    float fval=1;

    public PictureEditor() 
    { 
        JPanel panel = new JPanel(); 
        JMenuBar menubar = new JMenuBar(); 
        JMenu toolsmenu = new JMenu("Tools"); 


        final JSlider slider1;
            slider1 = new JSlider(JSlider.HORIZONTAL,0,4,1);
            slider1.setToolTipText("Slide To Change Brightness");
            slider1.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
            slider1.setMajorTickSpacing(1);
            slider1.setPaintLabels(true);
            slider1.setPaintTicks(true);
            slider1.setPaintTrack(true);
            slider1.setAutoscrolls(true);
            slider1.setBounds(50, 55, 200, 50);

            slider1.addChangeListener(new ChangeListener() 
            {

                @Override
                public void stateChanged(ChangeEvent e) 
                {
                    System.out.println("Before");
                    fval=slider1.getValue();
                    chgBrt(fval);
                    repaint();
                }
            });

        TitledBorder title;
        title = BorderFactory.createTitledBorder("Operations");
        title.setTitleJustification(TitledBorder.LEFT);

        JButton RT90 = new JButton("");
        JButton RT180 = new JButton("");
        JButton RTM90 = new JButton("");
        JButton RTM180 = new JButton("");
        JButton NEXT = new JButton("");
        Image img = null;
        Image imgn = null;

        try 
        {
            img = ImageIO.read(getClass().getResource("/images/images_Horizontal.png"));
            imgn = ImageIO.read(getClass().getResource("/images/next12.png"));
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        RT90.setIcon(new ImageIcon(img));
        RT180.setIcon(new ImageIcon(img));
        RTM90.setIcon(new ImageIcon(img));
        RTM180.setIcon(new ImageIcon(img));
        NEXT.setIcon(new ImageIcon(imgn));

        JPanel buttonPane = new JPanel();

        buttonPane.add(slider1);

        buttonPane.add(Box.createRigidArea(new Dimension(250,0)));

        buttonPane.setBorder(title);
        buttonPane.add(RT90);
        buttonPane.add(RT180);
        buttonPane.add(RTM90);
        buttonPane.add(RTM180);


        buttonPane.add(Box.createRigidArea(new Dimension(250,0)));


        NEXT.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) 
            {           
                nextImage();
            }
        });
        buttonPane.add(NEXT);
        getContentPane().add(buttonPane, BorderLayout.SOUTH);

        final File dir = new File("");
        final JFileChooser file;
        file = new JFileChooser();
        file.setCurrentDirectory(dir);
        file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        file.showOpenDialog(panel);
        String path = file.getSelectedFile().getAbsolutePath();
        System.out.println(path);
        pictures= getFilesInFolder(path.toString()); 

        Action nextpictureaction = new AbstractAction("Next Picture") 
        { 
            private static final long serialVersionUID = 2421742449531785343L; 

            @Override 
            public void actionPerformed(ActionEvent e) 
            {   
                nextImage();    
            }

        }; 
        setJMenuBar(menubar); 
        menubar.add(toolsmenu); 
        toolsmenu.add(nextpictureaction); 

        panel.add(label,BorderLayout.CENTER); 
        add(panel);     

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setLocationByPlatform(true); 
        setSize(1000, 700); 
        setTitle("PictureEditor"); 
        setVisible(true); 
    } 

    public Stack<File> getFilesInFolder(String startPath) 
    { 
        File startFolder = new File(startPath); 
        Stack<File> picturestack = new Stack<File>(); 

        String extension; 
        int dotindex; 

        // Go through the folder 
        for (File file : startFolder.listFiles()) 
        { 
            extension = ""; 
            dotindex = file.getName().lastIndexOf('.'); // Get the index of the dot in the filename 

            if (dotindex > 0) 
            { 
                extension = file.getName().substring(dotindex + 1);             
                // Iterate all valid file types and check it 
                for (String filetype : validpicturetypes) 
                { 
                    if (extension.equals(filetype))
                    { 
                        picturestack.add(file); 
                    } 
                } 
            } 
        } 
    return picturestack; 
    } 

    public void nextImage()
    {       

        try 
        {
            a=ImageIO.read(pictures.pop().getAbsoluteFile());       
        } 
        catch (IOException e1) 
        {
            e1.printStackTrace();
        }
        final ImageIcon image = new ImageIcon(a);

        label.setIcon(image);
        repaint();
    }
    @SuppressWarnings("null")
    public void chgBrt(float f)
    {
          Graphics g = null;
          Graphics2D g2d=(Graphics2D)g;
          try
          {  
              BufferedImage dest=changeBrightness(a,(float)fval);          
              System.out.println("Change Bright");

              int w = a.getWidth();
              int h = a.getHeight();              
              g2d.drawImage(dest,w,h,this);      

              ImageIO.write(dest,"jpeg",new File("/images/dest.jpg"));
              System.out.println("Image Write");
          }
          catch(Exception e)
          {
                e.printStackTrace();
          }
    }

    public BufferedImage changeBrightness(BufferedImage src,float val)
    {
         RescaleOp brighterOp = new RescaleOp(val, 0, null);
         return brighterOp.filter(src,null); //filtering
    }

    public static void main(String[] args) 
    { 
        javax.swing.SwingUtilities.invokeLater(new Runnable() 
        { 
            public void run() 
            { 
                new PictureEditor(); 
            } 
        }); 
    } 
}

anyone who can guide me and tell me where i am wrong ??

Roman C
  • 49,761
  • 33
  • 66
  • 176
Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63
  • 6
    a) don't dump the complete bunch of line onto us, instead show a SSCCE b) Please learn java naming conventions and stick to them. That said: what do you expect when painting on a null graphics? – kleopatra Aug 15 '13 at 10:48
  • @kleopatra - sorry sir, i will keep in mind that from next time. painting a null graphics means ? it shows an error at `g2d.drawImage(dest,w,h,this);` code – Java Curious ღ Aug 15 '13 at 10:54
  • I bet it's NullPointerException, isn't it? Look closely at your code to see why (hint: look at the location where you assign it) btw: be careful when assuming the gender of anybody, dear lady – kleopatra Aug 15 '13 at 10:59
  • @kleopatra - yes that's `NullPointerException`, and sorry for that gender mistake. m also male. – Java Curious ღ Aug 15 '13 at 11:06
  • @kleopatra - if you don't mine then will you tell me where i am wrong ? – Java Curious ღ Aug 15 '13 at 11:09
  • 2
    You get NPE because you explicitly set the graphics to `null`. Casting it to `Graphics2d` does not magically turn it to something else. You use elsewhere a label to display images - do the same for the modified image. (And, preferrably, do the heavy brightness change in the background using a SwingWorker). – kiheru Aug 15 '13 at 11:39
  • @kiheru - will you tell me what i will have to do for to solve out that problem. – Java Curious ღ Aug 15 '13 at 11:59
  • 2
    The simplest is to make ImageIcon of it and put it in a JLabel like you do elsewhere. There's no need for custom painting at all. You can use JLabel.setIcon() if you want to replace an image in an existing JLabel. – kiheru Aug 15 '13 at 12:06
  • @kiheru-yes i have changed brightness, thanx i have first initialize Graphics g and then after it works now i have to just replace jlabel image with new brightness changed image. i am trying on it. and thank you s much for guiding me that where i am wrong... – Java Curious ღ Aug 15 '13 at 12:11
  • @kiheru-yes yes yes i have done it. thank you again. i have repainted label with new bright changed image. – Java Curious ღ Aug 15 '13 at 12:22
  • 1
    See also this related [example](http://stackoverflow.com/a/10208380/230513). – trashgod Aug 15 '13 at 13:45
  • @trashgod - thank you so much. i have successfully implemented that one. thank for your wonderful guidance. – Java Curious ღ Aug 16 '13 at 06:25

1 Answers1

1

You may be able to adapt the approach shown in this RescaleTest, which varies the scaleFactor passed to RescaleOp to a value between zero and twice the slider's maximum.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    yes i have seen that example which link you have sent to me. thank you so much, i have got many new thing in that by using `scaleFactor`. – Java Curious ღ Aug 16 '13 at 09:48