Code for drawing blurred text

I was gonna put this in the shared code section, but then decided against it after seeing how much better the code there was. ::slight_smile:

Basically, what this code does is create a BufferedImage from the given parameters. It’s size depends on the font and String length, so there’s no need for any detailed input besides that. This works fairly well with entire strings as well as individual chars, so you could feed them in one by one, each with a different color parameter, and get a nice gradient effect. Enjoy!

P.S. this is my first ‘submitted’ stuff, so cretique away!


  public BufferedImage createBlurredText(Font f, Color col, String str, int blurIntensity){
       
      //     Create a graphics object so we can get at the details in the font
         BufferedImage bTemp = new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB);
         Graphics gTemp = bTemp.createGraphics();
         gTemp.setFont(f);
         FontMetrics fm = gTemp.getFontMetrics();
         
      	//How much extra space is needed to accomidate the blurred String
         int spread = 8;
			
      	
         BufferedImage bCBT = new BufferedImage(
            fm.stringWidth(str)+spread,
            fm.getFont().getSize()+spread,
            BufferedImage.TYPE_INT_ARGB);
      	
         Graphics2D gCBT = (Graphics2D)bCBT.createGraphics();
      	
      	/*Draws the string as centered as possible inside the buffer*/
         gCBT.setColor(col);
         gCBT.setFont(f);
         gCBT.drawString(str,spread/2,spread/2+fm.getFont().getSize());
      	
         int blur = 30-blurIntensity;

         float d = (((.10f/.09f)*(blur+132)/160)-1.0f);
      
         float[] blurKernel = {
                     1/9f - d, 1/9f -d, 1/9f-d,    // low-pass filter kernel
                     1/9f-d, 1/9f+8*d, 1/9f-d,
                     1/9f-d, 1/9f-d, 1/9f-d
                     };
               		
         ConvolveOp cop = new ConvolveOp(new Kernel(3, 3, blurKernel),
                                            ConvolveOp.EDGE_NO_OP,
                                            null);

          //Layer the blur effects						  
         for(int x=0;x<5;x++)
            gCBT.drawImage(bCBT, cop, 0, 0);
      		
            
         gTemp.dispose();//Get rid of any data in the 1x1 BF
         return bCBT;
      		
      }

How it looks like:

That’s pretty nifty!

I imagine you need to know all the Strings you’re going to use beforehand, however, or this will get too expensive. Looks great though.