title: Set color of a pixel in an image
body: <p>Hey,I'm trying to make a program which loads a colored image as grayscale on canvas and then returns the color to the pixels clicked.
  I'm stuck here when the setrgb() method is not doing what its supposed to. I have copied the color from original image by getRGB() and am using setRGB() to assign it to a new image.
  I have tried to output both the pixel color values but they are not same.
  Please help me out with this.
  Here's the code so far:</p>
  
  <pre><code>import java.awt.image.*;
  import javax.imageio.*;
  import javax.swing.*;
  import java.awt.event.*;
  import java.io.*;
  import java.awt.*;
  import java.applet.*;
  
  @SuppressWarnings("serial")
  public class BlackWHite extends Applet implements MouseListener 
  {
      String str;
      int x=0,y=0;
      BufferedImage bimg = null;
      BufferedImage nimg = null;
      BufferedImage image=null;
      double image_width =300;
      double image_height=300;
      private Image img;
  
      public void init()
      {
          img = null;
          str = JOptionPane.showInputDialog(null, "Enter file location : ", 
          "Choose Image", 1);  
          BufferedImage image=null;
          try {
              image = ImageIO.read(new File(str));
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          //getting width and height of image
          image_width = image.getWidth();
          image_height = image.getHeight();
  
          BufferedImage img = image;
  
          //drawing a new image      
          bimg = new BufferedImage((int)image_width, (int)image_height,
                               BufferedImage.TYPE_BYTE_GRAY);
          Graphics2D gg = bimg.createGraphics();
          gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
  
          nimg = new BufferedImage((int)image_width, (int)image_height,
               BufferedImage.TYPE_INT_RGB);
          Graphics2D g = nimg.createGraphics();
          g.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
          addMouseListener(this); 
      }
  
      public void loadImage()
      {
          try {
              img = getImage(getCodeBase(), "blackwhiteimage.jpg");
          } catch(Exception e) {
          }
      }
  
      public void paint(Graphics g)
      {       
          try {
              image = ImageIO.read(new File(str));
          } catch (IOException e) {
              e.printStackTrace();
          }
          convert(); //converting the image
          if (img == null)
              loadImage(); //draw
          g.drawImage(img, 0, 0, this);
          int c = image.getRGB(x,y);
          int red = (c &amp; 0x0000FFFF) &gt;&gt; 16;  
          int green = (c &amp; 0x0000FFFF) &gt;&gt; 8;  
          int blue = c &amp; 0x0000FFFF;  
          c=(red &lt;&lt; 16) | (green &lt;&lt; 8) | blue;
          System.out.print("c="+c);
          nimg=bimg;
          nimg.setRGB(x,y,c);
          try {
              ImageIO.write(nimg, "jpg", new File("pixelcolor.jpg"));
          } catch (IOException e) {
              e.printStackTrace();
          }
          g.drawImage(nimg, 0, 0, this);
          int f = nimg.getRGB(x,y);
          System.out.println("f="+f);
      }
  
      void convert() {
          try {
               //saving black and white image onto drive
              ImageIO.write(bimg, "jpg", new File("blackwhiteimage.jpg"));
          } catch (Exception e) {
              System.out.println(e);
          }
      }
  
      @Override
      public void mouseClicked(MouseEvent me) {
          x = me.getX(); 
          y = me.getY();
          repaint();
      }
  
      @Override
      public void mouseEntered(MouseEvent arg0) {     
      }
  
      @Override
      public void mouseExited(MouseEvent arg0) {
      }
  
      @Override
      public void mousePressed(MouseEvent arg0) {
      }
  
      @Override
      public void mouseReleased(MouseEvent arg0) {
      }
  }
  </code></pre>