Java 更改BuffereImage的像素会更改其他BuffereImage的像素
我对Java相当陌生,对在Java中编辑图像也完全陌生。我所拥有的是一幅加载到我的程序中的图像,其中包含一幅32x32的带有三种灰度的叶子图像。我拥有的是叶子缓冲图像,它保存着灰色的叶子精灵。我还有一个绿叶、红叶、橙叶和粉叶的缓冲图像。当我运行下面的代码时,由于某种原因,最后一种颜色覆盖了所有其他颜色,它们都变成橙色。有人能告诉我为什么,以及解决的办法吗Java 更改BuffereImage的像素会更改其他BuffereImage的像素,java,image,replace,pixel,bufferedimage,Java,Image,Replace,Pixel,Bufferedimage,我对Java相当陌生,对在Java中编辑图像也完全陌生。我所拥有的是一幅加载到我的程序中的图像,其中包含一幅32x32的带有三种灰度的叶子图像。我拥有的是叶子缓冲图像,它保存着灰色的叶子精灵。我还有一个绿叶、红叶、橙叶和粉叶的缓冲图像。当我运行下面的代码时,由于某种原因,最后一种颜色覆盖了所有其他颜色,它们都变成橙色。有人能告诉我为什么,以及解决的办法吗 public void colorLeaves() { leaf=ss.grab32Image(4, 1);
public void colorLeaves()
{
leaf=ss.grab32Image(4, 1);
greenLeaf=leaf;
redLeaf=leaf;
pinkLeaf=leaf;
orangeLeaf=leaf;
for(int xx=0;xx<leaf.getWidth();xx++)
{
for(int yy=0;yy<leaf.getHeight();yy++)
{
int clr = leaf.getRGB(xx, yy);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
Color c1=new Color(0,0,0);
Color c2=new Color(0,0,0);
Color c3=new Color(0,0,0);
/*
* GREEN
*/
c1 = new Color(0,158,15); //Middle Shade
c2 = new Color(0,119,11); //Dark Shade
c3 = new Color(0,198,16); //Light Shade
if(red==128&&green==128&&blue==128)
{
greenLeaf.setRGB(xx, yy, c1.getRGB());
}
if(red==96&&green==96&&blue==96)
{
greenLeaf.setRGB(xx, yy, c2.getRGB());
}
if(red==165&&green==165&&blue==165)
{
greenLeaf.setRGB(xx, yy, c3.getRGB());
}
/*
* RED
*/
c1 = new Color(219,26,42); //Middle Shade
c2 = new Color(183,22,35); //Dark Shade
c3 = new Color(247,56,72); //Light Shade
if(red==128&&green==128&&blue==128)
{
redLeaf.setRGB(xx, yy, c1.getRGB());
}
if(red==96&&green==96&&blue==96)
{
redLeaf.setRGB(xx, yy, c2.getRGB());
}
if(red==165&&green==165&&blue==165)
{
redLeaf.setRGB(xx, yy, c3.getRGB());
}
/*
* PINK
*/
c1 = new Color(255,56,152); //Middle Shade
c2 = new Color(216,49,130); //Dark Shade
c3 = new Color(255,102,175); //Light Shade
if(red==128&&green==128&&blue==128)
{
pinkLeaf.setRGB(xx, yy, c1.getRGB());
}
if(red==96&&green==96&&blue==96)
{
pinkLeaf.setRGB(xx, yy, c2.getRGB());
}
if(red==165&&green==165&&blue==165)
{
pinkLeaf.setRGB(xx, yy, c3.getRGB());
}
/*
* ORANGE
*/
c1 = new Color(255,110,38); //Middle Shade
c2 = new Color(224,97,33); //Dark Shade
c3 = new Color(249,136,79); //Light Shade
if(red==128&&green==128&&blue==128)
{
orangeLeaf.setRGB(xx, yy, c1.getRGB());
}
if(red==96&&green==96&&blue==96)
{
orangeLeaf.setRGB(xx, yy, c2.getRGB());
}
if(red==165&&green==165&&blue==165)
{
orangeLeaf.setRGB(xx, yy, c3.getRGB());
}
}
}
}
public void colorLeaves()
{
叶=ss.grab32Image(4,1);
绿叶=叶子;
红叶=叶;
粉叶=叶;
橙叶=叶;
对于(int xx=0;xx 16;
绿色整数=(clr&0x0000ff00)>>8;
int blue=clr&0x000000ff;
颜色c1=新颜色(0,0,0);
颜色c2=新颜色(0,0,0);
颜色c3=新颜色(0,0,0);
/*
*绿色的
*/
c1=新颜色(0158,15);//中间色调
c2=新颜色(0119,11);//暗色调
c3=新颜色(0198,16);//浅色
如果(红色==128&&绿色==128&&蓝色==128)
{
greenLeaf.setRGB(xx,yy,c1.getRGB());
}
如果(红色==96&&绿色==96&&蓝色==96)
{
greenLeaf.setRGB(xx,yy,c2.getRGB());
}
如果(红色==165&&绿色==165&&蓝色==165)
{
greenLeaf.setRGB(xx,yy,c3.getRGB());
}
/*
*红色的
*/
c1=新颜色(219,26,42);//中间色调
c2=新颜色(183,22,35);//深色
c3=新颜色(247,56,72);//浅色
如果(红色==128&&绿色==128&&蓝色==128)
{
setRGB(xx,yy,c1.getRGB());
}
如果(红色==96&&绿色==96&&蓝色==96)
{
setRGB(xx,yy,c2.getRGB());
}
如果(红色==165&&绿色==165&&蓝色==165)
{
setRGB(xx,yy,c3.getRGB());
}
/*
*粉红的
*/
c1=新颜色(255,56152);//中间阴影
c2=新颜色(216,49130);//深色
c3=新颜色(255102175);//浅色
如果(红色==128&&绿色==128&&蓝色==128)
{
setRGB(xx,yy,c1.getRGB());
}
如果(红色==96&&绿色==96&&蓝色==96)
{
setRGB(xx,yy,c2.getRGB());
}
如果(红色==165&&绿色==165&&蓝色==165)
{
setRGB(xx,yy,c3.getRGB());
}
/*
*橙色的
*/
c1=新颜色(255110,38);//中间色调
c2=新颜色(224,97,33);//暗色调
c3=新颜色(249136,79);//浅色
如果(红色==128&&绿色==128&&蓝色==128)
{
setRGB(xx,yy,c1.getRGB());
}
如果(红色==96&&绿色==96&&蓝色==96)
{
setRGB(xx,yy,c2.getRGB());
}
如果(红色==165&&绿色==165&&蓝色==165)
{
setRGB(xx,yy,c3.getRGB());
}
}
}
}
redLeaf
、greenLeaf
、pinkLeaf
和orangelaf
对同一对象的所有引用。因此,对其中一个对象所做的任何修改都将更改所有其他对象。这就是为什么只会发生最后一次更改
您可以看到有关如何克隆缓冲映像的以下线程:感谢您的快速响应,它工作得非常好。:D