gfive | Alors, un bouton qui affiche une image et qui clignote, en AWT :
(en fait, il peut aussi 'clignoter' entre 2 images) : Code :
- package atchik.util.gwidgets.alert;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- public class BlinkingImageButton extends BlinkingButton implements Runnable, BlinkingComponent, MouseListener {
- private Vector listeners;
- private Image imgOn;
- private Image imgOff;
- private String actionCommand;
-
- private Point onOrigin;
- private Point offOrigin;
- private boolean enabled;
-
- public BlinkingImageButton(Image imageOn, Image imageOff, Dimension d) {
- this.imgOn = imageOn;
- this.imgOff = imageOff;
- this.addMouseListener(this);
- this.enabled = true;
- onOrigin = null;
- offOrigin = null;
- setCursor(new Cursor(Cursor.HAND_CURSOR));
- listeners = new Vector();
- setSize(d);
- }
-
- public void setEnabled(boolean enabled) {
- this.enabled = enabled;
- if (enabled) {
- setCursor(new Cursor(Cursor.HAND_CURSOR));
- } else {
- setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
- }
- }
-
- public void addActionListener(ActionListener l) {
- listeners.addElement(l);
- }
-
- public void removeActionListener(ActionListener l) {
- listeners.remove(l);
- }
-
- /** Called when the button is clicked */
- public void buttonClicked() {
- if (enabled) {
- for (int i = 0; i < listeners.size(); i++) {
- ((ActionListener)listeners.elementAt(i)).actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
- }
- }
- }
-
- public void setActionCommand(String s) {
- actionCommand = s;
- }
-
- public String getActionCommand() {
- return actionCommand;
- }
-
- public void paint(Graphics g) {
- g.setColor(getBackground());
- g.fillRect(0, 0, getSize().width, getSize().height);
- if (imgOn != null) {
- if (onOrigin == null) {
- onOrigin = new Point(1 + ((getSize().width - imgOn.getWidth(this)) / 2),
- 1 + ((getSize().height - imgOn.getHeight(this)) / 2));
- } else {
- onOrigin = new Point(0, 0);
- }
- }
- if (imgOff != null) {
- if (offOrigin == null) {
- offOrigin = new Point(1 + ((getSize().width - imgOff.getWidth(this)) / 2),
- 1 + ((getSize().height - imgOff.getHeight(this)) / 2));
- } else {
- offOrigin = new Point(0, 0);
- }
- }
- if (on) {
- if (imgOn != null) {
- g.drawImage(imgOn, onOrigin.x, onOrigin.y, getBackground(), this);
- }
- } else {
- if (imgOff != null) {
- g.drawImage(imgOff, offOrigin.x, offOrigin.y, getBackground(), this);
- }
- }
- }
-
-
- public void mouseClicked(MouseEvent e) {
- buttonClicked();
- }
-
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- }
|
|