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));
}
}
}
}
这是我的代码。基本上我不希望它绘制像素,如果它是一个透明像素(透明像素将是一个像素,当你在谷歌上查看图像时,它是背景的棋盘)。我究竟做错了什么?目前这些图像的背景只是黑色,所以像素确实被绘制出来了吗?...
答案 0 :(得分:1)
我要做的是首先绘制背景,然后在上面绘制图像,类似......
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;
}
用于测试它的代码......
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);
}
}
}
}
我想我没有解释得最好。我的意思是,当背景格格不入时(意味着它是一个透明的图像)时,背景应该是清晰的,只要在背后的背景中绘制任何东西。但是现在,它只会填充剩余的图像高度和宽度,这些是带有黑色的空像素?我该如何预防呢?朋友告诉我它与alpha有关吗?
如果你能提供你想要的东西和你拥有的东西,我真的很高兴; P
我修改了上面的代码,使用透明图像作为默认基础,清除背景并按照之前的方式绘制其余部分......我还更改了TestPane
的背景颜色以验证它;)
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;
}