這可以用RadialGradientPaint
和適當AlphaComposite
來解決。
以下是MCVE顯示如何可以做到這一點。它使用相同的圖片,user1803551 used in his answer,所以截圖看起來(幾乎)相同。但是這樣一來增加了MouseMotionListener
,使您可以四處移動孔,通過將當前鼠標位置到updateGradientAt
方法,其中所需圖像的實際創作發生:
- 它先用填充圖像原始圖像
- 然後它會創建一個
RadialGradientPaint
,它的中心顏色完全不透明,邊緣(!)的顏色完全透明。這可能看起來違反直覺,但其目的是從現有圖像中「挖出」洞,這是通過下一步完成的:
AlphaComposite.DstOut
被分配給Graphics2D
。這一個導致一個α值的「反轉」,如式
Ar = Ad*(1-As)
Cr = Cd*(1-As)
其中r
代表「結果」,s
代表「源」和d
代表「目的地」
結果是在所需位置具有徑向漸變透明度的圖像,在中心處完全透明且在邊界(!)完全不透明。然後使用Paint
和Composite
的組合來填充具有孔的尺寸和座標的橢圓。 (也可以撥打fillRect
電話,填寫整個圖像 - 它不會改變結果)。
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TransparentGradientInImage
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TransparentGradientInImagePanel p =
new TransparentGradientInImagePanel();
f.getContentPane().add(p);
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class TransparentGradientInImagePanel extends JPanel
{
private BufferedImage background;
private BufferedImage originalImage;
private BufferedImage imageWithGradient;
TransparentGradientInImagePanel()
{
try
{
background = ImageIO.read(
new File("night-sky-astrophotography-1.jpg"));
originalImage = convertToARGB(ImageIO.read(new File("7bI1Y.jpg")));
imageWithGradient = convertToARGB(originalImage);
}
catch (IOException e)
{
e.printStackTrace();
}
addMouseMotionListener(new MouseAdapter()
{
@Override
public void mouseMoved(MouseEvent e)
{
updateGradientAt(e.getPoint());
}
});
}
private void updateGradientAt(Point point)
{
Graphics2D g = imageWithGradient.createGraphics();
g.drawImage(originalImage, 0, 0, null);
int radius = 100;
float fractions[] = { 0.0f, 1.0f };
Color colors[] = { new Color(0,0,0,255), new Color(0,0,0,0) };
RadialGradientPaint paint =
new RadialGradientPaint(point, radius, fractions, colors);
g.setPaint(paint);
g.setComposite(AlphaComposite.DstOut);
g.fillOval(point.x - radius, point.y - radius, radius * 2, radius * 2);
g.dispose();
repaint();
}
private static BufferedImage convertToARGB(BufferedImage image)
{
BufferedImage newImage =
new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return newImage;
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(background, 0, 0, null);
g.drawImage(imageWithGradient, 0, 0, null);
}
}
您可以與fractions
和RadialGradientPaint
的colors
播放,以實現不同的效果。例如,這些值...
float fractions[] = { 0.0f, 0.1f, 1.0f };
Color colors[] = {
new Color(0,0,0,255),
new Color(0,0,0,255),
new Color(0,0,0,0)
};
引起小,透明孔,具有大的,柔軟的 「電暈」:
而這些值
float fractions[] = { 0.0f, 0.9f, 1.0f };
Color colors[] = {
new Color(0,0,0,255),
new Color(0,0,0,255),
new Color(0,0,0,0)
};
導致一個大而透明的中心,帶有一個小的「電暈」:
RadialGradientPaint
JavaDocs有一些可能有助於找到所需值的示例。
,我發佈了一些相關的問題(類似的)答案:
編輯迴應有關性能的疑問,在紀念中被問到NTS
的如何Paint
/Composite
方法的性能進行比較的getRGB
/setRGB
方法的問題確實有趣。從我之前的經驗來看,我的直覺是第一個比第二個快得多,因爲一般來說,getRGB
/setRGB
往往很慢,並且內置機制高度優化(並且在某些情況下,甚至可能會硬件加速)。
事實上,Paint
/Composite
方法比getRGB
/setRGB
方法快,但並不如我預期的多。下面當然不是一個真正深刻的「基準」(我沒有使用卡尺或江鈴控股此),但應提供有關實際性能的良好估計:
// NOTE: This is not really a sophisticated "Benchmark",
// but gives a rough estimate about the performance
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.image.BufferedImage;
public class TransparentGradientInImagePerformance
{
public static void main(String[] args)
{
int w = 1000;
int h = 1000;
BufferedImage image0 = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
BufferedImage image1 = new BufferedImage(w, h,
BufferedImage.TYPE_INT_ARGB);
long before = 0;
long after = 0;
int runs = 100;
for (int radius = 100; radius <=400; radius += 10)
{
before = System.nanoTime();
for (int i=0; i<runs; i++)
{
transparitize(image0, w/2, h/2, radius);
}
after = System.nanoTime();
System.out.println(
"Radius "+radius+" with getRGB/setRGB: "+(after-before)/1e6);
before = System.nanoTime();
for (int i=0; i<runs; i++)
{
updateGradientAt(image0, image1, new Point(w/2, h/2), radius);
}
after = System.nanoTime();
System.out.println(
"Radius "+radius+" with paint "+(after-before)/1e6);
}
}
private static void transparitize(
BufferedImage imgA, int centerX, int centerY, int r)
{
for (int x = centerX - r; x < centerX + r; x++)
{
for (int y = centerY - r; y < centerY + r; y++)
{
double distance = Math.sqrt(
Math.pow(Math.abs(centerX - x), 2) +
Math.pow(Math.abs(centerY - y), 2));
if (distance > r)
continue;
int argb = imgA.getRGB(x, y);
int a = (argb >> 24) & 255;
double factor = distance/r;
argb = (argb - (a << 24) + ((int) (a * factor) << 24));
imgA.setRGB(x, y, argb);
}
}
}
private static void updateGradientAt(BufferedImage originalImage,
BufferedImage imageWithGradient, Point point, int radius)
{
Graphics2D g = imageWithGradient.createGraphics();
g.drawImage(originalImage, 0, 0, null);
float fractions[] = { 0.0f, 1.0f };
Color colors[] = { new Color(0, 0, 0, 255), new Color(0, 0, 0, 0) };
RadialGradientPaint paint = new RadialGradientPaint(point, radius,
fractions, colors);
g.setPaint(paint);
g.setComposite(AlphaComposite.DstOut);
g.fillOval(point.x - radius, point.y - radius, radius * 2, radius * 2);
g.dispose();
}
}
我的電腦上的計時沿的
...
Radius 390 with getRGB/setRGB: 1518.224404
Radius 390 with paint 764.11017
Radius 400 with getRGB/setRGB: 1612.854049
Radius 400 with paint 794.695199
線示出的是,Paint
/Composite
方法大致快兩倍的getRGB
/setRGB
方法。除了性能之外,Paint
/Composite
還有其他一些優勢,主要是上述RadialGradientPaint
的可能參數化,這是我更喜歡這種解決方案的原因。
非常好,這是否會給我的解決方案帶來更好的性能呢? – user1803551
@ user1803551它確實給了更好的性能(爲此添加了EDIT),但並不像我預期的那樣多(這可能是由於' RadialGradientPaint' - 你可以通過這種方式獲得一些漂亮的效果,而簡單地「剪下一個洞」就是這個類最簡單的例子之一) – Marco13