/** * 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 closest matching MediaSizeName among given array of Media */ public static MediaSizeName findMedia(Media[] media, float x, float y, int units) { if (x <= 0.0f || y <= 0.0f || units < 1) { throw new IllegalArgumentException("args must be +ve values"); } if (media == null || media.length == 0) { throw new IllegalArgumentException("args must have valid array of media"); } int size = 0; MediaSizeName[] msn = new MediaSizeName[media.length]; for (int i = 0; i < media.length; i++) { if (media[i] instanceof MediaSizeName) { msn[size++] = (MediaSizeName) media[i]; } } if (size == 0) { return null; } int match = 0; double ls = x * x + y * y; double tmp_ls; float[] dim; float diffx = x; float diffy = y; for (int i = 0; i < size; i++) { MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]); if (mediaSize == null) { continue; } dim = mediaSize.getSize(units); if (x == dim[0] && y == dim[1]) { match = i; break; } else { diffx = x - dim[0]; diffy = y - dim[1]; tmp_ls = diffx * diffx + diffy * diffy; if (tmp_ls < ls) { ls = tmp_ls; match = i; } } } return msn[match]; }