📄 mediasize.java
字号:
/* * @(#)MediaSize.java 1.16 06/04/07 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.print.attribute.standard;import java.util.HashMap;import java.util.Vector;import javax.print.attribute.Size2DSyntax;import javax.print.attribute.Attribute;/** * Class MediaSize is a two-dimensional size valued printing attribute class * that indicates the dimensions of the medium in a portrait orientation, with * the X dimension running along the bottom edge and the Y dimension running * along the left edge. Thus, the Y dimension must be greater than or equal to * the X dimension. Class MediaSize declares many standard media size * values, organized into nested classes for ISO, JIS, North American, * engineering, and other media. * <P> * MediaSize is not yet used to specify media. Its current role is * as a mapping for named media (see {@link MediaSizeName MediaSizeName}). * Clients can use the mapping method * <code>MediaSize.getMediaSizeForName(MediaSizeName)</code> * to find the physical dimensions of the MediaSizeName instances * enumerated in this API. This is useful for clients which need this * information to format & paginate printing. * <P> * * @author Phil Race, Alan Kaminsky */public class MediaSize extends Size2DSyntax implements Attribute { private static final long serialVersionUID = -1967958664615414771L; private MediaSizeName mediaName; private static HashMap mediaMap = new HashMap(100, 10); private static Vector sizeVector = new Vector(100, 10); /** * Construct a new media size attribute from the given floating-point * values. * * @param x X dimension. * @param y Y dimension. * @param units * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or * <CODE>Size2DSyntax.MM</CODE>. * * @exception IllegalArgumentException * (Unchecked exception) Thrown if <CODE>x</CODE> < 0 or <CODE>y</CODE> * < 0 or <CODE>units</CODE> < 1 or <CODE>x</CODE> > <CODE>y</CODE>. */ public MediaSize(float x, float y,int units) { super (x, y, units); if (x > y) { throw new IllegalArgumentException("X dimension > Y dimension"); } sizeVector.add(this); } /** * Construct a new media size attribute from the given integer values. * * @param x X dimension. * @param y Y dimension. * @param units * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or * <CODE>Size2DSyntax.MM</CODE>. * * @exception IllegalArgumentException * (Unchecked exception) Thrown if <CODE>x</CODE> < 0 or <CODE>y</CODE> * < 0 or <CODE>units</CODE> < 1 or <CODE>x</CODE> > <CODE>y</CODE>. */ public MediaSize(int x, int y,int units) { super (x, y, units); if (x > y) { throw new IllegalArgumentException("X dimension > Y dimension"); } sizeVector.add(this); } /** * Construct a new media size attribute from the given floating-point * values. * * @param x X dimension. * @param y Y dimension. * @param units * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or * <CODE>Size2DSyntax.MM</CODE>. * @param media a media name to associate with this MediaSize * * @exception IllegalArgumentException * (Unchecked exception) Thrown if <CODE>x</CODE> < 0 or <CODE>y</CODE> * < 0 or <CODE>units</CODE> < 1 or <CODE>x</CODE> > <CODE>y</CODE>. */ public MediaSize(float x, float y,int units, MediaSizeName media) { super (x, y, units); if (x > y) { throw new IllegalArgumentException("X dimension > Y dimension"); } mediaName = media; mediaMap.put(mediaName, this); sizeVector.add(this); } /** * Construct a new media size attribute from the given integer values. * * @param x X dimension. * @param y Y dimension. * @param units * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or * <CODE>Size2DSyntax.MM</CODE>. * @param media a media name to associate with this MediaSize * * @exception IllegalArgumentException * (Unchecked exception) Thrown if <CODE>x</CODE> < 0 or <CODE>y</CODE> * < 0 or <CODE>units</CODE> < 1 or <CODE>x</CODE> > <CODE>y</CODE>. */ public MediaSize(int x, int y,int units, MediaSizeName media) { super (x, y, units); if (x > y) { throw new IllegalArgumentException("X dimension > Y dimension"); } mediaName = media; mediaMap.put(mediaName, this); sizeVector.add(this); } /** * Get the media name, if any, for this size. * * @return the name for this media size, or null if no name was * associated with this size (an anonymous size). */ public MediaSizeName getMediaSizeName() { return mediaName; } /** * Get the MediaSize for the specified named media. * * @param media - the name of the media for which the size is sought * @return size of the media, or null if this media is not associated * with any size. */ public static MediaSize getMediaSizeForName(MediaSizeName media) { return (MediaSize)mediaMap.get(media); } /** * The specified dimensions are used to locate a matching MediaSize * instance from amongst all the standard MediaSize instances. * If there is no exact match, the closest match is used. * <p> * The MediaSize is in turn used to locate the MediaSizeName object. * This method may return null if the closest matching MediaSize * has no corresponding Media instance. * <p> * This method is useful for clients which have only dimensions and * want to find a Media which corresponds to the dimensions. * @param x - X dimension * @param y - Y dimension. * @param units * Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or * <CODE>Size2DSyntax.MM</CODE> * @return MediaSizeName matching these dimensions, or null. * @exception IllegalArgumentException if x <= 0, y <= 0, or units < 1 * */ public static MediaSizeName findMedia(float x, float y, int units) { MediaSize match = MediaSize.ISO.A4; if (x <= 0.0f || y <= 0.0f || units < 1) { throw new IllegalArgumentException("args must be +ve values"); } double ls = x * x + y * y; double tmp_ls; float []dim; float diffx = x; float diffy = y; for (int i=0; i < sizeVector.size() ; i++) { MediaSize mediaSize = (MediaSize)sizeVector.elementAt(i); dim = mediaSize.getSize(units); if (x == dim[0] && y == dim[1]) { match = mediaSize; break; } else { diffx = x - dim[0]; diffy = y - dim[1]; tmp_ls = diffx * diffx + diffy * diffy; if (tmp_ls < ls) { ls = tmp_ls; match = mediaSize; } } } return match.getMediaSizeName(); } /** * Returns whether this media size attribute is equivalent to the passed * in object. * To be equivalent, all of the following conditions must be true: * <OL TYPE=1> * <LI> * <CODE>object</CODE> is not null. * <LI> * <CODE>object</CODE> is an instance of class MediaSize. * <LI> * This media size attribute's X dimension is equal to * <CODE>object</CODE>'s X dimension. * <LI> * This media size attribute's Y dimension is equal to * <CODE>object</CODE>'s Y dimension. * </OL> * * @param object Object to compare to. * * @return True if <CODE>object</CODE> is equivalent to this media size * attribute, false otherwise. */ public boolean equals(Object object) { return (super.equals(object) && object instanceof MediaSize); } /** * Get the printing attribute class which is to be used as the "category" * for this printing attribute value. * <P> * For class MediaSize and any vendor-defined subclasses, the category is * class MediaSize itself. * * @return Printing attribute class (category), an instance of class * {@link java.lang.Class java.lang.Class}. */ public final Class<? extends Attribute> getCategory() { return MediaSize.class; } /** * Get the name of the category of which this attribute value is an * instance. * <P> * For class MediaSize and any vendor-defined subclasses, the category * name is <CODE>"media-size"</CODE>. * * @return Attribute category name. */ public final String getName() { return "media-size"; } /** * Class MediaSize.ISO includes {@link MediaSize MediaSize} values for ISO * media. * <P> */ public final static class ISO { /** * Specifies the ISO A0 size, 841 mm by 1189 mm. */ public static final MediaSize A0 = new MediaSize(841, 1189, Size2DSyntax.MM, MediaSizeName.ISO_A0); /** * Specifies the ISO A1 size, 594 mm by 841 mm. */ public static final MediaSize A1 = new MediaSize(594, 841, Size2DSyntax.MM, MediaSizeName.ISO_A1); /** * Specifies the ISO A2 size, 420 mm by 594 mm. */ public static final MediaSize A2 = new MediaSize(420, 594, Size2DSyntax.MM, MediaSizeName.ISO_A2); /** * Specifies the ISO A3 size, 297 mm by 420 mm. */ public static final MediaSize A3 = new MediaSize(297, 420, Size2DSyntax.MM, MediaSizeName.ISO_A3); /** * Specifies the ISO A4 size, 210 mm by 297 mm. */ public static final MediaSize A4 = new MediaSize(210, 297, Size2DSyntax.MM, MediaSizeName.ISO_A4); /** * Specifies the ISO A5 size, 148 mm by 210 mm. */ public static final MediaSize A5 = new MediaSize(148, 210, Size2DSyntax.MM, MediaSizeName.ISO_A5); /** * Specifies the ISO A6 size, 105 mm by 148 mm. */ public static final MediaSize A6 = new MediaSize(105, 148, Size2DSyntax.MM, MediaSizeName.ISO_A6); /** * Specifies the ISO A7 size, 74 mm by 105 mm. */ public static final MediaSize A7 = new MediaSize(74, 105, Size2DSyntax.MM, MediaSizeName.ISO_A7); /** * Specifies the ISO A8 size, 52 mm by 74 mm. */ public static final MediaSize A8 = new MediaSize(52, 74, Size2DSyntax.MM, MediaSizeName.ISO_A8); /** * Specifies the ISO A9 size, 37 mm by 52 mm. */ public static final MediaSize A9 = new MediaSize(37, 52, Size2DSyntax.MM, MediaSizeName.ISO_A9); /** * Specifies the ISO A10 size, 26 mm by 37 mm. */ public static final MediaSize A10 = new MediaSize(26, 37, Size2DSyntax.MM, MediaSizeName.ISO_A10); /** * Specifies the ISO B0 size, 1000 mm by 1414 mm. */ public static final MediaSize B0 = new MediaSize(1000, 1414, Size2DSyntax.MM, MediaSizeName.ISO_B0); /** * Specifies the ISO B1 size, 707 mm by 1000 mm. */ public static final MediaSize B1 = new MediaSize(707, 1000, Size2DSyntax.MM, MediaSizeName.ISO_B1); /** * Specifies the ISO B2 size, 500 mm by 707 mm. */ public static final MediaSize B2 = new MediaSize(500, 707, Size2DSyntax.MM, MediaSizeName.ISO_B2); /** * Specifies the ISO B3 size, 353 mm by 500 mm. */ public static final MediaSize B3 = new MediaSize(353, 500, Size2DSyntax.MM, MediaSizeName.ISO_B3); /** * Specifies the ISO B4 size, 250 mm by 353 mm. */ public static final MediaSize B4 = new MediaSize(250, 353, Size2DSyntax.MM, MediaSizeName.ISO_B4); /** * Specifies the ISO B5 size, 176 mm by 250 mm. */ public static final MediaSize B5 = new MediaSize(176, 250, Size2DSyntax.MM, MediaSizeName.ISO_B5); /** * Specifies the ISO B6 size, 125 mm by 176 mm. */ public static final MediaSize B6 = new MediaSize(125, 176, Size2DSyntax.MM, MediaSizeName.ISO_B6); /** * Specifies the ISO B7 size, 88 mm by 125 mm. */ public static final MediaSize B7 = new MediaSize(88, 125, Size2DSyntax.MM, MediaSizeName.ISO_B7); /** * Specifies the ISO B8 size, 62 mm by 88 mm. */ public static final MediaSize B8 = new MediaSize(62, 88, Size2DSyntax.MM, MediaSizeName.ISO_B8); /** * Specifies the ISO B9 size, 44 mm by 62 mm. */ public static final MediaSize B9 = new MediaSize(44, 62, Size2DSyntax.MM, MediaSizeName.ISO_B9); /** * Specifies the ISO B10 size, 31 mm by 44 mm. */ public static final MediaSize B10 = new MediaSize(31, 44, Size2DSyntax.MM, MediaSizeName.ISO_B10); /** * Specifies the ISO C3 size, 324 mm by 458 mm. */ public static final MediaSize C3 = new MediaSize(324, 458, Size2DSyntax.MM, MediaSizeName.ISO_C3); /** * Specifies the ISO C4 size, 229 mm by 324 mm. */ public static final MediaSize C4 = new MediaSize(229, 324, Size2DSyntax.MM, MediaSizeName.ISO_C4); /** * Specifies the ISO C5 size, 162 mm by 229 mm. */ public static final MediaSize C5 = new MediaSize(162, 229, Size2DSyntax.MM, MediaSizeName.ISO_C5); /** * Specifies the ISO C6 size, 114 mm by 162 mm. */ public static final MediaSize C6 = new MediaSize(114, 162, Size2DSyntax.MM, MediaSizeName.ISO_C6); /** * Specifies the ISO Designated Long size, 110 mm by 220 mm. */ public static final MediaSize DESIGNATED_LONG = new MediaSize(110, 220, Size2DSyntax.MM, MediaSizeName.ISO_DESIGNATED_LONG); /** * Hide all constructors. */ private ISO() { } } /** * Class MediaSize.JIS includes {@link MediaSize MediaSize} values for JIS * (Japanese) media. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -