Hi,
I have 2 images stored in the variables Image img_arrowl; Image img_arrowr; and now I need a function or method to set the opacity of these two images. So the syntax I'm after would be something like the following:

img_arrowl = set_imgopacity(img_arrowl,0.75);
img_arrowr = set_imgopacity(img_arrowr,0.75);

The only thing is how do I create the set_imgopacity() function/method. I have spent all day searching the web with no straight forward answer. And Java isn't my major of all languages so could somebody help me with creating this function.

Any help is appreciated.
Thankyou

cwarn23

Recommended Answers

All 8 Replies

Hi,
I have managed to assemble the following but how do I convert the Raster result to an Image type?

public Raster transform(Image img, float alpha) {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    bi.getGraphics().drawImage(img, 0, 0, null);
    RasterOp rop = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, alpha }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, null);
    //BufferedImage fImage = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    Raster result;
    result = rop.filter(bi.getData(), null);
    return result;
    }

Hi,
I have managed to assemble the following but how do I convert the Raster result to an Image type?

public Raster transform(Image img, float alpha) {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    bi.getGraphics().drawImage(img, 0, 0, null);
    RasterOp rop = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, alpha }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, null);
    //BufferedImage fImage = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    Raster result;
    result = rop.filter(bi.getData(), null);
    return result;
    }

I dint understand why do u need image object

because if you want to save that image you can directly use WritableRaster class..

So that I can delete line 3 and have something like the following:

public Raster transform(Image img, float alpha) {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    //bi.getGraphics().drawImage(img, 0, 0, null);
    RasterOp rop = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, alpha }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, null);
    Raster result;
    result = rop.filter(bi.getData(), null);
    return result;
    }
public void paint(Graphics g) {
Image source = retrieve_image();
source = transform(source, 0.5);
g.drawImage(source,300,419,31,31,this)
}

Basically I am trying to get an Image then set the opacity and display it at specified coordinates with a specified size. Each image will be at different coordinates with different sizes though. So as you can see that has a syntax error where a Raster is being assigned to an Image so are there any ways around it?
Thankyou

Use an AlphaComposite to set the drawing mode and alpha before drawing your image. Here's a simple sample...

Image myImage = ...
float alpha = 0.5;
@Override
public void paintComponent(Graphics g) {
	Graphics2D g2D = (Graphics2D) g;

	AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
	g2D.setComposite(composite); // Set current alpha
	g2D.drawImage(myImage, 0, 0, null);
}

or this one

http://download.oracle.com/javase/tutorial/2d/images/drawimage.html

Thankyou so much. Although I had read that article a million times I seemed to have skimmed by the solution in their code. But now I have worked out the secret trick. Thanks for refering me to that link for me to reread again. The code I ended up using is the following:

public Graphics2D transform(Image img, float alpha, Graphics2D g2d,int xx, int yy) {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
    bi.getGraphics().drawImage(img, 0, 0, null);
    RasterOp rop = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, alpha }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f }, null);
    //BufferedImage fImage = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
    //Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(bi, (BufferedImageOp) rop,xx,yy);
    return g2d;
    }



     public void paint(Graphics g)
     {   
          g.drawImage(my_gif,150,0,300,450,this);
          float alpha;
          alpha = 0.66f;
          Image img_arrowl = getImage(base,"l.png");
          Image img_arrowr = getImage(base,"r.png");

          Graphics2D g2d = transform(img_arrowl,alpha,(Graphics2D) g,269,419);
          g2d = transform(img_arrowr,alpha,g2d,300,419);
          g = (Graphics) g2d;
      }

As complicated as it looks it's actually very expandable in design having use of multiple libraries. The above code is the solution which I managed to piece together and works very well. Now for the mouse integration which I hope won't be any harder. :)
Thankyou for all your posts as this was an interesting problem to solve.

*solved*

put that into JPanel or JLabel with correct deffinitons for BorderLayout or GridLaout and then you forgot about absolute Sizing for Image or ImageIcon

there are

http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Geom-image-manip.doc.html

http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/

http://java.sun.com/products/java-media/jai/forDevelopers/jaifaq.html

don't forget check these methods for deepest workaround on http://www.java.net/projects/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.