我有一个 READY 图像(图像的颜色是灰色的,它是 GIVEN 的,即我不在 Java 中创建它,否则我会简单地动态更改颜色)。图像的形状很奇怪,即它不是一个矩形。我有一个透明部分和“图像内容”部分(常规 .png 样式)。我只需要使“图像内容”部分变得更暗。我尝试了以下方法:
public class ImageDarkener extends JFrame {
private JButton button;
private JLabel myImage;
public ImageDarkener()
{
createImageDarkener();
}
private void createImageDarkener() {
Container contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
contentPane.setSize(900,600);
myImage = new JLabel();
myImage.setIcon(new ImageIcon("myImage.png"));
myImage.setBounds(403,31,200,200);
contentPane.add(myImage);
button = new JButton("Go darker");
button.setBounds(90,400, 100, 100);
contentPane.add(button);
button.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event) {
buttonActionPerformed(event);
}
}
);
setTitle("Image gets darker");
setSize(900,600);
setVisible(true);
setLocationRelativeTo(null);
}
private void buttonActionPerformed(ActionEvent event) {
myImage.darker();
// Here I am stuck...I tried many combinations with darker().....etc.
I need to be able to click the button and myImage should get darker and darker
until it is completely black...
}
public static void main(String args[]) {
ImageDarkener img = new ImageDarkener();
img.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}