Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-03-2011, 03:16 AM   PM User | #1
Zerkky
New to the CF scene

 
Join Date: May 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Zerkky is an unknown quantity at this point
Changing Effects on Java Image- Help!

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!
Zerkky is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:21 PM.


Advertisement
Log in to turn off these ads.