ImageIcon backpackImageIcon = new ImageIcon("images/gui/button_backpack.png");
JButton backpackButton = new JButton();
backpackButton.setBounds(660,686,33,33);
backpackButton.setBorderPainted(false);
backpackButton.setFocusPainted(false);
backpackButton.setVisible(true);
backpackButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("B"), "backpackButtonPress");
backpackButton.getActionMap().put("backpackButtonPress", ClassBackpackButton);
backpackButton.setAction(ClassBackpackButton);
backpackButton.setIcon(backpackImageIcon);
backpackButton.setToolTipText("Backpack[B]");
panel.add(backpackButton);
我有多个按钮以这种方式设置。我希望能够做的是让它们在悬停时变暗 10%,在点击时可能变暗 20%。我试图四处寻找如何做到这一点,但没有运气(只找到了 javascript 的东西)。抱歉,如果之前有人问过这个问题,感谢您的帮助。
**编辑**
我试图这样做,但它只是将图像变为空白:
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(new File("images/gui/button_backpack.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedImage darkerBackpackBufferedImage = new BufferedImage(32, 32, BufferedImage.TYPE_BYTE_INDEXED);
RescaleOp op = new RescaleOp(1.3f, 0, null);
darkerBackpackBufferedImage = op.filter(bufferedImage, null);
ImageIcon darkerBackpackImageIcon = new ImageIcon((Image) darkerBackpackBufferedImage);
backpackButton.setRolloverIcon((ImageIcon) darkerBackpackImageIcon);
**编辑** 与解决方案
这是我为阅读以上内容的任何人使用的修改后的 shiftColor 函数......祝你好运:)
public BufferedImage shiftColor(BufferedImage img, int rShift, int gShift, int bShift) {
Color tmpCol;
int tmpRed, tmpGreen, tmpBlue;
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
tmpCol=new Color(img.getRGB(x,y));
tmpRed = (tmpCol.getRed()-rShift < 0) ? 0 : tmpCol.getRed()-rShift; //if shifted color is less than 0 change to 0
tmpGreen = (tmpCol.getGreen()-gShift < 0) ? 0 : tmpCol.getGreen()-gShift; //if shifted color is less than 0 change to 0
tmpBlue = (tmpCol.getBlue()-bShift < 0) ? 0 : tmpCol.getBlue()-bShift; //if shifted color is less than 0 change to 0
tmpCol=new Color(tmpRed, tmpGreen, tmpBlue);
img.setRGB(x,y,tmpCol.getRGB());
}
}
return img;
}