Image editor

problem with refreshing picture after button pressed

Page 1 of 1

7 Replies - 13868 Views - Last Post: 06 May 2008 - 04:15 PM Rate Topic: -----

#1 toph786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 08

Image editor

Post icon  Posted 05 May 2008 - 07:32 PM

Right now I'm creating a picture editor with 4 buttons. By pressing the buttons they will make the picture darker, lighter, negative, and black and white. When I run this code, everything works fine, but when I press the buttons, the picture does not change. The buttons call on the function and change the value in the array that I set up, but the picture never changes in the frame. I think it has something to do with the show() function when I say: frame.add(getJLabel()...)

Could anyone please help me out with this?

THANKS!!



import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;



public final class PicEdit extends JComponent implements ActionListener
{
	public BufferedImage picture1;
	public JFrame frame;
	public String filename;
	
	//create the buttons
	JButton button1 = new JButton("Black & White");
	JButton button2 = new JButton("Brighter");
	JButton button3 = new JButton("Darker");
	JButton button4 = new JButton("Negative");
	
	

public int length, height;
public Color[][] picArray;


	public PicEdit(int w, int h)
	{	
	picture1 = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
	filename = w + "-by-" + h;
	}
	
	
//get the picture from the class file
	public PicEdit(String filename) {
		this.filename = filename;
		try {
			// try to read from file in working directory
			File file = new File(filename);
			if (file.isFile()) {
				picture1 = ImageIO.read(file);
			}

			// now try to read from file in same directory as this .class file
			else {
				URL url = getClass().getResource(filename);
				if (url == null) { url = new URL(filename); }
				picture1 = ImageIO.read(url);
			}
		}
		catch (IOException e) {
			// e.printStackTrace();
			throw new RuntimeException("Could not open file: " + filename);
		}

		// check that image was read in
		if (picture1 == null) {
			throw new RuntimeException("Invalid image file: " + filename);
		}
	}

	
	//create the picture by reading it from the file
  
	public PicEdit(File file) {
		try { picture1 = ImageIO.read(file); }
		catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("Could not open file: " + file);
		}
		if (picture1 == null) {
			throw new RuntimeException("Invalid image file: " + file);
		}
	}


	
//put the picture in a label
public JLabel getJLabel()
{
	if(picture1 == null)
	{
		return null;
	}
	ImageIcon icon = new ImageIcon(picture1);
	
	return new JLabel(icon);
}



public void show()
	{
	
	if(frame==null){
		frame = new JFrame();
	
	JPanel area = new JPanel();
	area.setLayout(new GridLayout(0,1));
	area.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
	
	area.add(button1);
	area.add(button2);
	area.add(button3);
	area.add(button4);

	button1.addActionListener(this);
	button2.addActionListener(this);
	button3.addActionListener(this);
	button4.addActionListener(this);
	
	
	frame.add(area, BorderLayout.EAST);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setTitle("Picture Editor");
	frame.setVisible(true);
	frame.pack();
	frame.add(getJLabel(), BorderLayout.CENTER);
	
	
	}else
	
	{
	
	frame.repaint();
	}
	

	}


//2. Extract the pixels into an array


public int height()
	{
	return picture1.getHeight();
	}

public int length()
	{
	return picture1.getWidth();
	}

public Color get(int i, int j)
	{
	return new Color(picture1.getRGB(i,j));
	}
public void set(int i, int j, Color c)
	{
	if (c == null) { throw new RuntimeException("can't set Color to null"); }
	picture1.setRGB(i,j,c.getRGB());
	}
	
	
	
	// function that turns the picture into a black & white picture	
	public void blackwhite()
	{
		PicEdit p2 = new PicEdit(length,height);
		for(int i=0; i<length; i++)
		{
			for(int j=0; j<height; j++)
			{
				Color c = picArray[i][j];
				int r = c.getRed();
				int g = c.getGreen();
				int b = c.getBlue();
				
				int black = (int)(.3*r + .59*g + .11*b);
				Color bl = new Color(black, black, black);
				p2.set(i, j, bl);
			}
		}
		p2.show();
	}
	

	
	
	// function that makes the picture brighter
	public void brighter()
	{
		PicEdit p2 = new PicEdit(length,height);
		for(int i = 0; i<length; i++)
		{
			for(int j = 0; j<height; j++)
			{
				Color c = picArray[i][j];
				int r = c.getRed();
				int g = c.getGreen();
				int b = c.getBlue();
				
				int brighterRed = (int)(Math.sqrt((r/255))*255);
				int brighterGreen = (int)(Math.sqrt((g/255))*255);
				int brighterBlue = (int)(Math.sqrt((b/255))*255);
				
				Color bright = new Color(brighterRed, brighterGreen, brighterBlue);
				p2.set(i, j, bright);
				
				
			}
		}
		p2.show();
	}
	
	//function that makes the picture darker	
	public void darker()
	{
		for(int i = 0; i<length; i++)
		{
			for(int j = 0; j<height; j++)
			{
				
				Color c = picArray[i][j];
				int r = c.getRed();
				int g = c.getGreen();
				int b = c.getBlue();
				
				//now add the code to darken the pixels 
				
				
			}
		}
	}

	
	//function that turns the picture into the negative
	//ALSO BELOW IS HOW I ORIGINALLY DID IT
	public void negative()
	{
		for(int i=0; i<length; i++)
		{
			for(int j=0; j<height; j++)
			{
				Color c = picArray[i][j];
				int r = c.getRed();
				int g = c.getGreen();
				int b = c.getBlue();
				
				int negR = 255 - r;
				int negG = 255 - g;
				int negB = 255 - b;
				
				Color NegColor = new Color(negR, negG, negB);
				picArray[i][j] = NegColor;
				
				
			}
		}
	}
	
	
	
	//from page 553 in Java for Dummies
	public void actionPerformed(ActionEvent pushed)
	{
		if(pushed.getSource() == button1)
		{
			blackwhite();
			frame.repaint();
		}
		
		if(pushed.getSource() == button2)
		{
			brighter();
			frame.repaint();
		}
		
		if(pushed.getSource() == button3)
		{
			darker();
			frame.repaint();	
		}
		
		if(pushed.getSource() == button4)
		{
			negative();
			frame.repaint();
		}
	}
	
	
	
	//main argument
	public static void main(String[] args)
	{
		
		PicEdit pic = new PicEdit(args[0]);
		pic.show();
		
		
	}
	
	
}









This post has been edited by toph786: 05 May 2008 - 07:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Image editor

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Image editor

Posted 05 May 2008 - 09:09 PM

Many flaws in your design.... (my opinion)

OK your are not saving height and length in your first contructor so when you create a new PicEdit(length, width) you create it 0,0

When you call your constructor width (lenght, height)
your pixelArray is not defined

Actually I don't see why you have two constructors and why your show() method re-creates the JPanel, the buttons and the JLabel everytime:

Your first constructor is fine for reading the file. Let this contructor also create the JFrame, the Panel, place the buttons in it and the JLabel with the Icon. Save the JLabel in an instance variable as the BufferedImage.
Get rid of your show method().

When a button is clicked, don't call a constructor again, you have all you need to perform your image operations.
Create a new icon with the transformed image.
and do label.setIcon(newIcon);

no need to repaint(), redraw(), show()
Was This Post Helpful? 0
  • +
  • -

#3 toph786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 08

Re: Image editor

Posted 06 May 2008 - 12:44 PM

ok, so I changed things around a little bit. I initialized the array in the constructor. How can i paint the picture of the new array onto the frame that I created?

Thanks again for your help







import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

import java.awt.image.BufferedImage;



public final class photo extends JComponent implements ActionListener
{
	public BufferedImage picture1;
	public JFrame frame;
	public String filename;
	
	//create the buttons
	JButton button1 = new JButton("Black & White");

	
	
public int[][] picArray;
public int width, height;





	public photo(int w, int h)
	{	
	picture1 = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
		System.out.println(picture1.getWidth());
	filename = w + "-by-" + h;
	}
	
	
//get the picture from the class file
	public photo(String filename) {
		this.filename = filename;
		try {
			// try to read from file in working directory
			File file = new File(filename);
			if (file.isFile()) {
				picture1 = ImageIO.read(file);
			}

			// now try to read from file in same directory as this .class file
			else {
				URL url = getClass().getResource(filename);
				if (url == null) { url = new URL(filename); }
				picture1 = ImageIO.read(url);
			}
		}
		catch (IOException e) {
			// e.printStackTrace();
			throw new RuntimeException("Could not open file: " + filename);
		}

		// check that image was read in
		if (picture1 == null) {
			throw new RuntimeException("Invalid image file: " + filename);
		}
		

		picArray = new int[width()][height()];
		for (int i=0; i<width(); i++)
		{
				for (int j = 0; j<height(); j++)
				{
						picArray[i][j] = picture1.getRGB(i, j);
						int r = (picArray[i][j] & (0xff0000)) >> 16;
						int g = (picArray[i][j] & (0x00ff00)) >> 8;
						int b = (picArray[i][j] & (0x0000ff));

					   
				}
		 }
	 }
		
   public int height()
		{
   return picture1.getHeight();
		}

	public int width()
		{
		return picture1.getWidth();
		}	   
		
	public void blackwhite()
	{
	
		for(int i=0; i<width(); i++)
		{
			for(int j=0; j<height(); j++)
			{
				int p = picArray[i][j];
				int r = (picArray[i][j] & (0xff0000)) >> 16;
				int g = (picArray[i][j] & (0x00ff00)) >> 8;
				int b = (picArray[i][j] & (0x0000ff));
				
				
				
				int black = (int)(.3*r + .59*g + .11*b);
			
				picArray[i][j] = black;
				System.out.println(picArray[j][i]);
			}
		}
	}
	
	public void actionPerformed(ActionEvent pushed)
	{
		if(pushed.getSource() == button1)
		{
			blackwhite();
			frame.repaint();
		}
	}
	
	
	
	
	public void show()
	{
	
	if(frame==null){
		frame = new JFrame();
	
	JPanel area = new JPanel();
	area.setLayout(new GridLayout(0,1));
	area.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
	
	area.add(button1);
	

	button1.addActionListener(this);
	
	
	
	frame.add(area, BorderLayout.EAST);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setTitle("Chris Thompson's Picture Editor");
	frame.setVisible(true);
	frame.pack();
	//frame.add(paintComponent(), BorderLayout.CENTER);
	
	
	}else
	
	{
	
	frame.repaint();
	}
	

	}
	
	
	
	
	//main argument
	public static void main(String[] args)
	{
		
		photo pic = new photo(args[0]);
		pic.show();
		
		
	}
	
	  

	}



Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Image editor

Posted 06 May 2008 - 01:51 PM

OK your code is a lot shorter... to here is the remake

import javax.swing.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.net.URL;
import java.awt.*;

import java.awt.image.BufferedImage;


//ok your class photo extends JComponent but it is JComponent
//that contains nothing so we'll male it a JFrame 
public final class photo extends JFrame implements ActionListener
{
	public BufferedImage picture1;
	public String filename;

	//create the buttons
	JButton button1 = new JButton("Black & White");
	// the label displaying the image
	JLabel label = new JLabel("");

	public int[][] picArray;
	public int width, height;


//	get the picture from the class file
	public photo(String filename) {
		super("Black and White editor V1.0");
		this.filename = filename;
		try {
			// try to read from file in working directory
			// (will also work if the user passes the directory in the filename String)
			File file = new File(filename);
			if (file.isFile()) {
				picture1 = ImageIO.read(file);
			}

			// now try to read from file i
			else {
				URL url = getClass().getResource(filename);
				if (url == null) { url = new URL(filename); }
				picture1 = ImageIO.read(url);
			}
		}
		catch (IOException e) {
			// e.printStackTrace();
			throw new RuntimeException("Could not open file: " + filename + " Reason: " + e);
		}

		// check that image was read in
		if (picture1 == null) {
			throw new RuntimeException("Invalid image file: " + filename);
		}
		
		// make an Icon from image
		ImageIcon icon = new ImageIcon(picture1);
		// put the Icon in the Label
		label.setIcon(icon);
		width = picture1.getWidth();
		height = picture1.getHeight();

		picArray = new int[width][height];
		for (int i=0; i<width; i++)
		{
			for (int j = 0; j<height; j++)
			{
				picArray[i][j] = picture1.getRGB(i, j);
			}
		}
		// create the panel holding Label and Buttons
		JPanel panel = new JPanel();
		panel.add(label);
		button1.addActionListener(this);
		panel.add(button1);
		// add panel to the frame
		add(panel);
		// make frame visible 
		setSize(300, 300);
		setVisible(true);
	}


	public void blackwhite()
	{

		for(int i=0; i<width; i++)
		{
			for(int j=0; j<height; j++)
			{
				int r = (picArray[i][j] & (0xff0000)) >> 16;
				int g = (picArray[i][j] & (0x00ff00)) >> 8;
				int b = (picArray[i][j] & (0x0000ff));
				// or something like that (not really sure of your algorithm here
				// you will have to << back every colors
				int black = (int)(.3*r + .59*g + .11*b);
				picture1.setRGB(i,j,black);				
				System.out.println(picArray[i][j]);
			}
		}
		// make a new Icon
		ImageIcon icon = new ImageIcon(picture1);
		// put iy in the JLabel
		label.setIcon(icon);
		// no need to repainy(), redraw().... the GUI will handle it because it is in a JLabel
	}

	public void actionPerformed(ActionEvent pushed)
	{
		if(pushed.getSource() == button1)
		{
			blackwhite();
		}
	}


	//main argument
	public static void main(String[] args)
	{
		photo pic = new photo(args[0]);
	}



}




Your algoritm to convert to black is not perfect
you will have to << back the colors before adding them
Was This Post Helpful? 0
  • +
  • -

#5 toph786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 08

Re: Image editor

Posted 06 May 2008 - 02:22 PM

awesome. it's slowly coming together. How would you remedy the situation with the r, g, b. This is the only way i thought it would work, but apparently there is something wrong with this algorithm. Is there any way that you could point me in the right direction?

I also am applying this to make a picture brighter. But I cannot set the RGB values to equal a color. Any suggestions on that? ie:

int brighterRed = (int)(Math.sqrt((r/255))*255);
int brighterGreen = (int)(Math.sqrt((g/255))*255);
int brighterBlue = (int)(Math.sqrt((b/255))*255);
				
int bright = new int (brighterRed, brighterGreen, brighterBlue);
picture1.setRGB(i,j,bright);





thanks again I really appreciate it and have been working hours to try to get this up and running.






public void blackwhite()
	{

		for(int i=0; i<width; i++)
		{
			for(int j=0; j<height; j++)
			{
				int r = (picArray[i][j] & (0xff0000)) >> 16;
				int g = (picArray[i][j] & (0x00ff00)) >> 8;
				int b = (picArray[i][j] & (0x0000ff));
				// or something like that (not really sure of your algorithm here
				// you will have to << back every colors
				int black = (int)(.3*r + .59*g + .11*b);
				picture1.setRGB(i,j,black);				


Your algoritm to convert to black is not perfect
you will have to << back the colors before adding them


This post has been edited by toph786: 06 May 2008 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Image editor

Posted 06 May 2008 - 02:50 PM

View Posttoph786, on 6 May, 2008 - 02:22 PM, said:

awesome. it's slowly coming together. How would you remedy the situation with the r, g, b. This is the only way i thought it would work, but apparently there is something wrong with this algorithm. Is there any way that you could point me in the right direction?

I also am applying this to make a picture brighter. But I cannot set the RGB values to equal a color. Any suggestions on that? ie:


You'll have to shift back... same thing applies to your brighter algorihtm
   int r = (picArray[i][j] & (0xff0000)) >> 16;
   int g = (picArray[i][j] & (0x00ff00)) >> 8;
   int b = (picArray[i][j] & (0x0000ff));
 
   // don't know where you pickup that thow
   r = (int)  ((double) r) * 0.3;
   g = (int) ((double) g) * 0.59; 
   b = (int) ((double) b) * 0.11;

   int black = r << 8;
   black += g;
   black = black << 8;
   black += b;

   picture1.setRGB(i,j,black);				




Was This Post Helpful? 0
  • +
  • -

#7 toph786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 08

Re: Image editor

Posted 06 May 2008 - 03:33 PM

i'm still getting a green tint for that. I know you said you didn't know where I got those algorithms, but they are the same ones that I did from a VB program I made, so they should work. Is there somewhere else that I should look?

thanks again I know this can be quite frustrating




 public void blackwhite()
	{
	
		for(int i=0; i<width; i++)
		{
			for(int j=0; j<height; j++)
			{
				int p = picArray[i][j];
				int r = (picArray[i][j] & (0xff0000)) >> 16;
				int g = (picArray[i][j] & (0x00ff00)) >> 8;
				int b = (picArray[i][j] & (0x0000ff));
				
			
				  
				
				// don't know where you pickup that tho
				r = (int)  (((double) r) * 0.3);
				g = (int) (((double) g) * 0.59); 
				b = (int) (((double) b) * 0.11);
				
				int black = r+g+b;
				black = r << 8;
				black += g;
				black = black << 8;
				black += b;
				picture1.setRGB(i,j,black);
				
			}
		}




This post has been edited by toph786: 06 May 2008 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Image editor

Posted 06 May 2008 - 04:15 PM

Doesn't make sense

	int black = r+g+b;
// colors are stored
0x00112233
// where 11:r  22:g  33:b
// you split tham by a mask with 0x0000FF and you shifted them



So you have to shift them back

May be in your VB code you where not doing the shitd
r = rgb & 0xFF0000;
g = rgb & 0xFF00;
b = rgb & 0xFF;

WITHOUT the shift

Then after you modified r, g and b
black = r + g + b;
would work

You are telling me that:

int p = picArray[i][j];
int r = (picArray[i][j] & (0xff0000)) >> 16;
int g = (picArray[i][j] & (0x00ff00)) >> 8;
int b = (picArray[i][j] & (0x0000ff));

// not doing modification
			
int black = r+g+b;



black should be == p ???

If you have problems whith the shifts... let Java do it for you
That should work :-)

	for(int i=0; i<width; i++)
	{
		for(int j=0; j<height; j++)
		{
			Color color = new Color(picArray[i][j]);
			int r = color.getRed();
			int g = color.getGreen();
			int b = color.getBlue();
			// do whatever you have to do
			// with r, g and B
			//
			// then
			color = new Color(r, g, b);
						picture1.setRGB(i, j, color.getRGB());				
		}
	}



Then you can make a general method to which you just pass the delta for the 3 if they are all calculated the same way....

void updateColor(double deltaR, double deltaG, double deltaB) {
...
... calculations with the previous loop
}

so now you blackAndWhite method can:

updateColor(0.3, 0.59, 0.11);

This post has been edited by pbl: 06 May 2008 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1