在悬停/点击时使JButton变暗

时间:2012-04-24 19:51:29

标签: java swing mouseover jbutton imageicon

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

3 个答案:

答案 0 :(得分:5)

有两种方式

1)使用built-in methods in JButtons API

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

2)使用ButtonModel

答案 1 :(得分: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)中创建明暗图像并在启动时加载它们。上面的代码将是流程密集型的,会降低您的应用程序运行时间。

答案 2 :(得分:0)

查看此tutorial。您可能对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) {}

}