Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Draw image at 50% opacity

Posted on 2005-04-23
4
Medium Priority
?
1,344 Views
Last Modified: 2012-06-27
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
Comment
Question by:InteractiveMind
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
limaideal earned 2000 total points
ID: 13850136
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
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13850219
limaideal,
how would I use this with my program?
0
 
LVL 25

Author Comment

by:InteractiveMind
ID: 13850278
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 22 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question