Changing Effects on Java Image- Help!

Discussion in 'Java' started by Zerkky, May 3, 2011.

  1. Zerkky

    Zerkky New Member

    Joined:
    May 3, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Well, I'm sure that I am the 1000th person to ask a nooby question on this part of Hack Forums. Simply put, I need help doing some image processing in Java with Eclipse. I have created enough code so that my image opens up into a GUI and 4 buttons show up. My professor has started me with the 'negative' image effect that works correctly, but I can't seem to get other things to work (such as flipping the image horizontally or making the image go grayscale/black and white). If anyone can help me get started in the right direction or know a solution, that would be great. Below is the code, and, as you can tell, only the negativeButton works. The rest of the code under buttons such as bwButton, flipButton1, and flipButton2 is either copy/pasted or completely wrong as I have been playing around with no success.

    Code:
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    
    public class ImageProcessing extends JFrame
    {
        private Container contents;
        private JButton negativeButton, bwButton, flipButton1, flipButton2;
        private JLabel label;
        private    BufferedImage img;
            
        public ImageProcessing()
        {
            super("title");
            
            try {
            img = ImageIO.read(new File("msm.jpg")); }
            catch (IOException ex) {}
            
            contents = getContentPane();
            contents.setLayout(new FlowLayout());
            
            negativeButton = new JButton("Negative");
            bwButton = new JButton("Black and White");
            flipButton1 = new JButton("Flip Horizontally");
            flipButton2 = new JButton("Flip Vertically");
            label = new JLabel(new ImageIcon(img));
            
            contents.add(negativeButton);
            contents.add(bwButton);
            contents.add(flipButton1);
            contents.add(flipButton2);
            contents.add(label);
                    
            setSize(400, 400);
            setVisible(true);
            
            negativeButton.addActionListener(new negativeButtonHandler());
            bwButton.addActionListener(new bwButtonHandler()); 
            flipButton1.addActionListener(new flipButton1Handler());
            flipButton2.addActionListener(new flipButton2Handler());
        }
    
        // Code for processing the image into a negative effect.
        private class negativeButtonHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
            double[] d = new double[3];
            WritableRaster r = (WritableRaster) img.getData();
                
            for (int i=0; i<img.getWidth(); i++)
            {
                for (int j=0; j<img.getHeight(); j++)
                {
                    d = r.getPixel(i,j,d);
                    d[0] = 255-d[0];
                    d[1] = 255-d[1];
                    d[2] = 255-d[2];
                    r.setPixel(i,j,d);
                }
            }
            img.setData(r);
            repaint();
            }
        }
        
        // Code for processing the image into a black and white effect.
        private class bwButtonHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
            double[] d = new double[3];
            WritableRaster r = (WritableRaster) img.getData();
                
            for (int i=0; i<img.getWidth(); i++)
            {
                for (int j=0; j<img.getHeight(); j++)
                {
                    d = r.getPixel(i,j,d);
                    d[0] = (128*d[0])/255;
                    d[1] = (128*d[0])/255;
                    d[2] = (128*d[1])/255;
                    r.setPixel(i,j,d);
                }
            }
            img.setData(r);
            repaint();
            }
        }
        
        private class flipButton1Handler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
            double[] d = new double[3];
            WritableRaster r = (WritableRaster) img.getData();
                
            for (int i=0; i<img.getWidth(); i++)
            {
                for (int j=0; j<img.getHeight(); j++)
                {
                    d = r.getPixel(i,j,d);
                    d[0] = 128;
                    d[1] = 128;
                    d[2] = 128;
                    r.setPixel(i,j,d);
                }
            }
            img.setData(r);
            repaint();
            }
        }
            
        private class flipButton2Handler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
            double[] d = new double[3];
            WritableRaster r = (WritableRaster) img.getData();
                
            for (int i=0; i<img.getWidth(); i++)
            {
                for (int j=0; j<img.getHeight(); j++)
                {
                    d = r.getPixel(i,j,d);
                    d[0] = 128+d[0];
                    d[1] = 128+d[1];
                    d[2] = 128+d[2];
                    r.setPixel(i,j,d);
                }
            }
            img.setData(r);
            repaint();
            }
        }
        
        public static void main(String[] args)
        {
            ImageProcessing bg = new ImageProcessing();
            bg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    I think the "d[0] = 255-d[0]", "d[1] = ...", etc section is the part that needs to be changed to change the image to the desired output, but I'm not too sure. Thanks in advance!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice