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
|
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
public class MonBorder implements Border
{
public static final int TOP = 1;
public static final int LEFT = 2;
public static final int BOTTOM = 3;
public static final int RIGHT = 4;
public static final float LEADING = 0.0f;
public static final float CENTER = 0.5f;
public static final float TRAILING = 1.0f;
private JComponent component;
private int side;
private float alignment;
private Insets borderInsets = new Insets(0, 0, 0, 0);
//private int gap = 5;
private int gap = 0;
public MonBorder(JComponent component, int side, float alignment )
{
if (side == TOP
|| side == LEFT
|| side == BOTTOM
|| side == RIGHT)
{
this.side = side;
}
else
{
String message = "Side doit être TOP, LEFT, BOTTOM, ou RIGHT";
throw new IllegalArgumentException(message);
}
this.component = component;
component.setSize( component.getPreferredSize() );
this.alignment = alignment > 1.0f ? 1.0f : alignment < 0.0f ? 0.0f : alignment;
updateAndAlign();
}
private void updateAndAlign()
{
if (side == TOP)
{
borderInsets.top = component.getPreferredSize().height + gap;
component.setAlignmentX(alignment);
component.setAlignmentY(0.0f);
}
else if (side == BOTTOM)
{
borderInsets.bottom = component.getPreferredSize().height + gap;
component.setAlignmentX(alignment);
component.setAlignmentY(1.0f);
}
else if (side == LEFT)
{
borderInsets.left = component.getPreferredSize().width + gap;
component.setAlignmentX(0.0f);
component.setAlignmentY(alignment);
}
else if (side == RIGHT)
{
borderInsets.right = component.getPreferredSize().width + gap;
component.setAlignmentX(1.0f);
component.setAlignmentY(alignment);
}
}
@Override
public Insets getBorderInsets(Component c)
{
return borderInsets;
}
@Override
public boolean isBorderOpaque()
{
return false;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
{
float x2 = (width - component.getWidth()) * component.getAlignmentX() + x;
float y2 = (height - component.getHeight()) * component.getAlignmentY() + y;
component.setLocation((int)x2, (int)y2);
}
/*
* Remplace le Border existant avec un CompoundBorder contenant le
* Border original et le ComponentBorder
*/
public void installe(JComponent parent)
{
adjusteBorder(parent);
Border current = parent.getBorder();
if (current == null)
{
parent.setBorder(this);
}
else
{
CompoundBorder compound = new CompoundBorder(current, this);
parent.setBorder(compound);
}
parent.add(component);
}
/*
* Le nouveau Border doit être ajusté pour permettre au composant
* de s'insérer dans les limites du composant parent
*/
private void adjusteBorder(JComponent parent)
{
Insets parentInsets = parent.getInsets();
// ajustement de la hauteur
if (side == RIGHT || side == LEFT)
{
int parentHeight = parent.getPreferredSize().height - parentInsets.top - parentInsets.bottom;
int diff = component.getHeight() - parentHeight;
if (diff > 0)
{
if (alignment == LEADING)
{
borderInsets.bottom += diff;
}
else if (alignment == TRAILING)
{
borderInsets.top += diff;
}
else
{
int half = diff / 2;
borderInsets.top += half;
borderInsets.bottom += (diff - half);
}
}
}
// ajustement de la largeur
if (side == TOP || side == BOTTOM)
{
int parentWidth = parent.getPreferredSize().width - parentInsets.left - parentInsets.right;
int diff = component.getWidth() - parentWidth;
if (diff > 0)
{
if (alignment == LEADING)
{
borderInsets.right += diff;
}
else if (alignment == TRAILING)
{
borderInsets.left += diff;
}
else
{
int half = diff / 2;
borderInsets.left += half;
borderInsets.right += (diff - half);
}
}
}
}
} |
Partager