473,378 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need help with Blending

23
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:

Expand|Select|Wrap|Line Numbers
  1. int red = value;
  2. int green = value;
  3. int blue = value;
  4. int alpha = alphaMinimum;
  5. int x = 0;
  6. int y = 0;
  7.  
  8. for (int alpha = 255; alpha >= alphaMinimum; alpha -= 5)
  9. {
  10. g2d.setColor (new Color (value, value, value, alpha));
  11. g2d.drawRoundRect (
  12.             x++, y++, width, height, radius, radius);
  13. width -= 2;
  14. height -= 2;
  15. }
  16.  
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.
Jan 10 '09 #1
5 1254
JosAH
11,448 Expert 8TB
Have a look at the BufferedImage class and its get/setRGB methods.

kind regards,

Jos
Jan 11 '09 #2
HxRLxY
23
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.

Expand|Select|Wrap|Line Numbers
  1.  public void paintComponent (Graphics g)
  2.       {
  3.          Graphics2D g2d = (Graphics2D)g;
  4.  
  5.           //useful variables
  6.          int imageWidth = image.getWidth();
  7.          int imageHeight = image.getHeight();   
  8.          int imageWidthSquared = (int)(Math.pow (imageWidth, 2));
  9.          int imageHeightSquared = (int)(Math.pow (imageHeight, 2));
  10.  
  11.           //maximum distance a pixel can be from the center of the image
  12.          int maxDistance = (int)(Math.sqrt (imageWidthSquared + imageHeightSquared));
  13.  
  14.           //find the coordinates of the center of the image
  15.          int centerX = (int)(imageWidth / 2);
  16.          int centerY = (int)(imageHeight / 2);
  17.  
  18.          for (int y = 0; y < imageHeight; y++)
  19.          {
  20.             for (int x = 0; x < imageWidth; x++)
  21.             {
  22.                 //calculate the distance from the center of the image
  23.                int distanceXSquared = (int)(
  24.                   Math.pow (Math.abs (x - centerX), 2));
  25.                int distanceYSquared = (int)(
  26.                   Math.pow (Math.abs (y - centerY), 2));
  27.                int distance = (int)(Math.sqrt (distanceXSquared + distanceYSquared));
  28.  
  29.                 //determine a multiplier for the alpha value
  30.                float fractionalDistance = ((float)distance / maxDistance);
  31.                int alpha = (int)(255 * fractionalDistance);
  32.  
  33.                 //determine pixel color values
  34.                int value = image.getRGB (x, y);
  35.                int red = (value & 0x00FF0000) >> 16;
  36.                int green = (value & 0x0000FF00) >> 8;
  37.                int blue = (value & 0x000000FF);
  38.  
  39.                 //set pixel with new alpha value
  40.                int newValue = (alpha << 24) + (red << 16) + (green << 8) + blue;   
  41.                image.setRGB (x, y, value);
  42.             }
  43.          }
  44.  
  45.          g2d.drawImage (image, 0, 0, null);
  46.       }
  47.  
Jan 11 '09 #3
HxRLxY
23
Nevermind Jos. I'm an idiot. In line 41 I typed value instead of newValue. Thanks for the help.
Jan 11 '09 #4
JosAH
11,448 Expert 8TB
@HxRLxY
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:

Expand|Select|Wrap|Line Numbers
  1. int newValue= (image.getRGB(x, y)&0xffffff) | (alpha << 24);
  2.  
kind regards,

Jos
Jan 12 '09 #5
HxRLxY
23
Great tip Jos. Thanks again for the help.
Jan 13 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Philippe Meunier | last post by:
Hi, I am using the .NET toolbar with VB.NET 2003. My toolbar uses icons images that are actually PNG files and uses alpha blending on it. So to make alpha blending work correctly with the...
8
by: miro2021 | last post by:
Hi, Ive just started knocking up a rough idea for a website and need advice as to whether the way I have designed the layout will work. Basically as you can probably see from my mock up I want...
6
by: Renato | last post by:
Hi, I have seen the board described in this link. http://www.seventech.it/english/merlinoboard/2.php I think that its a good solution to realize an embedded system with VGA output, in that cases...
0
by: Chris Barnard | last post by:
Hi, I've got a form with a custom shape defined by a region. Does anyone know if it's possible to add a drop-shadow underneath the form similar to the standard windows one - with alpha blending?...
1
by: Simon | last post by:
Dear reader, Is there a possibility to close a form with a dissolving effect. It gives a nice effect instate of the hard close of the form.
0
by: gagandeepsingh | last post by:
Hi all, I m facing problem with Shock Wave Flash Active X Control, Problem: when i insert this control on my dialog window it shows the background window(white color) it happens only in case of...
0
by: ameshkin | last post by:
Hi, Im pretty new at PHP and need help with something very simple. I wrote a function which watermarks an image. The function works, but i can't figure out how to output the image into a file. ...
8
by: _DD | last post by:
It seems that there is some odd interaction with LinearGradientBrush that depends on positioning. A simple test: --------- Rectangle Square = new Rectangle(.....); in Paint: ...
1
by: bern11 | last post by:
Is there a way to draw a Line in GDI that will bit-wise AND or bit-wise OR the target pixel with a desired color? Same question with fill-patterns. I want to draw in Reb, Blue, & Green, and have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.