All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.util.swing.button.ImageButton Maven / Gradle / Ivy

There is a newer version: 13.03-beta
Show newest version
/**
 * Copyright (C) 2010-2012 Agrosense [email protected]
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.util.swing.button;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import javax.swing.JButton;

/**
 * Button with an add icon displayed on it
 * @author Merijn Zengers
 */
public class ImageButton extends JButton {

    private Image image;
    private Image selectedImage;
    private Image disabledImage;
    private int w, h;
    private Color backGround;

    /**
     * Constructs an ImageButton with the specified image
     * Will take the height and with of the image as dimensions for the button
     * Any unfilled pixels will be filled with the supplied background
     * @param image The Image to draw
     * @param selectedImage the image to draw if the button is selected
     * @param backGround The Color of any unfilled pixels
     * @param toolTip the tool tip of the button
     */
    public ImageButton(Image image, Image selectedImage, Image disabledImage, Color backGround, String toolTip) {
        this.image = image;
        this.selectedImage = selectedImage;
        this.disabledImage = disabledImage;
        w = image.getWidth(null);
        h = image.getHeight(null) ;
        this.backGround = backGround;
        this.setMargin(new Insets(0, 0, 0, 0));
        this.setToolTipText(toolTip);
        this.setBorderPainted(false);
    }

    @Override
    public void paint(Graphics g) {
        super.paintComponent(g);

        if(!isEnabled()){
            g.drawImage(disabledImage, 0, 0, backGround, null);
        } else if (isFocusPainted() && isFocusOwner() && selectedImage != null) {
            g.drawImage(selectedImage, 0, 0, backGround, null);
        } 
        else {
            g.drawImage(image, 0, 0, backGround, null);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(w, h);
    }

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(w, h);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy