1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
public class MSS_Button extends JComponent implements ActionListener {
public static final long serialVersionUID = 1;
/** The initial size of a button. */
public static final int BUTTON_INIT_SIZE = 60;
/** The final size of a button. */
public static final int BUTTON_FINAL_SIZE = 80;
/** The actual size of button. */
public int size = 60;
/** A boolean which says if we are zooming in. */
private boolean isZoomIn = false;
/** A boolean which says if we are zooming out. */
private boolean isZoomOut = false;
/** The button picture url. */
private ImageIcon icon = null;
/** The transparency of picture to display. */
private float transparency = 0.4f;
/**
* Constructor.
* @param picture The url to the picture to display on the button.
*/
public MSS_Button(URL picture) {
super();
icon = new ImageIcon(picture);
this.setOpaque(false);
this.setSize(BUTTON_INIT_SIZE, BUTTON_INIT_SIZE);
this.setPreferredSize(new Dimension(BUTTON_INIT_SIZE, BUTTON_INIT_SIZE));
this.addMouseListener(new MSS_Button_MouseListener());
}
/**
* Set the current transparency to use
* for displaying the button.
*
* @param trans The transparecy to use.
*/
public void setTransparency(float trans)
{
transparency = trans;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,transparency));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(icon.getImage(),0,0,getWidth(),getHeight(),null);
g2d.dispose();
}
/**
* This class implements the graphical behaviour of an MSS_ActionButton
* @author Thomas Bonavia
*
* @see MouseListener.
* @see MSS_ActionButton.
*/
private class MSS_Button_MouseListener extends MouseAdapter
{
/**
* When the mouse first button is pressed on the {@code MSS_ActionButton},
* The icon becomes opaque.
* @param arg0 The mouse event.
*/
@Override
public void mousePressed(MouseEvent arg0) {
MSS_Button b = (MSS_Button)arg0.getSource();
if(arg0.getButton() == MouseEvent.BUTTON1)
{
b.setTransparency(1.0f);
}
b.repaint();
}
/**
* When the mouse first button is release on the {@code MSS_ActionButton},
* The icon becomes semi-transparent (like in {@code MouseEntered}).
* @param arg0 The mouse event.
*/
@Override
public void mouseReleased(MouseEvent arg0) {
MSS_Button b = (MSS_Button)arg0.getSource();
if(arg0.getButton() == MouseEvent.BUTTON1)
{
b.setTransparency(0.7f);
}
b.repaint();
}
/**
* When the mouse enter on the {@code MSS_ActionButton},
* The icon becomes semi-transparent.
* @param arg0 The mouse event.
*/
@Override
public void mouseEntered(MouseEvent arg0) {
MSS_Button b = (MSS_Button)arg0.getSource();
b.setTransparency(0.7f);
isZoomIn = true;
isZoomOut= false;
}
/**
* When the mouse exit the {@code MSS_ActionButton},
* The icon becomes more transparent.
* @param arg0 The mouse event.
*/
@Override
public void mouseExited(MouseEvent arg0) {
MSS_Button b = (MSS_Button)arg0.getSource();
b.setTransparency(0.4f);
isZoomIn = false;
isZoomOut= true;
}
@Override
public void mouseClicked(MouseEvent arg0) {}
}
public void actionPerformed(ActionEvent e) {
if(size <= BUTTON_FINAL_SIZE && isZoomIn)
{
size = size+2;
}
if(size >= BUTTON_INIT_SIZE && isZoomOut)
{
size = size-2;
}
this.setSize(new Dimension(size,size));
}
} |
Partager