Blurring an Image in Swing

Use the class ConvolveOp and apply a blur filter.

import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
   private boolean firstTime = true;
   private BufferedImage sourceBi;
   private BufferedImage destBi;
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
   public void paint(Graphics graphics) {
      Graphics2D g2d = (Graphics2D) graphics;
      if (firstTime) {
         Image img = new javax.swing.ImageIcon("djkrush.jpg").getImage(); 
         sourceBi = toBufferedImage(img);
         destBi = new BufferedImage(sourceBi.getWidth(), sourceBi.getHeight(), 
         setSize(sourceBi.getWidth(), sourceBi.getHeight()*2);
         Kernel kernel = new Kernel(3, 3, new float[] 
                                            {  1f/9f, 1f/9f, 1f/9f,
                                               1f/9f, 1f/9f, 1f/9f,
                                               1f/9f, 1f/9f, 1f/9f });
         ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
         cOp.filter(sourceBi, destBi);
         firstTime = false;
      g2d.drawImage(sourceBi, 0, 0, this);
      g2d.drawImage(destBi, 0, sourceBi.getHeight(), this);
   public static BufferedImage toBufferedImage(Image image) {
      BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), 
      // copy the original image
      Graphics g = bi.createGraphics();
      g.drawImage(image, 0, 0, null);
      return bi;
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 150);

Image used:

To learn more about image processing,
check out this expensive but excellent book: