please dont rip this site

Language Java Nosugar Custom World Code Worldlayout.java

// 
//  WorldLayout manager class
//  part of the set of documents known as Java no sugar.
//  Copyright (c) 1996 Sunil Gupta, sunil@magnetic.demon.co.uk
//  placed into the public domain.
//
//
//  Layout managers can't rely upon minimumSize() to return anything 
//  sensible
//

import java.awt.*;
import java.util.*;
import WorldConstraints;

public class WorldLayout implements LayoutManager
{
    private Hashtable constraint_table;
    private Dimension original_size = null;
    
    //**************************************************************
    // Constructors
    //**************************************************************
    public WorldLayout(int width, int height)
    {
        this();
        original_size = new Dimension(width,height);
    }
    
    public WorldLayout()
    {
        constraint_table = new Hashtable();
    }

    //**************************************************************
    // constraints
    //**************************************************************
    protected WorldConstraints lookupConstraints(Component  comp)
    {
        return ( this.getConstraints(comp) );
    }
    
    public WorldConstraints getConstraints(Component  comp)
    {
        return ( (WorldConstraints) constraint_table.get(comp) );
    }
    
    public void setConstraints(Component  comp, WorldConstraints  constraints)
    {
        constraint_table.put(comp, constraints);
    }
    
    //**************************************************************
    // unwanted methods defined by LayoutManager interface
    //**************************************************************
    public void addLayoutComponent(String   name,Component  comp)
    {
    }
    
    //**************************************************************
    // methods defined by LayoutManager interface
    //**************************************************************
    public void layoutContainer(Container   target)
    {
        Component component;
        int n_components, index, new_width, new_height;
        WorldConstraints constraint;
        Dimension container_size, component_size, min_component_size;
        double x,y, x_ratio, y_ratio;
        boolean first_time = true;
        
        //------------- resize each component to the size of the container--------
        container_size = target.size();

        // ------- if the original size is known, a resize hapened so adjust ratios
        if (original_size != null)
        {
            x_ratio = (double)container_size.width / (double)original_size.width;
            y_ratio = (double)container_size.height / (double)original_size.height;
        }
        else
        {
            x_ratio = 1.0;
            y_ratio = 1.0;
        }
            
        //------------- resize each component to the size of the container--------
        n_components = target.countComponents();
        for (index =0; index < n_components; index ++)
        {
            //- - - - - -get component and constraint - - - - - - - - -
            component = target.getComponent(index);
            constraint = (WorldConstraints)this.getConstraints(component);
            if (constraint == null)
                constraint = new WorldConstraints();
                    
            //- - - - get component sizes- - - - - - - - - - - - - - - - - - 
            component_size = component.preferredSize();
            min_component_size = component.minimumSize();

            //- - - -resize, observing minimum component size - - - - - - 
            if ( constraint.resizeX )
            {
                if (constraint.override_width)
                    new_width = (int) Math.round( (double)container_size.width * constraint.width /100.0 );
                else
                    new_width = (int) Math.round( (double)component_size.width * x_ratio);
                
                component_size.width = Math.max(new_width,min_component_size.width); 
            }
            
            if ( constraint.resizeY )
            {
                if (constraint.override_height)
                    new_height = (int) Math.round( (double)container_size.height * constraint.height/100.0);
                else
                    new_height = (int) Math.round( (double)component_size.height * y_ratio);
                    
                component_size.height = Math.max(new_height,min_component_size.height); 
            }
            component.resize(component_size.width, component_size.height);
            
            //- - - - - -move proportional to container size- - - - - - -
            x = (double) container_size.width * constraint.x / 100.0;
            y = (double) container_size.height * constraint.y / 100.0;
            component.move( (int)Math.round(x), (int)Math.round(y) );
        }
        
        //------------remember original size for next time------------
        if (original_size == null)
        {
            original_size = container_size;
            first_time = false;
        }
    }
    
    public Dimension minimumLayoutSize(Container  target)
    {
        return (original_size);
    }
    
    public Dimension preferredLayoutSize(Container  target)
    {
        return (original_size);
    }
    
    public void removeLayoutComponent(Component  comp)
    {
        constraint_table.remove(comp);
    }
    
    public String toString()
    {
        return ( getClass().getName() + ": from Java no sugar") ;
    }
}


file: /Techref/language/java/nosugar/custom/world/code/WorldLayout.java, 5KB, , updated: 1997/1/11 21:32, local time: 2024/12/24 09:31,
TOP NEW HELP FIND: 
3.139.79.187:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://sxlist.com/Techref/language/java/nosugar/custom/world/code/WorldLayout.java"> language java nosugar custom world code WorldLayout</A>

Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .