The original picture before its color is edited.
The picture after its color is edited.
EditPictureColor source code:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JColorChooser;
//The PictureArea extends Canvas class
//It is where the picture is drawn
class PictureArea extends Canvas{
BufferedImage bi;
Robot rt;
int adjX;
int adjY;
PictureArea(String src){
addMouseListener(new MouseList()); //Register the PictureArea with the mouse event
try{
bi=ImageIO.read(new File(src)); //read the picture file
rt=new Robot(); //create a Robot object
}catch(Exception ie){}
adjX=0;
adjY=0;
}
class MouseList extends MouseAdapter{
public void mousePressed(MouseEvent e){
//fetch the color where the mouse is clicked
//since the PictureArea is not placed exactly at the point (0,0) on the window Frame
//we need to adjust the x-axis and y-axis of the clicked point so that
//the pixel color at desired point can be achieved
Color colorFrom=rt.getPixelColor(e.getX()+adjX, e.getY()+adjY);
//show a color chooser dialog
Color colorTo=JColorChooser.showDialog(null, "Choose a color", Color.WHITE);
changeColor(colorFrom, colorTo); //change color of the picture
repaint(); //redraw the Picture
}
}
//The changeColor method has code to change a color of the picture to a different color
//selected by the user from the color chooser dialog
public void changeColor(Color colorFrom, Color colorTo){
try{
int RGBFrom=colorFrom.getRGB();
int colTo=colorTo.getRGB();
for(int i=0;i<bi.getWidth();i++){
for(int j=0;j<bi.getHeight();j++)
{
int RGB=bi.getRGB(i, j);
if(RGBFrom==RGB)
bi.setRGB(i, j, colTo);
}
}
//write the update Picture to an image file called img.png
ImageIO.write(bi, "png", new File("img.png"));
}catch(Exception e){}
}
//The setAdjustment method will be invoked from the Main class so
//set adjX and adjY values
public void setAdjustment(int x,int y){
adjX=x;
adjY=y;
}
//THe paint method of the Canvas class is able to draw the picture
//when the program loads
public void paint(Graphics g){
if(bi!=null)
g.drawImage(bi,0,0,bi.getWidth(),bi.getHeight(),null);
}
}
//The Main class represents the window interface of the program
//On the interface we place the PictureArea object ia to show the picture
class Main extends Frame{
PictureArea ia;
String filename;
Main(){
selectPicture();
ia=new PictureArea(filename);
setTitle("Edit picture color");
setSize(600,400);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
add(ia,BorderLayout.CENTER);
setVisible(true);
ia.requestFocus();
ia.setAdjustment(ia.getX(), ia.getY());
ia.setBackground(Color.BLACK);
}
//The selectPicture method will be invoked to display file dialog for file selection
public void selectPicture(){
FileDialog fd=new FileDialog(this,"Choose a picture",FileDialog.LOAD);
fd.setVisible(true);
filename=fd.getFile();
if(filename!=null)
filename=fd.getDirectory()+"/"+fd.getFile();
else System.exit(100);
}
}
public class EditPictureColor{
public static void main(String[] args){
new Main();
}
}
For code explanation, please read comments along with the code.
amaze your friends Write For Us Gaming
ReplyDeleteBook Marketing Services
ReplyDelete