Need help with Blending

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HxRLxY
    New Member
    • Sep 2008
    • 23

    #1

    Need help with Blending

    I am creating a custom JPanel that displays an image in the background on top of a custom color. What I want to do is to have the edges of the top image blend into the background color. Only the outer 20 pixels or so. I have to do this at runtime due to the nature of the program, so using Photoshop or the like isn't an option. I've tried something like this:

    Code:
    int red = value;
    int green = value;
    int blue = value;
    int alpha = alphaMinimum;
    int x = 0;
    int y = 0;
    
    for (int alpha = 255; alpha >= alphaMinimum; alpha -= 5)
    {
    g2d.setColor (new Color (value, value, value, alpha));
    g2d.drawRoundRect (
                x++, y++, width, height, radius, radius);
    width -= 2;
    height -= 2;
    }
    The idea being that if I draw a series of concentric shapes around the image, while incrementally lowering the transparency, it will produce a blending effect. The problem is that when rendered, there are lines in the corners of the image that I can't figure out how to get rid of. It also seems like a brute force approach, and as such isn't very elegant code.

    Another idea I had was start in the middle of the image and move outward radially while slowly lowering the opacity of the pixels as I get closer to the edges. But I don't even know where to start for that (apart from at the center of the image.)

    Any ideas, suggestions, guidelines, pointers, tips, thoughts, axioms, or rambling pontifications would be greatly appreciated. Thank you.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have a look at the BufferedImage class and its get/setRGB methods.

    kind regards,

    Jos

    Comment

    • HxRLxY
      New Member
      • Sep 2008
      • 23

      #3
      Thanks for the tip Jos. Here is what I've come up with. I decided I would try to change a given pixel's alpha value based on it's distance from the center of the image, which should create a radial blending effect. I've used the getRGB/setRGB methods as you suggested, but when I run the code, the image doesn't blend. It looks just like the normal image. I've tried changing the AlphaComposite setting of the Graphics2D object with no effect. I've tried: SRC_OVER and SRC_ATOP, neither of which do anything different when the image is shown on the screen. My BufferedImage is TYPE_INT_ARGB. Any further help is greatly appreciated.

      Code:
       public void paintComponent (Graphics g)
            {
               Graphics2D g2d = (Graphics2D)g;
                        
            	//useful variables
               int imageWidth = image.getWidth();
               int imageHeight = image.getHeight();   
               int imageWidthSquared = (int)(Math.pow (imageWidth, 2));
               int imageHeightSquared = (int)(Math.pow (imageHeight, 2));
               
            	//maximum distance a pixel can be from the center of the image
               int maxDistance = (int)(Math.sqrt (imageWidthSquared + imageHeightSquared));
               
            	//find the coordinates of the center of the image
               int centerX = (int)(imageWidth / 2);
               int centerY = (int)(imageHeight / 2);
               
               for (int y = 0; y < imageHeight; y++)
               {
                  for (int x = 0; x < imageWidth; x++)
                  {
                  	//calculate the distance from the center of the image
                     int distanceXSquared = (int)(
                        Math.pow (Math.abs (x - centerX), 2));
                     int distanceYSquared = (int)(
                        Math.pow (Math.abs (y - centerY), 2));
                     int distance = (int)(Math.sqrt (distanceXSquared + distanceYSquared));
                     
                  	//determine a multiplier for the alpha value
                     float fractionalDistance = ((float)distance / maxDistance);
                     int alpha = (int)(255 * fractionalDistance);
                     
                  	//determine pixel color values
                     int value = image.getRGB (x, y);
                     int red = (value & 0x00FF0000) >> 16;
                     int green = (value & 0x0000FF00) >> 8;
                     int blue = (value & 0x000000FF);
                  	
                  	//set pixel with new alpha value
                     int newValue = (alpha << 24) + (red << 16) + (green << 8) + blue;   
                     image.setRGB (x, y, value);
                  }
               }
            	
               g2d.drawImage (image, 0, 0, null);
            }

      Comment

      • HxRLxY
        New Member
        • Sep 2008
        • 23

        #4
        Nevermind Jos. I'm an idiot. In line 41 I typed value instead of newValue. Thanks for the help.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by HxRLxY
          Nevermind Jos. I'm an idiot. In line 41 I typed value instead of newValue. Thanks for the help.
          I make those mistakes all the time, leaving me staring at my screen with a stupid look on my face :-) A bit of micro-optimisation: all you need to do is shift in the new alpha value, i,e. you can leave the rgb values as they are:

          Code:
          int newValue= (image.getRGB(x, y)&0xffffff) | (alpha << 24);
          kind regards,

          Jos

          Comment

          • HxRLxY
            New Member
            • Sep 2008
            • 23

            #6
            Great tip Jos. Thanks again for the help.

            Comment

            Working...