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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
| import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.TexturePaint;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import org.jdesktop.swingx.JXFrame;
/**
*
*/
/**
* @author Sinok
*
*/
public class MyLabel extends JLabel implements ComponentListener{
private TexturePaint backgroundPaint;
private Shape backgroundShape;
private Color borderColor = Color.darkGray;
/**
*
*/
public MyLabel() {
super();
init();
// TODO Raccord de constructeur auto-généré
}
/**
* @param image
* @param horizontalAlignment
*/
public MyLabel(Icon image, int horizontalAlignment) {
super(image, horizontalAlignment);
init();
// TODO Raccord de constructeur auto-généré
}
/**
* @param image
*/
public MyLabel(Icon image) {
super(image);
init();
// TODO Raccord de constructeur auto-généré
}
/**
* @param text
* @param icon
* @param horizontalAlignment
*/
public MyLabel(String text, Icon icon, int horizontalAlignment) {
super(text, icon, horizontalAlignment);
init();
// TODO Raccord de constructeur auto-généré
}
/**
* @param text
* @param horizontalAlignment
*/
public MyLabel(String text, int horizontalAlignment) {
super(text, horizontalAlignment);
init();
// TODO Raccord de constructeur auto-généré
}
/**
* @param text
*/
public MyLabel(String text) {
super(text);
init();
// TODO Raccord de constructeur auto-généré
}
private GradientPaint prepareUpperGradient() {
return new GradientPaint(0,0,Color.lightGray,0,getHeight()/2,Color.white);
}
private GradientPaint prepareLowerGradient() {
return new GradientPaint(0,getHeight()/2,Color.white,0,getHeight(),Color.lightGray);
}
private Shape prepareShape() {
Border b= this.getBorder();
Insets insets = new Insets(0,0,0,0);
if(b != null) {
insets = b.getBorderInsets(this);
}
System.out.println("width "+getWidth()+" height "+getHeight());
System.out.println("insets "+insets.toString());
return new RoundRectangle2D.Double(insets.left,insets.top,getWidth()-insets.right*2,getHeight()-insets.bottom*2,10,10);
}
private TexturePaint prepareBackgroundPaint() {
GradientPaint upperGradient = prepareUpperGradient();
GradientPaint lowerGradient = prepareLowerGradient();
BufferedImage im = new BufferedImage(getWidth(), getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D imG2d = im.createGraphics();
imG2d.setPaint(upperGradient);
imG2d.fill(new Rectangle2D.Double(0,0,getWidth(),getHeight()/2));
imG2d.setPaint(lowerGradient);
imG2d.fill(new Rectangle2D.Double(0,getHeight()/2,getWidth(),getHeight()));
imG2d.dispose();
Rectangle2D rect = new Rectangle2D.Double(0,0,im.getWidth(), im.getHeight());
return new TexturePaint(im,rect);
}
/* (non-Javadoc)
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
@Override
protected void paintComponent(Graphics g) {
// TODO Raccord de méthode auto-généré
if(isShowing()) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g2d);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if(backgroundPaint == null) {
backgroundPaint = prepareBackgroundPaint();
}
if(backgroundShape == null) {
backgroundShape = prepareShape();
}
g2d.setPaint(backgroundPaint);
g2d.fill(backgroundShape);
super.paintComponent(g2d);
}
}
/* (non-Javadoc)
* @see javax.swing.JComponent#paintBorder(java.awt.Graphics)
*/
@Override
protected void paintBorder(Graphics g) {
// TODO Raccord de méthode auto-généré
if(isShowing()) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(borderColor);
g2d.draw(backgroundShape);
super.paintBorder(g);
}
}
public void componentHidden(ComponentEvent e) {
// TODO Raccord de méthode auto-généré
}
public void componentMoved(ComponentEvent e) {
backgroundPaint = prepareBackgroundPaint();
backgroundShape = prepareShape();
repaint();
}
public void componentResized(ComponentEvent e) {
backgroundPaint = prepareBackgroundPaint();
backgroundShape = prepareShape();
repaint();
}
public void componentShown(ComponentEvent e) {
// TODO Raccord de méthode auto-généré
}
private void init() {
this.addComponentListener(this);
setOpaque(false);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Raccord de méthode auto-généré
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
MyLabel l = new MyLabel("toto");
l.setHorizontalAlignment(JLabel.CENTER);
l.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
f.add(l);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
});
}
} |
Partager