我正在为一个摇摆项目工作,我必须多次显示相同的图像,每次更改像素颜色。
例如:第一个图像必须以蓝色显示,第二个图像必须以橙色显示,但问题是当显示第二个图像时,它也会以橙色改变第一个图像的颜色。如何以其颜色显示每个图像?
谢谢。
private void drawPixel(int index,String name) throws IOException {
File input = new File("map-pointer-clipart-3.png");
BufferedImage imagePointer = ImageIO.read(input);
Graphics g = this.imagePoints.getGraphics();
changeColorPixelLabel(imagePointer,labelClassesCount-1);
int x = (index % this.width);
int y = (index / this.width);
g.drawImage(imagePointer,x-20, y-31,100,100, null);
repaint();
}
private void changeColorPixelLabel(BufferedImage img, int index) {
for(int i=0; i<img.getWidth(); i++) {
for(int j=0; j<img.getHeight(); j++) {
Color c = labelConstraintColor.get(index);
if(img.getRGB(i, j) == new Color(255,255,255).getRGB()) {
img.setRGB(i, j, c.getRGB());
}
}
}
}
答案 0 :(得分:1)
创建BufferedImage
的第二个实例,将原始图像绘制到它......
BufferedImage copy= new BufferedImage(imagePointer.getWidth(), imagePointer.getHeight(), BufferedImage.TYPE_ARGB);
Graphics2D g2d = copy.createGraphics();
g2d.drawImage(0, 0, imagePointer, null);
g2d.dispose();
并且知道你有一个可以操作的新副本