0
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;
}
4

3 回答 3

5

有两种方法

1)在 JButtons API 中使用内置方法

button.setIcon((Icon i));
button.setRolloverIcon((Icon i));
button.setPressedIcon(Icon i));
button.setDisabledIcon(Icon i));

2)使用按钮模型

于 2012-04-24T21:07:30.837 回答
1

此函数将返回一个带有颜色偏移的 BufferedImage(即更亮/更暗)

public BufferedImage shiftColor(BufferedImage img, int rShift, int gShift,int bShift) {
    Color tmpCol;
    for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
            tmpCol=new Color(img.getRGB(x,y));
            tmpCol=new Color(tmpCol.getRed()-rShift,tmpCol.getGreen()-gShift,tmpCol.getBlue()-bShift);
            img.setRGB(x,y,tmpCol.getRGB());
        }
    }
    return img;
}

即使这可行,我仍然建议在图像编辑器(即 Photoshop)中创建明暗图像并在启动时加载两者。上面的代码将是进程密集型的,并且会减慢您的应用程序运行时间。

于 2012-04-24T21:32:00.137 回答
0

看看这个教程。您可能对 mouseEntered 感兴趣。由于不是在 Java 中更改背包的颜色,而是考虑 2 个图像,一个是浅色背包,另一个是深色背包。将鼠标悬停在按钮上时更改它们。

public class MouseEventDemo implements MouseListener {
    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.addMouseListener(this);
    addMouseListener(this);

    backpackButton.setIcon(backpackImageIcon);
    backpackButton.setToolTipText("Backpack[B]");
    panel.add(backpackButton);  

    public void mousePressed(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {
       //set button color to dark color
    }
    public void mouseExited(MouseEvent e) {
       //set button color to light color
    }
    public void mouseClicked(MouseEvent e) {}

}
于 2012-04-24T20:02:53.877 回答