After having searched through I found a lot of complicated solutions, with timings and color filters etc, and some clues too. Here is a simple solution to fade out an image into darkness:
BufferedImage imi=new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
imi.getGraphics().drawImage(bi, 0, 0, w, h, null);
Graphics2D g2=(Graphics2D)imi.getGraphics();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)0.2));
g2.setColor(Color.black);
g2.fillRect(0, 0, w, h);
That is you first draw the image itself then you draw a black on top with a given (0.2) transparency,
where bi is assumed to hold your image of interest.
For a complete and gradual fade out you use the following in your paint method:
public void paint(Graphics g) {
if(imk>times) ;
else {
if(imk==0) { g.drawImage(im, 0, 0, this); imk++; }
else {
Graphics2D g2=(Graphics2D)g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)rate));
g2.setColor(Color.black);
g2.fillRect(0, 0, w, h);
imk++;
}
}
}
where im is the image of interest, and times is the number of times to take to gradually withdraw.
It can be set up as
int imk=0, times=1;
double rate=0.1, limit=0.009;
times=(int)(Math.log(limit)/Math.log(1-rate));
BufferedImage imi=new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
imi.getGraphics().drawImage(bi, 0, 0, w, h, null);
Graphics2D g2=(Graphics2D)imi.getGraphics();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)0.2));
g2.setColor(Color.black);
g2.fillRect(0, 0, w, h);
That is you first draw the image itself then you draw a black on top with a given (0.2) transparency,
where bi is assumed to hold your image of interest.
For a complete and gradual fade out you use the following in your paint method:
public void paint(Graphics g) {
if(imk>times) ;
else {
if(imk==0) { g.drawImage(im, 0, 0, this); imk++; }
else {
Graphics2D g2=(Graphics2D)g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)rate));
g2.setColor(Color.black);
g2.fillRect(0, 0, w, h);
imk++;
}
}
}
where im is the image of interest, and times is the number of times to take to gradually withdraw.
It can be set up as
int imk=0, times=1;
double rate=0.1, limit=0.009;
times=(int)(Math.log(limit)/Math.log(1-rate));
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου