Tuesday, 15 March 2011

java - Avoid Overlapping components in Null Layout -



java - Avoid Overlapping components in Null Layout -

i have null layout in class extending jcomponent can add together components @ desired coordinate. don't want components overlapped. idea, how can accomplish thing? using other layout? want of functionality of flowlayout. there way?

you need implement own layout manager, e.g. one:

public class nonoverlappinglayout implements layoutmanager { @override public void addlayoutcomponent (string name, component comp) { // nil } @override public void removelayoutcomponent (component comp) { // nil } @override public dimension preferredlayoutsize (container parent) { int count = parent.getcomponentcount (); rectangle [] r = new rectangle [count]; (int = 0; < count; i++) { component c = parent.getcomponent (i); if (c.isvisible ()) r [i] = c.getbounds (); else r [i] = new rectangle (0, 0, 0, 0); } r = dolayout (r); rectangle result = new rectangle (0, 0, 0, 0); (int = 0; < count; i++) { result = result.union (r [i]); } homecoming result.getsize (); } @override public dimension minimumlayoutsize (container parent) { homecoming preferredlayoutsize (parent); } @override public void layoutcontainer (container parent) { int count = parent.getcomponentcount (); rectangle [] r = new rectangle [count]; (int = 0; < count; i++) { component c = parent.getcomponent (i); if (c.isvisible ()) r [i] = c.getbounds (); else r [i] = new rectangle (0, 0, 0, 0); } r = dolayout (r); int minx = 0; int miny = 0; (int = 0; < count; i++) { minx = math.min (minx, r [i].x); miny = math.min (miny, r [i].y); } (int = 0; < count; i++) { r [i].translate (-minx, -miny); component c = parent.getcomponent (i); if (c.isvisible ()) c.setbounds (r [i]); } } public rectangle [] dolayout (rectangle [] initialpositions) { int count = initialpositions.length; rectangle [] result = new rectangle [count]; (int = 0; < count; i++) result [i] = new rectangle (initialpositions [i]); boolean intersection; { intersection = false; (int = 0; < count; i++) { int xf = 0; int yf = 0; rectangle r1 = result [i]; (int j = 0; j < count; j++) { rectangle r2 = result [j]; if (i != j && r1.intersects (r2)) { if (r1.x * 2 + r1.width < r2.x * 2 + r2.width) xf -= 1; else xf += 1; if (r1.y * 2 + r1.height < r2.y * 2 + r2.height) yf -= 1; else yf += 1; intersection = true; } } if (xf > 0) result [i].x += 1; else if (xf < 0) result [i].x -= 1; if (yf > 0) result [i].y += 1; else if (yf < 0) result [i].y -= 1; } } while (intersection); homecoming result; } public static void main (string [] args) { jframe frame = new jframe (); frame.setdefaultcloseoperation (jframe.exit_on_close); frame.getcontentpane ().setlayout (new nonoverlappinglayout ()); random r = new random (); (int = 0; < 100; i++) { jbutton b = new jbutton (); b.setbounds (r.nextint (780), r.nextint (580), 20, 20); frame.getcontentpane ().add (b); } frame.getcontentpane (); frame.pack (); frame.setvisible (true); } }

java layout

No comments:

Post a Comment