gpt4 book ai didi

java - Java图形中如何使透明度透明?

转载 作者:行者123 更新时间:2023-12-02 12:10:42 25 4
gpt4 key购买 nike

 private static void convertTo2D(BufferedImage image, Graphics g, int locX, int locY, int sizeWidth, int sizeHeight) {
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
Image img = image.getScaledInstance((sizeWidth), (sizeHeight), Image.SCALE_SMOOTH);
image = new BufferedImage((sizeWidth), (sizeHeight), BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(img, 0, 0, null);
//The horizontal of image
for(int x=0; x<(sizeWidth); x++) {
//The vertical of image
for(int y=0; y<(sizeHeight); y++) {
//Get RGB color and draw it
int color = image.getRGB(x, y);
Color clr = new Color(color, true);
g.setColor(clr);
// Alpha = 0 - Fully Transparent
if(clr.getAlpha() !=1) {
g.drawLine((x + locX), (y + locY), (x + locX), (y + locY));
}
}
}
}

这是我的代码。基本上,如果它是透明像素,我不希望它绘制像素(透明像素是当您在谷歌上查看图像时将棋盘作为背景的像素)。我究竟做错了什么?目前这些图像的背景只是黑色,所以像素会被绘制出来吗?...

最佳答案

我要做的就是先绘制背景,然后在其上绘制图像,就像......

protected BufferedImage makeImageFrom(BufferedImage original) {
BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, img.getWidth(), img.getHeight());
g2d.setColor(Color.LIGHT_GRAY);
int row = 0;
for (int y = 0; y < img.getHeight(); y += 10) {
int offset = (row % 2 == 0) ? 10 : 0;
for (int x = 0; x < img.getWidth(); x += 20) {
g2d.fillRect(offset + x, y, 10, 10);
}
row++;
}
g2d.drawImage(original, 0, 0, this);
g2d.dispose();
return img;
}

Check boarded

以及用于测试它的代码......

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

public static void main(String args[]) throws ParseException {
new Test();
}

public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}

public class TestPane extends JPanel {

private BufferedImage img;

public TestPane() throws IOException {
BufferedImage original = ImageIO.read(get your own image);
img = makeImageFrom(original);
}

protected BufferedImage makeImageFrom(BufferedImage original) {
BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, img.getWidth(), img.getHeight());
g2d.setColor(Color.LIGHT_GRAY);
int row = 0;
for (int y = 0; y < img.getHeight(); y += 10) {
int offset = (row % 2 == 0) ? 10 : 0;
for (int x = 0; x < img.getWidth(); x += 20) {
g2d.fillRect(offset + x, y, 10, 10);
}
row++;
}
g2d.drawImage(original, 0, 0, this);
g2d.dispose();
return img;
}

@Override
public Dimension getPreferredSize() {
return img == null ? new Dimension(100, 100) : new Dimension(img.getWidth(), img.getHeight());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
int x = (getWidth() - img.getWidth()) / 2;
int y = (getHeight() - img.getHeight()) / 2;
g.drawImage(img, x, y, this);
}
}

}
}

I guess I didn't explain best. What I meant is, when the background is checkered (meaning it's a transparent image), the background should be clear and just have whatever has been painted in the background behind it. However right now, it will just fill the rest of the image height and width that are empty pixels with black? How do I prevent that? I was told by a friend it has to do with the alpha?

如果您能提供一张您想要的和您拥有的图片,我会非常高兴;P

我修改了上面的代码,使用透明图像作为默认基础,清除背景并按照之前的方式绘制其余部分...我还更改了 TestPane 的背景颜色来验证它; )

Transparent

protected BufferedImage makeImageFrom(BufferedImage original) {
BufferedImage img = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setBackground(new Color(255, 255, 255, 0));
g2d.clearRect(0, 0, img.getHeight(), img.getHeight());
g2d.setColor(Color.LIGHT_GRAY);
int row = 0;
for (int y = 0; y < img.getHeight(); y += 10) {
int offset = (row % 2 == 0) ? 10 : 0;
for (int x = 0; x < img.getWidth(); x += 20) {
g2d.fillRect(offset + x, y, 10, 10);
}
row++;
}
g2d.drawImage(original, 0, 0, this);
g2d.dispose();
return img;
}

关于java - Java图形中如何使透明度透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46574515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com