📄 simplebasisamsimagebutton.java
字号:
/* * Portions Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *//** * Copyright(c) 1997 DTAI, Incorporated (http://www.dtai.com) * * All rights reserved * * Permission to use, copy, modify and distribute this material for * any purpose and without fee is hereby granted, provided that the * above copyright notice and this permission notice appear in all * copies, and that the name of DTAI, Incorporated not be used in * advertising or publicity pertaining to this material without the * specific, prior written permission of an authorized representative of * DTAI, Incorporated. * * DTAI, INCORPORATED MAKES NO REPRESENTATIONS AND EXTENDS NO WARRANTIES, * EXPRESS OR IMPLIED, WITH RESPECT TO THE SOFTWARE, INCLUDING, BUT * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR ANY PARTICULAR PURPOSE, AND THE WARRANTY AGAINST * INFRINGEMENT OF PATENTS OR OTHER INTELLECTUAL PROPERTY RIGHTS. THE * SOFTWARE IS PROVIDED "AS IS", AND IN NO EVENT SHALL DTAI, INCORPORATED OR * ANY OF ITS AFFILIATES BE LIABLE FOR ANY DAMAGES, INCLUDING ANY * LOST PROFITS OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES RELATING * TO THE SOFTWARE. */package com.sun.jumpimpl.presentation.simplebasis;import java.awt.AWTEventMulticaster;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Image;import java.awt.Insets;import java.awt.MediaTracker;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.image.FilteredImageSource;import java.awt.image.RGBImageFilter;public class SimpleBasisAMSImageButton extends Component implements FocusListener, MouseListener { public static final int UNPRESSED = 0; public static final int DEPRESSED = 1; public static final int OVER = 2; public static final int DISABLED = 3; private static Font defaultFont = new Font("Serif", Font.BOLD, 12); private static final SimpleBasisAMSBorder defaultUnpressedBorder = new DefaultJUMPGuiAMSImageButtonBorder(false); private static final SimpleBasisAMSBorder defaultArmedBorder = new DefaultJUMPGuiAMSImageButtonBorder(true); private String label = null; private int labelX = -1; private int labelY = -1; private Dimension prefSize = null; private boolean paintBorders = false; private boolean labelDisplay = true; private MediaTracker tracker = null; private Image images[] = new Image[4]; private SimpleBasisAMSBorder borders[] = new SimpleBasisAMSBorder[4]; private boolean generatedDisabled = false; private int buttonState = UNPRESSED; private ActionListener actionListener = null; private Color textColor = Color.white; private Font currentFont = null; private boolean textShadow = false; private boolean mousedown = false; private boolean lastFocused = false; private boolean focusable = true; /** * Constructs an SimpleBasisAMSImageButton */ public SimpleBasisAMSImageButton() { tracker = new MediaTracker(this); setUnpressedBorder(defaultUnpressedBorder); setDepressedBorder(defaultArmedBorder); addFocusListener(this); addMouseListener(this); } /** * Constructs an SimpleBasisAMSImageButton with the given image. * * @param image the image for all states of the button * (until other images are assigned) */ public SimpleBasisAMSImageButton(Image image) { this(); setUnpressedImage(image); } public SimpleBasisAMSImageButton(String label) { this(); setLabel(label); } public SimpleBasisAMSImageButton(Image image, String label) { this(image); setLabel(label); } public void doAction() { ActionEvent ae = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ""); actionListener.actionPerformed(ae); } public void setFocusable(boolean val) { focusable = val; } public boolean isFocusable() { return focusable; } public boolean isEnabled() { return true; } public boolean lastFocused() { return lastFocused; } public void focusGained(FocusEvent e) { //paintBorders = true; repaint(); lastFocused = true; } public void focusLost(FocusEvent e) { //paintBorders = false; repaint(); } /* public SimpleBasisAMSImageButton(Image image, int width, int height) { this(image); prefSize = new Dimension(width, height); } */ /* public SimpleBasisAMSImageButton(Image image, String label, int width, int height) { this(image); setLabel(label); prefSize = new Dimension(width, height); } */ /** * Used internally to add the Image to the array and the MediaTracker, * start loading the image if necessary via the tracker's "checkID", and * repaint if necessary. * * @param id the buttonState id (also used as image id for the MediaTracker) * @param image the image, which is not supposed to be null */ private synchronized void setImage(int id, Image image) { if (images[id] != image) { images[id] = image; if (image != null) { tracker.addImage(image, id); tracker.checkID(id, true); } if (buttonState == id) { repaint(); } } } public void setTextShadow(boolean val) { textShadow = val; } public void setFont(Font f) { currentFont = f; } public void setTextColor(Color c) { textColor = c; repaint(); } public void setPaintBorders(boolean val) { paintBorders = val; } public void setLabelDisplay(boolean val) { labelDisplay = val; } /** * Sets the image to display when the button is not pressed or hilited * because of a mouse-over. This image is also used in those other cases * if no alternative image is requested. * * @param image the unarmed image */ public void setUnpressedImage(Image image) { setImage(UNPRESSED, image); if (images[DEPRESSED] == null) { setDepressedImage(image); } if (images[OVER] == null) { setOverImage(image); } if ( (images[DISABLED] == null) || generatedDisabled) { setDisabledImage(null); } } /** * Sets the image to display when the button is pressed and the mouse * is still over the button. * * @param image the armed image */ public void setDepressedImage(Image image) { if (image != null) { setImage(DEPRESSED, image); } else { setImage(DEPRESSED, images[UNPRESSED]); } } /** * Sets the image to display when the button is not pressed and the mouse * is over the button. * * @param image the over image */ public void setOverImage(Image image) { if (image != null) { setImage(OVER, image); } else { setImage(OVER, images[UNPRESSED]); } } /** * Sets the image to display when the button is disabled. * * @param image the disabled image */ public void setDisabledImage(Image image) { generatedDisabled = false; if ( (image == null) && (images[UNPRESSED] != null)) { generatedDisabled = true; image = createImage(new FilteredImageSource(images[UNPRESSED]. getSource(), new DisableImageFilter())); } setImage(DISABLED, image); } /** * Gets the image to display when the button is not pressed or hilited * because of a mouse-over. This image is also used in those other cases * if no alternative image is requested. * * @return the unarmed image */ /* public Image getUnpressedImage() { return (images[UNPRESSED]); } */ /** * Gets the image to display when the button is pressed and the mouse * is still over the button. * * @return the armed image */ /* public Image getDepressedImage() { return (images[DEPRESSED]); } */ /** * Gets the image to display when the button is not pressed and the mouse * is over the button. * * @return the over image */ /* public Image getOverImage() { return (images[OVER]); } */ /** * Gets the image to display when the button is disabled. * * @return the armed image */ /* public Image getDisabledImage() { return (images[DISABLED]); } */ /** * Used internally to add the Border to the array and repaint if necessary. * * @param id the buttonState, used to index the array * @param border the Border, which is not supposed to be null */ private synchronized void setBorder(int id, SimpleBasisAMSBorder border) { if (borders[id] != border) { borders[id] = border; if (buttonState == id) { repaint(); } } } /** * Sets the border to display when the button is not pressed or hilited * because of a mouse-over. This border is also used in those other cases * if no alternative border is requested. * * @param border the unarmed border */ public void setUnpressedBorder(SimpleBasisAMSBorder border) { setBorder(UNPRESSED, border); if (borders[DEPRESSED] == null) { setDepressedBorder(border); } if (borders[OVER] == null) { setOverBorder(border); } if (borders[DISABLED] == null) { setDisabledBorder(border); } } /** * Sets the border to display when the button is pressed and the mouse * is still over the button. * * @param border the armed border */ public void setDepressedBorder(SimpleBasisAMSBorder border) { if (border != null) { setBorder(DEPRESSED, border); } else { setBorder(DEPRESSED, borders[UNPRESSED]); } } /** * Sets the border to display when the button is not pressed and the mouse * is over the button.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -