Converting a color image to a black and white one in Swing

Use the ColorConvertOp class. Its filter() method does a pixel-by-pixel color conversion of the original image.
Following example captures the screen, converts it to gray-scale and displays it in a window:

import java.awt.event.*;
import java.awt.image.*;
import java.awt.color.*;
import javax.swing.*;
import java.awt.*;
  
public class Main extends JFrame
{
   public Main() throws Exception {
      Robot robot = new Robot();
      int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
      int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
 
      // capture the whole screen
      BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, width, height));
  
      // convert to black and white
      BufferedImage bwimage = convert2bw(image);
 
      // add the image inside a JLabel component to the window
      getContentPane().add(new JLabel(new ImageIcon(bwimage)));
      pack();
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   // converts an image to a black and white one
   public BufferedImage convert2bw(BufferedImage image) {
      ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
      ColorConvertOp ccop = new ColorConvertOp(cs, null);

      return ccop.filter(image, null);
   }
  
   public static void main(String args[]) throws Exception {
      Main main = new Main();
      main.show();
   }
}