I have some general comments about the code you included. Hopefully they can help you figure out the problem you are having...

[EMAIL PROTECTED] wrote:
I have a question....
If I have a buffered image. I apply some changes to the image, such as smoothen 
the image. How can I display the new image. Here is a snippet of my source 
code. Because everytime I apply the the new image. The only thing is see is my 
original image. What are the few things to keep in mind to update an 
image(i.e.)after using setrgb()

[code]
    class MyPanel extends JPanel
    {
        //Purpose: a panel in which to draw

        //global offscreen and the Graphics2D variable g2
        private BufferedImage bufImag;
        protected Graphics2D g2;
        private int x,y;
        final int offScrWidth=1500,offScrHeight=1500;

        public MyPanel()
        {
          bufImag = new 
BufferedImage(offScrWidth,offScrHeight,BufferedImage.TYPE_INT_RGB);

This statement doesn't seem to serve any purpose because the following lines of code will replace bufImag with a new reference to a different image...?

                  try
                  {
                          InputStream in = new FileInputStream("Skull.JPG");
                          bufImag = ImageIO.read(in);

This line of code causes a brand new BufferedImage to be created and to be stored in the field named "bufImag" thereby replacing the reference to the image you created a couple of lines earlier. The contents of this new image will be initialized from the data read from the Skull.JPG file synchronously before the read() method returns. At this point, bufImag contains the image that was in the file.

                  }
                  catch (IOException ex) {}

          g2 = bufImag.createGraphics();

This creates a graphics object that can be used to render onto the image you just read from the "Skull.JPG" file. That image already has some perfectly good contents which were read from the JPEG file so I'm not sure why you would want, or need, to render onto it...?

          setPreferredSize(new Dimension(offScrWidth,offScrHeight));



                g2.drawImage(bufImag, 0,0,null);

The "g2" graphics object was created above from the image read from the file so it will render into that image. Here you asking that graphics object to render bufImag onto it, but that is the same image that the graphics was created from - so, in essence you are asking this graphics object to copy that image onto itself.

Luckily this has no effect so you wouldn't notice a problem here, but it is wasted effort so this line could easily be deleted.

        }

                public void smoothme ()
                {
                          int sum=0;
                          int width=bufImag.getWidth();
                          int height=bufImag.getHeight();
                          int[][] pixels = new int[width][height];


                          int I,J,i,j;
                                for (J=0;J<height;J++)
                                {
                                        for (I=0;I<width;I++)
                                        {
                                                pixels[I][J] = 
(bufImag.getRGB(I,J)& 0x000000ff);

This will extract the blue channel from every pixel in the image and store it in the pixels array. (Also, note that you are using 32 bits per pixel to store only 8 bits of data, which is a waste of storage.)
                                         }
                                }

                          bufImag = new 
BufferedImage(offScrWidth,offScrHeight,BufferedImage.TYPE_INT_RGB);
        //set of codes for processing the image

                                for (J=0;J<width;J++)
                                {
                                        for (I=0;I<height;I++)
                                        {
                                                
bufImag.setRGB(I,J,((pixels[I][J]<<8)+pixels[I][J]<<8)+pixels[I][J]);

After consulting with the operator precedence table I think this code is simply copying the data that was taken from the blue channel of the original image into the red, green, and blue channels of the new image. Was that the intent of the code? I'm not sure how that operation would be considered "smoothing"?

                                         }
                                }

                           g2.drawImage(bufImag, 0,0,null);

The object g2 still points to the original image, but the bufImag field now points to the image you just filled with new data. This operation will copy the new data you calculated into the old image, which is no longer referenced anywhere so it doesn't do any good to perform this copy. What was the intent of this line of code?

                           repaint();


                }
                  public void paintComponent(Graphics g)
                  {
                        //Purpose: do a complete screen refresh here
                        //Input: a Graphics to draw on
                        //Output: changes to the Graphics

                    super.paintComponent(g);
                    Graphics2D g2 = (Graphics2D)g;//copy offscreen to screen
                    g2.drawImage(bufImag, null,0,0);

The method you are using here to copy the image seems to be the "drawImage(Image, BufferedImageOp, x, y)" variant which is a fairly obscure variant to be using to perform this operation. The "drawImage(Image, x, y, [null Observer])" method you used in the other locations does the same operation and doesn't require a cast to Graphics2D.

                  }
                }
                class smooth{
                   // Main class
                   public static void main(String[] args){

                      MyPanel     myPanel=new MyPanel();
                      MyFrame     myFrame=new MyFrame(myPanel);

                      System.out.println("\n\nEnd of Processing");
                   }
                }

Hopefully something I've pointed out above can help you find your problem...

                        ...jim

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to