Color Change Application in Java

 

 

As you can see in the above pictures, this application produces two windows (JFrames in Java). One window holds the image to be manipulated, and the other the controls for adjusting pixel colors in the first window. In a previous blog I showed a color changing program that had hardcoded values for a onetime change of a picture's colors. In this blog I show a program that can dynamically adjust shades of red, green and blue with slider controls. The next step in this program is two fold: (1) add a drag and drop feature where you can drag and drop multiple pictures into the image window, and (2) add a control for selecting which pictures are affected by the slider controls.  E.g., I might put a picture of a pool in the image window and then put a picture of a gate around it. Then I could dynamically change the color of the gate, so I could decide what color gate I liked around the pool.

I just decided to add this page to the blog section as well as the programming section. I didn't put a whole lot of story or comments behind the code on this page so I thought it should be listed on the program links page only. However, this is part of my blog series on how an interviewer's question can get me to write some code on my own. Not getting paid to write some code means it'll only get written on days where I feel like it, and it frequently takes a back burner to more pressing events; e.g., painting the walls in my home.

HERE'S THE CODE 

Back To My BLOG Page

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;

public class ColorChange extends Component

 BufferedImage bufferedImage;

 public ColorChange(String fileName)
{
File file;
try 
{
file = new File(fileName);
bufferedImage = ImageIO.read(file);

catch (Exception e) 
{
System.out.println("unable to load image file");
System.exit(0);



public void paint(Graphics g)
{
int width = 512;
int height = width * (bufferedImage.getHeight())/(bufferedImage.getWidth());
g.drawImage(bufferedImage, 0, 0, width, height, null);
}

public static void main(String s[]) 
{
JFrame f = new JFrame("ColorChange Image");
f.addWindowListener(new WindowAdapter() 
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
f.setPreferredSize(new Dimension(512, 384));
final ColorChange cc = new ColorChange(s[0]);
f.add(cc);
f.pack();
f.setVisible(true);

JSliderFrame controls = new JSliderFrame(cc);
}
}

class JSliderFrame extends JFrame {
static int oldRed = 0;
static int oldGreen = 0;
static int oldBlue = 0;
public JSliderFrame(final ColorChange cc) {
super("Color Controls");
Container content = getContentPane();
content.setBackground(Color.white);

final JSliderX slider1 = new JSliderX(cc);
slider1.setMinimum(0);
slider1.setMaximum(255);
slider1.setValue(0);
slider1.setBorder(BorderFactory.createTitledBorder
("RED"));
slider1.setMajorTickSpacing(50);
slider1.setMinorTickSpacing(15);
slider1.setPaintTicks(true);
slider1.setPaintLabels(true);
slider1.setSnapToTicks(true);
slider1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
slider1.change_colorR(evt);

} }
);
content.add(slider1, BorderLayout.NORTH);

final JSliderX slider2 = new JSliderX(cc);
slider2.setMinimum(0);
slider2.setMaximum(255);
slider2.setValue(0);
slider2.setBorder(BorderFactory.createTitledBorder
("GREEN"));
slider2.setMajorTickSpacing(50);
slider2.setMinorTickSpacing(15);
slider2.setPaintTicks(true);
slider2.setPaintLabels(true);
slider2.setSnapToTicks(true);
slider2.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
slider2.change_colorG(evt);

} }
);
content.add(slider2, BorderLayout.CENTER);

final JSliderX slider3 = new JSliderX(cc);
slider3.setMinimum(0);
slider3.setMaximum(255);
slider3.setValue(0);
slider3.setBorder(BorderFactory.createTitledBorder
("BLUE"));
slider3.setMajorTickSpacing(50);
slider3.setMinorTickSpacing(15);
slider3.setPaintTicks(true);
slider3.setPaintLabels(true);
slider2.setSnapToTicks(true);
slider3.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
slider3.change_colorB(evt);

} }
);
content.add(slider3, BorderLayout.SOUTH);

this.setPreferredSize(new Dimension(600, 225));
pack();
setVisible(true);
}

class JSliderX extends JSlider
{
ColorChange localCC;

public JSliderX(ColorChange cc)
{
localCC = cc;
}

public void change_colorR(javax.swing.event.ChangeEvent evt)

int A,R,G,B;
int redVal = this.getValue();
int deltaRed = redVal - oldRed;
if (deltaRed == 0) 
return;
else
oldRed = redVal;
for (int i = 0; i < localCC.bufferedImage.getWidth(); i++) 
{
for (int j = 0; j < localCC.bufferedImage.getHeight(); j++) 
{
A = (localCC.bufferedImage.getRGB(i,j)>>24);
R = (localCC.bufferedImage.getRGB(i,j)&0x00FF0000)>>16;
G = (localCC.bufferedImage.getRGB(i,j)&0x0000FF00)>>8;
B = (localCC.bufferedImage.getRGB(i,j)&0x000000FF);
R += deltaRed;
if (R > 255) R = 255;
if (R < 0) R = 0;
int rgb = A << 24 | R << 16 | G << 8 | B;
localCC.bufferedImage.setRGB(i,j,rgb);
}

localCC.repaint();
}

public void change_colorG(javax.swing.event.ChangeEvent evt)
{
int A,R,G,B;
int greenVal = this.getValue();
int deltaGreen = greenVal - oldGreen;
if (deltaGreen == 0) 
return;
else
oldGreen = greenVal;
for (int i = 0; i < localCC.bufferedImage.getWidth(); i++) 
{
for (int j = 0; j < localCC.bufferedImage.getHeight(); j++) 
{
A = (localCC.bufferedImage.getRGB(i,j)>>24);
R = (localCC.bufferedImage.getRGB(i,j)&0x00FF0000)>>16;
G = (localCC.bufferedImage.getRGB(i,j)&0x0000FF00)>>8;
B = (localCC.bufferedImage.getRGB(i,j)&0x000000FF);
G += deltaGreen;
if (G > 255) G = 255;
if (G < 0) G = 0;
int rgb = A << 24 | R << 16 | G << 8 | B;
localCC.bufferedImage.setRGB(i,j,rgb);

}
localCC.repaint();
}

public void change_colorB(javax.swing.event.ChangeEvent evt)
{
int A,R,G,B;
int blueVal = this.getValue();
int deltaBlue = blueVal - oldBlue;
if (deltaBlue == 0) 
return;
else
oldBlue = blueVal;
for (int i = 0; i < localCC.bufferedImage.getWidth(); i++) 
{
for (int j = 0; j < localCC.bufferedImage.getHeight(); j++) 
{
A = (localCC.bufferedImage.getRGB(i,j)>>24);
R = (localCC.bufferedImage.getRGB(i,j)&0x00FF0000)>>16;
G = (localCC.bufferedImage.getRGB(i,j)&0x0000FF00)>>8;
B = (localCC.bufferedImage.getRGB(i,j)&0x000000FF);
B += deltaBlue;
if (B > 255) B = 255;
if (B < 0) B = 0;
int rgb = A << 24 | R << 16 | G << 8 | B;
localCC.bufferedImage.setRGB(i,j,rgb); 
}
}
localCC.repaint();
}
}
}

                                                                                      Return To My Programs