• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1347
  • Last Modified:

Draw image at 50% opacity

Hey,

I am doing something like the following:


            BufferedImage src = null;            // Class Instance
            ...
            Rectangle area = new Rectangle( getX(), getY(), getWidth(), getHeight() );
            try
            {
                Robot robot = new Robot();
                scr = robot.createScreenCapture( area );
            } catch ( Exception e )
            {
                e.printStackTrace();
            }
           
            JPanel pane = new JPanel()
            {
                public void paintComponent( Graphics g )
                {
                    super.paintComponent( g );
                    g.drawImage( scr, 0, 0, this );
                    g.drawImage( new ImageIcon( "bg.gif" ).getImage(), 0, 0, this );
                }
            };
            setContentPane( pane );


I works great, whoo! :-)  However, now, I wish to draw the image - which I draw on with this line:

     g.drawImage( new ImageIcon( "bg.gif" ).getImage(), 0, 0, this );

with an opacity of 50%. How to specify the opactiy of it?

Thanks very much in advance.

Regards;
0
InteractiveMind
Asked:
InteractiveMind
  • 2
  • 2
1 Solution
 
limaidealCommented:
And here's a piece of code found in Java2D Demo jdk1.4\demo\jfc\Java2D\src\java2d\demos\Composite\ACimages.java:

public void render(int w, int h, Graphics2D g2) {

        float alpha = 0.0f;
        int iw = w/3;
        int ih = (h-45)/3;
        float xx = 0, yy = 15;

        for (int i =0; i < imgs.length; i++) {

            xx = (i%3 == 0) ? 0 : xx+w/3;
            switch (i) {
                case 3 : yy = h/3+15; break;
                case 6 : yy = h/3*2+15;
            }

            g2.setComposite(AlphaComposite.SrcOver);
            g2.setColor(Color.black);
            AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha += .1f);
            String s = "a=" + Float.toString(alpha).substring(0,3);
            new TextLayout(s,g2.getFont(), g2.getFontRenderContext()).draw(g2, xx+3, yy-2);

            Shape shape=null;

            switch (i%3) {
                case 0 : shape = new Ellipse2D.Float(xx, yy, iw, ih);
                        break;
                case 1 : shape = new RoundRectangle2D.Float(xx, yy, iw, ih, 25, 25);
                        break;
                case 2 : shape = new Rectangle2D.Float(xx, yy, iw, ih);
                        break;
            }
            g2.setColor(colors[i]);
            g2.setComposite(ac);
            g2.fill(shape);
            g2.drawImage(imgs[i], (int) xx, (int) yy, iw, ih, null);
        }
    }
0
 
InteractiveMindAuthor Commented:
limaideal,
how would I use this with my program?
0
 
InteractiveMindAuthor Commented:
Ah... figured it, this is what I need to do:

            JPanel pane = new JPanel()
            {
                public void paintComponent( Graphics g )
                {
                    super.paintComponent( g );
                    g.drawImage( scr, 0, 0, this );
                   
                    Graphics2D g2 = (Graphics2D) g;
                    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f );
                    g2.setComposite( ac );
                   
                    g2.drawImage( new ImageIcon( "bg.gif" ).getImage(), 0, 0, this );
                }
            };


Cheers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now