gpt4 book ai didi

java - 如何在BufferedImage上多次实现actionListener

转载 作者:行者123 更新时间:2023-12-01 18:32:03 25 4
gpt4 key购买 nike

我目前正在 GUI 中设计一个图像编辑器,它有几个按钮,单击这些按钮时,应该对加载的图像应用不同的效果。我的效果与 ActionListener 一起使用,但如果我在单击另一个效果后尝试单击一个效果,图像会变成黑色,而不是应用所需的效果。如何对同一张图像应用多种效果?这是我的 ButtonPanel 类的代码,其中包括 ActionListener 和效果代码(到目前为止仅实现了其中三个效果):

class ButtonPanel extends JPanel
{
//JButton makeBlue;
BufferedImage img;
ImagePanel ip = new ImagePanel();
ButtonPanel(ImagePanel x)
{
final JButton makeBlue = new JButton ("Make Blue");
final JButton makeRed = new JButton ("Make Red");
final JButton makeGreen = new JButton ("Make Green");
final JButton invertColors = new JButton ("Invert Colors");
final JButton makeGreyscale = new JButton ("Greyscale");
final JButton makeWarhol = new JButton ("Warhol");
final JButton flipVertical = new JButton ("Flip Vertical");
final JButton flipHorizontal = new JButton ("Flip Horizontal");
final JButton rotateClockwise = new JButton ("Rotate Clockwise");
setBackground(Color.RED);
ip = x;
//int width, height;
//setBackground(Color.RED);
//int height = 0;
add(makeBlue);
add(makeRed);
add(makeGreen);
add(invertColors);
add(makeGreyscale);
add(makeWarhol);
add(flipVertical);
add(flipHorizontal);
add(rotateClockwise);
ActionListener action =
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BufferedImage img = ip.getImg();

if (e.getSource()== makeBlue)
{


int width = img.getWidth(); //# of pixel columns
int height = img.getHeight(); //# of pixels rows


for(int i=0; i<width;i++)
{
for(int j=0; j<height; j++)
{

int value = img.getRGB(i,j);
int y = 0xFF0000FF;
value = value & y;
img.setRGB(i,j,value);

}
}
ip.setImage(img);

setBackground(Color.GREEN);
repaint();

}
else if(e.getSource()== makeRed)
{
int width = img.getWidth(); //# of pixel columns
int height = img.getHeight(); //# of pixels rows


for(int i=0; i<width;i++)
{
for(int j=0; j<height; j++)
{
//fill in code here
int value = img.getRGB(i,j);
int y = 0xFFFF0000;
value = value & y;

//System.out.println(value);
img.setRGB(i,j,value);
}
}
ip.setImage(img);
repaint();
}
else if(e.getSource()== makeGreen)
{
int width = img.getWidth(); //# of pixel columns
int height = img.getHeight(); //# of pixels rows


for(int i=0; i<width;i++)
{
for(int j=0; j<height; j++)
{
//fill in code here
int value = img.getRGB(i,j);
int y = 0xFF00FF00;
value = value & y;
img.setRGB(i,j,value);
}
}
ip.setImage(img);
repaint();
}

}

};

makeBlue.addActionListener(action);
makeRed.addActionListener(action);
makeGreen.addActionListener(action);
makeWarhol.addActionListener(action);
flipVertical.addActionListener(action);
flipHorizontal.addActionListener(action);
rotateClockwise.addActionListener(action);
makeGreyscale.addActionListener(action);
invertColors.addActionListener(action);
}

}

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

您的问题是,您要从图像中提取所有一种颜色,然后用新提取的图像替换 ImagePanel 中的原始图像,也就是说,如果按下绿色按钮,则全部为绿色。然后,当您尝试从 ImagePanel 的图像中提取红色时,您会得到全黑,因为图像现在只有绿色。

如果你想提取原始颜色,你需要将原始图像保存在某个地方。也许您应该为 ImagePanel 提供一个方法,getOriginalImage() 或类似的方法,并让它存储两张图像,一张是显示的图像,一张是更改后的图像。

更好的是,使用模型- View -控制模式并将图像存储在 GUI 之外的其他位置。

关于java - 如何在BufferedImage上多次实现actionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23727379/

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