All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.darwinsys.swingui.layout.RelativeLayout Maven / Gradle / Ivy

package com.darwinsys.swingui.layout;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * 

* RelativeLayout, a Relative Layout Manager for Java SE. * Mainly for porting tired old code that uses x,y locations. * You really can't just assign x,y locations to components * in Java SE - it breaks badly when the user resizes (and you can * not mandate that the user can't resize you -- see any book * on UI design for that little discussion -- and can also look * bad due to resolution independance. Symantec Cafe 1.x, for example, * used to spit out unfortunate (and unmaintainable) code like this: *

 *       setLayout(null);
 *       setSize(331,241);
 *       label1=new Label("Info Applet", Label.CENTER);
 *       add(label1);
 *       label1.setBounds(91,19,107,15);
 * 
*

* Bleaarrgghh!!! * To make it work properly at all resolutions and survive * user-initiated resize actions, change it to *

 *	setLayout(new RelativeLayout(331,241,false);
 *	label1=new Label("Info Applet", Label.CENTER);
 *	add("91,19", label1);
 * 
*

Note that it's actually less work to get it right. * Symantec, Microsoft, and others, please take note!

* @author Ian Darwin, http://www.darwinsys.com/ */ public class RelativeLayout implements LayoutManager { /** requested absolute width of canvas */ protected int reqWid; /** requested absolute height of canvas */ protected int reqHgt; /** actual size width when laid out */ protected int curWid; /** actual size height when laid out */ protected int curHgt; /** to track Components added by named add form. */ protected List curComps = new ArrayList(); /** * Constructs an RelativeLayout, given original hard-coded size of panel. */ public RelativeLayout(int wid, int ht) { this.reqWid = wid; this.reqHgt = ht; } /** * Called by AWT when the user uses the form add(name, Component). * Adds the specified component with the specified name to the layout. * * @param name String with location for component c * Note: the "name" must contain x, y location, ie., *
add("" + 320 + "," + 100, new Button("Quit")); *
or *
add("320,100", new Button("Quit"). *
This adds the Button at x=320, y=100 when the Panel is * at its original full size. * @param comp Component to be added. */ public void addLayoutComponent(String name, Component comp) { int x, y; StringTokenizer st = new StringTokenizer(name, ","); x = Integer.parseInt(st.nextToken()); y = Integer.parseInt(st.nextToken()); addLayoutComponent(comp, new Dimension(x, y)); } /** This version is here in preparation for implementing LayoutManager2 */ public void addLayoutComponent(Component comp, Object constraint) { Dimension d = (Dimension)constraint; int x = d.width, y = d.height; // System.out.println("Adding: Name " + name +"; obj " + c // + "; x " + x + "; y " + y); Tracker t = new Tracker(x, y, comp); curComps.add(t); } /** * Called by AWT to lay out the components * in the target Container at its current size. * * @param target Container whose components are to be laid out. */ public void layoutContainer(Container target) { Dimension targSize = target.getSize(); Insets ins = target.getInsets(); // System.out.println("layoutContainer: size " + targSize); curWid = targSize.width; curHgt = targSize.height; float widRatio = (float)curWid / (float)reqWid; float hgtRatio = (float)curHgt / (float)reqHgt; for (int i = 0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy