2
Q
繪製圖像的形狀
A
回答
5
ImageFilter是你想要的。可以使用GrayFilter。一個組件內
class ShapeFilter extends RGBImageFilter {
public RedBlueSwapFilter() {
// The filter's operation does not depend on the
// pixel's location, so IndexColorModels can be
// filtered directly.
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
return (rgb & 0x00_ff_ff_ff) == 0x00_ff_ff_ff // White
|| rgb & 0xff_00_00_00) == 0x00_00_00) // or transparent
? rgb : 0xff_ff_00_00; // Red opaque
}
}
用法(JComponent中)用的createImage:
Image img = ImageIO.readImage(...);
ImageFilter filter = new ShapeFilter();
ImageProducer producer = new FilteredImageSource(img.getSource(), filter);
Image img2 = createImage(producer);
3
您可以生成一個圖像的 「面具」
該技術的大特點是你可以生成任何你想要的顏色或不透明度的面具
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.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
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.UIManager;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception 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 static GraphicsConfiguration getGraphicsConfiguration() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
}
public static BufferedImage createCompatibleImage(int width, int height, int transparency) {
BufferedImage image = getGraphicsConfiguration().createCompatibleImage(width, height, transparency);
image.coerceData(true);
return image;
}
public static void applyQualityRenderingHints(Graphics2D g2d) {
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
}
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();
applyQualityRenderingHints(g2);
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 BufferedImage tint(BufferedImage master, BufferedImage tint) {
int imgWidth = master.getWidth();
int imgHeight = master.getHeight();
BufferedImage tinted = createCompatibleImage(imgWidth, imgHeight, Transparency.TRANSLUCENT);
Graphics2D g2 = tinted.createGraphics();
applyQualityRenderingHints(g2);
g2.drawImage(master, 0, 0, null);
g2.drawImage(tint, 0, 0, null);
g2.dispose();
return tinted;
}
public class TestPane extends JPanel {
private BufferedImage master;
private BufferedImage mask;
public TestPane() {
try {
master = ImageIO.read(new File(bring your own image));
mask = generateMask(master, Color.RED, 1.0f);
} catch (IOException exp) {
exp.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
if (master != null && mask != null) {
size = new Dimension(master.getWidth() + mask.getWidth(), Math.max(master.getHeight(), mask.getHeight()));
}
return size;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (getWidth() - (master.getWidth() + mask.getWidth()))/2;
int y = (getHeight() - master.getHeight())/2;
g.drawImage(master, x, y, this);
x += mask.getWidth();
y = (getHeight() - mask.getHeight())/2;
g.drawImage(mask, x, y, this);
}
}
}
相關問題
- 1. 繪製形狀與渲染圖像?
- 2. Matplotlib繪製圖形上的形狀
- 3. 繪製矩形形狀阻力和圖像的降HTML5畫布
- 4. 計算形狀的繪製圖像中的獨特像素
- 5. 繪圖問題 - 從其他形狀繪製複合形狀
- 6. 核心圖形繪製多個形狀
- 7. 如何使用畫布以梯形形狀繪製圖像
- 8. 用繪製圖形方法繪製一個形狀?
- 9. 繪製矩形圖像 - LibGdx
- 10. 形狀可繪製
- 11. 可以用形狀填充圖像的4個點繪製形狀嗎?
- 12. 以自定義形狀繪製像素
- 13. Swift:試圖旋轉繪製的形狀?
- 14. Android的繪製形狀編程位圖
- 15. 由鼠標控制的形狀繪圖
- 16. 帶圖形C的圖像繪製#
- 17. 繪製形狀與JavaScript - 形狀問題
- 18. Android的XML形狀繪製
- 19. Actionscript中的繪製形狀
- 20. Android壁畫:繪製不同種類的圖像形狀
- 21. 如何刪除圖像上繪製的形狀
- 22. 如何使用可與圖像一起繪製的形狀?
- 23. 在android中更改圖像的繪製形狀
- 24. 查找然後隱藏圖像中的繪製形狀?
- 25. 繪製圖像的形狀和它保存在PHP
- 26. 根據繪圖2的形狀繪製作圖1
- 27. 繪製圖像用C#圖形
- 28. 如何保存繪製形狀,圖像C#
- 29. 在曲線路徑上繪製形狀或圖像
- 30. 繪製自定義形狀,以圖像加載到
謝謝!也可以在JComponent之外使用這個嗎? – Duke
['Toolkit.getDefaultToolkit().createImage'](https://docs.oracle.com/javase/8/docs/api/java/awt/Toolkit.html) –