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!


Sign In
Create Account

Back to top









