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
| public class BorderDrawerComponent extends LayerUI<JComponent> {
/**
*
*/
private static final long serialVersionUID = 1L;
private final List<BorderDrawComponentGroup> groups;
public BorderDrawerComponent() {
this.groups=new ArrayList<>();
}
public void addGroup(BorderDrawComponentGroup group) {
if ( !groups.contains(group) ) {
groups.add(group);
}
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
@SuppressWarnings("unchecked")
JLayer<? extends JComponent> l = (JLayer<? extends JComponent>) c;
update(l);
l.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
update(l);
l.repaint();
}
});
}
/*@Override
public void doLayout(JLayer<? extends JComponent> l) {
super.doLayout(l);
update(l);
}*/
private void update(JLayer<? extends JComponent> l) {
Map<JComponent, Rectangle> bounds = new HashMap<>();
getInnerComponents(l.getView(), l.getView(), bounds);
for(BorderDrawComponentGroup group : groups) {
group.setBounds( bounds );
}
}
private void getInnerComponents(JComponent root,
JComponent component, Map<JComponent, Rectangle> bounds) {
bounds.put( component, getBoundsInRoot(root, component));
for(Component child : component.getComponents()) {
if ( child instanceof JComponent ) {
getInnerComponents(root, (JComponent) child, bounds);
}
}
}
private Rectangle getBoundsInRoot(JComponent root, JComponent component) {
Rectangle bounds = SwingUtilities.convertRectangle(component.getParent(), component.getBounds(), root);
return bounds;
}
@Override
public void paint(Graphics g, JComponent c) {
super.paint(g, c);
for(BorderDrawComponentGroup group : groups) {
group.draw(g, c);
}
}
public static class BorderDrawComponentGroup {
private final JComponent[] components;
private final Border border;
private final int margin;
private Rectangle bounds;
public BorderDrawComponentGroup(Border border, int margin, JComponent...components) {
Objects.requireNonNull(components);
Objects.requireNonNull(border);
this.components = Arrays.copyOf(components, components.length);
this.border = border;
this.margin = margin;
}
public void draw(Graphics g, JComponent c) {
if ( bounds!=null ) {
border.paintBorder(c, g, bounds.x, bounds.y, bounds.width, bounds.height);
}
}
private void setBounds(Map<JComponent, Rectangle> bounds) {
this.bounds = null;
for(JComponent component : components ) {
Rectangle componentBounds = bounds.get(component);
if ( componentBounds!=null ) {
this.bounds = this.bounds==null?componentBounds:this.bounds.union(componentBounds);
}
}
if ( this.bounds!=null ) {
this.bounds.x-=margin;
this.bounds.y-=margin;
this.bounds.width+=margin+margin;
this.bounds.height+=margin+margin;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
JPanel panel = new JPanel(new GridBagLayout());
JComponent[] components = new JComponent[4];
int componentIndex=0;
JComponent[] component2s = new JComponent[4];
int component2Index=0;
for(int i=0; i<25; i++ ) {
int x= i/5;
int y = i%5;
JLabel label = new JLabel("Exemple " + i);
panel.add(label, new GridBagConstraints(x,y,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE, new Insets(5, 5, 5, 5),0, 0));
if ( x>=2 && x<=3 && y>=2 && y<=3 ) {
components[componentIndex++]=label;
}
if ( x>=3 && x<=5 && y>=3 && y<=4 ) {
component2s[component2Index++]=label;
}
}
BorderDrawerComponent layerUI = new BorderDrawerComponent();
JLayer<JComponent> jlayer = new JLayer<JComponent>(panel, layerUI);
layerUI.addGroup(new BorderDrawComponentGroup(BorderFactory.createLineBorder(Color.RED), 5, components));
layerUI.addGroup(new BorderDrawComponentGroup(BorderFactory.createDashedBorder(Color.BLUE, 5, 5), 1, component2s));
frame.getContentPane().add(jlayer);
frame.setSize(600, 400);
frame.setVisible(true);
}
} |
Partager