我的问题很具体。我正在开发一种游戏,其中有一个可以从敌人身上掉落的药水罐。药水罐的图像是一个透明的罐子,黑色边框和不透明的白色背景。之所以有不透明的白色背景是因为我还需要画出罐子里剩下多少药水。我目前的做法是:
因此,这将与绘制的丑陋红色矩形重叠,以表示药水的透明区域周围的不透明白色空间剩余的药水量。这是结果的图像:
http://i.imgur.com/j7ydEBF.png
出现问题是因为魔药罐在游戏中确实看起来很糟糕。白色背景看起来很可怕。
我的问题是:有没有办法去除那个不透明的白色背景,同时仍然能够在图像定义的空间中“填充”药水罐?
答案 0 :(得分:4)
基本上,你可以创建一个"掩码"可以从图像的非不透明部分生成原始图像。
所以我创建了两个药水图像(确定你可以使用精灵表)。一个有不透明的中心,一个是透明的(只是轮廓)。
由此,我能够生成第一张图像的蒙版(我想要的颜色),使用子图像减少我想要使用的图像量然后渲染它和轮廓
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestOverlay {
public static void main(String[] args) {
new TestOverlay();
}
public TestOverlay() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage potionBase;
private BufferedImage potionOutline;
private float value = 1f;
public TestPane() {
try {
potionBase = ImageIO.read(new File("Potion.png"));
potionOutline = ImageIO.read(new File("PotionOutline.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
value = value - 0.01f;
if (value < 0) {
value = 1f;
}
repaint();
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return potionBase == null ? super.getPreferredSize() : new Dimension(potionBase.getWidth(), potionBase.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
BufferedImage mask = generateMask(potionBase, Color.RED, 1f);
int y = (int) (mask.getHeight() * (1f - value));
if (y < mask.getHeight()) {
mask = mask.getSubimage(0, y, mask.getWidth(), mask.getHeight() - y);
}
int x = (getWidth() - mask.getWidth()) / 2;
y = y + ((getHeight() - potionOutline.getHeight()) / 2);
g2d.drawImage(mask, x, y, this);
y = ((getHeight() - potionOutline.getHeight()) / 2);
g2d.drawImage(potionOutline, x, y, this);
g2d.dispose();
}
}
public static BufferedImage generateMask(BufferedImage imgSource, Color color, float alpha) {
int imgWidth = imgSource.getWidth();
int imgHeight = imgSource.getHeight();
BufferedImage imgMask = createCompatibleImage(imgWidth, imgHeight, Transparency.TRANSLUCENT);
Graphics2D g2 = imgMask.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2.drawImage(imgSource, 0, 0, null);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, alpha));
g2.setColor(color);
g2.fillRect(0, 0, imgSource.getWidth(), imgSource.getHeight());
g2.dispose();
return imgMask;
}
public static BufferedImage createCompatibleImage(int width, int height, int transparency) {
BufferedImage image = getGraphicsConfiguration().createCompatibleImage(width, height, transparency);
image.coerceData(true);
return image;
}
public static GraphicsConfiguration getGraphicsConfiguration() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}
}
现在,经历过这个问题,我应该做的只是简单地创建一个&#34;填充&#34;药水和空的&#34;药水(其内部容器是空的)并且简单地使用相同的过程来生成子图像(已填充的罐子)而不是生成面具......但它很有趣;)
基本上,这意味着,&#34;空&#34; jar可以是完全透明的(除了轮廓),你可以在&#34;内画#34;它...
这是基于所示的here
图像着色的概念答案 1 :(得分:1)