Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:21:23+00:00 2026-06-15T09:21:23+00:00

Background: I need to be able to create imagery in disabled look. The commonly

  • 0

Background: I need to be able to create imagery in “disabled” look. The commonly suggested approach is to convert images to grayscale and show the grayscaled image. The drawback is that it only works with images, making it cumbersome to show graphics where you do not have immediate access to an image in disabled state.
Now I thought this could be done on the fly with java.awt.Composite (and then I would not need to know how for example an Icon is implemented to render it disabled). Only there seems to be no implementation to convert to grayscale, so I had to create my own…

That said, I hacked together an implementation (and it renders what I expect it to). But I’m not sure it will really work correctly for all cases (Javadocs of Composite/CompositeContext seem extremely thin for such a complex operation). And as you may see from my implementation I go a roundabout way to process pixel by pixel, as there seems to be no simple way to read/write pixels in bulk in a format not dictated by the involved rasters.

Any pointers to more extensive documentation / examples / hints are welcome.

Here’s the SSCCE – it renders a (colored) GradientPaint through the DisabledComposite to convert the gradient to grayscale. Note that in the real world you will not know what is rendered an with what calls. The Gradient is really only an example (sorry, but too often people don’t get that, so I’ll make it explicit this time).

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Composite;
import java.awt.CompositeContext;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class CompositeSSCE implements Runnable {

    static class DisabledComposite implements Composite {
        @Override
        public CompositeContext createContext(
            final ColorModel srcColorModel,
            final ColorModel dstColorModel,
            final RenderingHints hints) {
            return new DisabledCompositeContext(srcColorModel, dstColorModel);
        }
    } 

    static class DisabledCompositeContext implements CompositeContext {

        private final ColorModel srcCM;
        private final ColorModel dstCM;

        final static int PRECBITS = 22;
        final static int WEIGHT_R = (int) ((1 << PRECBITS) * 0.299); 
        final static int WEIGHT_G = (int) ((1 << PRECBITS) * 0.578);
        final static int WEIGHT_B = (int) ((1 << PRECBITS) * 0.114);
        final static int SRCALPHA = (int) ((1 << PRECBITS) * 0.667);

        DisabledCompositeContext(final ColorModel srcCM, final ColorModel dstCM) {
            this.srcCM = srcCM;
            this.dstCM = dstCM;
        }

        public void compose(final Raster src, final Raster dstIn, final WritableRaster dstOut) {
            final int w = Math.min(src.getWidth(), dstIn.getWidth());
            final int h = Math.min(src.getHeight(), dstIn.getHeight());
            for (int y = 0; y < h; ++y) {
                for (int x = 0; x < w; ++x) {
                    int rgb1 = srcCM.getRGB(src.getDataElements(x, y, null));
                    int a1 = ((rgb1 >>> 24) * SRCALPHA) >> PRECBITS;
                    int gray = (
                        ((rgb1 >> 16) & 0xFF) * WEIGHT_R +
                        ((rgb1 >>  8) & 0xFF) * WEIGHT_G +
                        ((rgb1      ) & 0xFF) * WEIGHT_B
                        ) >> PRECBITS;
                    int rgb2 = dstCM.getRGB(dstIn.getDataElements(x, y, null));
                    int a2 = rgb2 >>> 24;
                    int r2 = (rgb2 >> 16) & 0xFF;
                    int g2 = (rgb2 >>  8) & 0xFF;
                    int b2 = (rgb2      ) & 0xFF;
                    // mix the two pixels
                    gray = gray * a1 / 255;
                    final int ta = a2 * (255 - a1);
                    r2 = gray + (r2 * ta / (255*255));
                    g2 = gray + (g2 * ta / (255*255));
                    b2 = gray + (b2 * ta / (255*255));
                    a2 = a1 + (ta / 255);
                    rgb2 = (a2 << 24) | (r2 << 16) | (g2 << 8) | b2; 
                    Object data = dstCM.getDataElements(rgb2, null);
                    dstOut.setDataElements(x, y, data);
                }
            }
        }

        @Override
        public void dispose() {
            // nothing for this implementation
        }
    }

    // from here on out its only the fluff to make this a runnable example
    public static void main(String[] argv) {
        Runnable r = new CompositeSSCE();
        SwingUtilities.invokeLater(r);
    }

    // simple component to use composite to render
    static class DemoComponent extends JComponent {
        // demonstrate rendering an icon in grayscale with the composite
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            GradientPaint p = new GradientPaint(0, 0, Color.GREEN, 127, 127, Color.BLUE, true);
            g2d.setComposite(new DisabledComposite());
            g2d.setPaint(p);
            g2d.fillRect(0, 0, getWidth(), getHeight());
        }
    }

    // Fluff to use the Composite in Swing 
    public void run() {
        try {
            JFrame f = new JFrame("Test grayscale composite");
            DemoComponent c = new DemoComponent();
            c.setPreferredSize(new Dimension(500, 300));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setLayout(new BorderLayout());
            f.add(c, BorderLayout.CENTER);
            f.pack();
            f.setVisible(true);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:21:24+00:00Added an answer on June 15, 2026 at 9:21 am

    You can always create a BufferedImage, draw whatever needs to be grey-scaled to the Graphics object for that image using bufferedImage.createGraphics(), and then use the javax.swing.GreyFilter to create a grey-scaled copy of the image.

    This page shows you how to use a Filter.

    You might also want to compare your approach with SwingX’s BlendComposite, which I expect does the same thing as you are doing. BlendComposite has a Saturation mode that would allow grey-scale. (It also has more modes.)

    This page has a demo of BlendComposite.

    About efficiency, I expect there’s no important difference between them, because there’s intermediate steps, and from what I can see, complete copies of the image data with both. But if you retain the grey-scaled image using the first method I suggested, you can prevent recalculation of non-dynamic controls.

    If you do one of the above, the performance will be correct, and I expect that’s what you really want.

    From your comments I guess you might just want to apply the effect to a component. For this you could use JLayer, only available in Java 7. From the Javadoc there is an example of overlaying a translucent green. You could replace this with grey. If you want to use JLayer in previous versions of Java you could use JXLayer, part of the SwingX project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a site, I need to be able to dynamically display background images depending
I need to be able to differentiate between application didFinishLaunching and application entering background
I need to place two repeated background images on the left and right border
I want to be able to create a transparent background using only CSS but
Background : I need to create a custom like extension method for linqtohiberante to
I'm using Backstretch jQuery plugin to create full screen background images, but have a
Background We need to develop a specialised CMS (internal use only) to support a
I need background transparent only for black div. but it is applying even for
I come from a .NET background and need to do a web project in
When i send my application to the background i need to kill the process,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.