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