Unit measure : GUI Utilities « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Swing JFC » GUI UtilitiesScreenshots 
Unit measure
   
/*
 * Ruler.java
 
 * Created on Oct 1, 2007, 8:33:09 AM
 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.util.HashMap;

/**
 *
 @author gtoffoli
 */
public class Unit {

    public static final double PIXEL = 1.0;
    public static final double INCHES = 72.0;
    public static final double CENTIMETERS = 28.3464;
    public static final double MILLIMETERS = 2.83464;

    /** Holds value of property unitName. */
    private String unitName;
    private String keyName;

    /** Holds value of property conversionValue. */
    private double conversionValue;

    /** Creates a new instance of Unit */
    public Unit(String unitName, double conversionValue) {
        this(unitName, conversionValue, unitName);
    }
    
    public Unit(String unitName, double conversionValue, String keyName) {
        this.unitName = unitName;
        this.conversionValue = conversionValue;
        this.setKeyName(keyName);
    }

    /** Getter for property unitName.
     @return Value of property unitName.
     *
     */
    public String getUnitName() {
        return this.unitName;
    }

    /** Setter for property unitName.
     @param unitName New value of property unitName.
     *
     */
    public void setUnitName(String unitName) {
        this.unitName = unitName;
    }

    /** Getter for property conversionValue.
     @return Value of property conversionValue.
     *
     */
    public double getConversionValue() {
        return this.conversionValue;
    }

    /** Setter for property conversionValue.
     @param conversionValue New value of property conversionValue.
     *
     */
    public void setConversionValue(double conversionValue) {
        this.conversionValue = conversionValue;
    }

    private static Unit[] units = null;
    
    private static HashMap<String,Unit> unitsMap = null;
    
    public static Unit[] getStandardUnits()
    {
        if (units == null)
        {
            unitsMap = new HashMap<String,Unit>();
            units = new Unit[4];
            units[0new Unit("pixels",Unit.PIXEL, "pixels");  
            unitsMap.put("pixels", units[0]);
            units[1new Unit("inches",Unit.INCHES, "inches");
            unitsMap.put("inches", units[1]);
            units[2new Unit("cm",    Unit.CENTIMETERS, "cm");
            unitsMap.put("cm", units[2]);
            units[3new Unit("mm",    Unit.MILLIMETERS, "mm");
            unitsMap.put("mm", units[3]);
        }
        return units;
    }

    public static int getUnitIndex(String unitName)
    {
        Unit[] units = getStandardUnits();
        for (int i=0; i<units.length; ++i)
        {
            if (units[i].getUnitName().equalsIgnoreCase(unitName)) return i;
        }
        return -1;
    }

    public String toString()
    {
        return getUnitName();
    }

    /**
     * convert from pixel to the unit represented by this class
     *
     @param pixel
     @return
     */
    public double convert(int pixels)
    {
        return ((double)pixels)/conversionValue;
    }

    /**
     * convert from pixel to the unit represented by this class
     *
     @param pixel
     @return
     */
    public long toPixel(double val)
    {
        return (long)(val*conversionValue);
    }

    static public double convertPixelsToInches(long pixels)
  {
    return ((double)pixels)/INCHES;
  }

  static public long convertInchesToPixels(double inches)
  {
    return (long)(inches*INCHES);
  }

  static public double convertPixelsToCentimeters(long pixels)
  {
    return ((double)pixels)/CENTIMETERS;
  }

  static public long convertCentimetersToPixels(double centimeters)
  {
    return (long)(centimeters*CENTIMETERS);
  }

  static public double convertPixelsToMillimeters(long pixels)
  {
    return ((double)pixels)/MILLIMETERS;
  }

  static public long convertMillimetersToPixels(double millimeters)
  {
    return (long)(millimeters*CENTIMETERS);
  }

  static public long convertToPixels(double value, double convert)
  {
    return (long)(value*convert);
  }
        
        static public Unit getUnit(String unitName)
  {
            if (unitsMap == nullgetStandardUnits();
            return unitsMap.get(unitName);
        }

    public String getKeyName() {
        return keyName;
    }

    public void setKeyName(String keyName) {
        this.keyName = keyName;
    }
}

   
    
    
  
Related examples in the same category
1.Utilities for GUI work.
2.A utility class to tune the Metal look and feel.
3.Renders a component into an image
4.Positions the specified dialog at a position relative to its parent.
5.Computes the maximum bounds of the current screen device. If this method is called on JDK 1.4, Xinerama-aware results are returned.
6.Computes the center point of the current screen device.
7.Positions the specified frame at a relative position in the screen, where 50% is considered to be the center of the screen.
8.Positions the specified frame in the middle of the screen.
9.Positions the specified frame at a random location on the screen while ensuring that the
10.AWT Utilities
11.Center On Screen
12.Center Within Parent
13.Center window
14.Calculates preferred max height for the given components without checking isVisible
15.Calculates preferred max width for the given components without checking isVisible
16.Is Focusable
17.Loading the given image
18.Press a key pragmatically
19.Set All Opaque
20.Set Frame Location Relative To
21.Set Wait Cursor
22.Shadow Popup Border
23.Unit conversions
24.Get Point For Centering
25.Get Point For Staggering
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.