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的东西)。对不起,如果这已被问及之前,并感谢您的任何帮助。Darken JButton(s)悬停/点击
** 编辑 **
我试图做到这一点,但它只是将图像空白:
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;
}
tmpCol =新颜色(img。的getRGB()); BufferedImage类型中的getRGB(int,int)方法不适用于参数() – KisnardOnline 2012-04-24 21:52:23
非常确定它应该是getRGB(x,y);没有试图成为一个痛苦,我真的很感谢帮助,只是想纠正任何人谁发现这个职位 – KisnardOnline 2012-04-24 21:54:54
而是,使用'RescaleOp',说明[这里](http://stackoverflow.com/a/10208380/) 230513)。 – trashgod 2012-04-24 23:22:33