Java Code Examples for javax.print.attribute.standard.MediaSize#getSize()

The following examples show how to use javax.print.attribute.standard.MediaSize#getSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WrongPaperForBookPrintingTest.java    From openjdk-jdk8u with GNU General Public License v2.0 7 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    Book pageable = new Book();
    pageable.append(new WrongPaperForBookPrintingTest(), pf);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(pageable);
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 2
Source File: WrongPaperForBookPrintingTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    Book pageable = new Book();
    pageable.append(new WrongPaperForBookPrintingTest(), pf);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(pageable);
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 3
Source File: WrongPaperPrintingTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf));
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 4
Source File: WrongPaperPrintingTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf));
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 5
Source File: WrongPaperPrintingTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf));
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 6
Source File: WrongPaperPrintingTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf));
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 7
Source File: WrongPaperForBookPrintingTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    Book pageable = new Book();
    pageable.append(new WrongPaperForBookPrintingTest(), pf);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(pageable);
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 8
Source File: WrongPaperPrintingTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf));
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 9
Source File: WrongPaperForBookPrintingTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    Book pageable = new Book();
    pageable.append(new WrongPaperForBookPrintingTest(), pf);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(pageable);
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example 10
Source File: CustomMediaSizeName.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 11
Source File: CustomMediaSizeName.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 12
Source File: CustomMediaSizeName.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 13
Source File: CustomMediaSizeName.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 14
Source File: CustomMediaSizeName.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 15
Source File: CustomMediaSizeName.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 16
Source File: CustomMediaSizeName.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 17
Source File: CustomMediaSizeName.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 18
Source File: CustomMediaSizeName.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 19
Source File: CustomMediaSizeName.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}
 
Example 20
Source File: CustomMediaSizeName.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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];
}