Example usage for java.awt MediaTracker waitForID

List of usage examples for java.awt MediaTracker waitForID

Introduction

In this page you can find the example usage for java.awt MediaTracker waitForID.

Prototype

public void waitForID(int id) throws InterruptedException 

Source Link

Document

Starts loading all images tracked by this media tracker with the specified identifier.

Usage

From source file:ImageFrame.java

/**
 * Set the image from a file.//from w  w  w.j av  a  2s  .  com
 */
public void setImage(File file) throws IOException {
    // load the image
    Image image = getToolkit().getImage(file.getAbsolutePath());

    // wait for the image to entirely load
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(image, 0);
    try {
        tracker.waitForID(0);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    if (tracker.statusID(0, true) != MediaTracker.COMPLETE) {
        throw new IOException("Could not load: " + file + " " + tracker.statusID(0, true));
    }

    setTitle(file.getName());
    setImage(image);
}

From source file:ImageOps.java

public void init() {
    setBackground(Color.white);//from w ww. ja  va2s .  c  o  m

    bi = new BufferedImage[4];
    String s[] = { "bld.jpg", "bld.jpg", "boat.gif", "boat.gif" };
    for (int i = 0; i < bi.length; i++) {
        Image img = getImage(getURL("images/" + s[i]));
        try {
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(img, 0);
            tracker.waitForID(0);
        } catch (Exception e) {
        }
        int iw = img.getWidth(this);
        int ih = img.getHeight(this);
        bi[i] = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
        Graphics2D big = bi[i].createGraphics();
        big.drawImage(img, 0, 0, this);
    }
}

From source file:SplashScreen.java

/**
 * Create a new splash screen object of the specified image.
 * The image file is located and referred to through the deployment, not
 * the local file system; A typical value might be "/com/company/splash.jpg".
 * //from w ww.  ja v  a 2  s.c o  m
 * @param imageFileName  Name of image file resource to act as splash screen.
 */
public SplashScreen(String imageFileName) {
    super(new Frame());

    try {
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        URL imageUrl = getClass().getResource(imageFileName);
        image_ = toolkit.getImage(imageUrl);

        MediaTracker mediaTracker = new MediaTracker(this);
        mediaTracker.addImage(image_, 0);
        mediaTracker.waitForID(0);

        width_ = image_.getWidth(this);
        height_ = image_.getHeight(this);

        Dimension screenSize = toolkit.getScreenSize();

        x_ = (screenSize.width - width_) / 2;
        y_ = (screenSize.height - height_) / 2;
    } catch (Exception exception) {
        exception.printStackTrace();
        image_ = null;
    }
}

From source file:ClipImage.java

public void init() {
    img = getImage(getURL("largeJava2sLogo.GIF"));
    try {//ww w .  j  ava  2 s . com
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(img, 0);
        tracker.waitForID(0);
    } catch (Exception e) {
    }
}

From source file:ClipImage.java

public void init() {
    setBackground(Color.white);/*from w  ww  . ja  va2s  .  co  m*/
    img = getImage(getURL("images/clouds.jpg"));
    try {
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(img, 0);
        tracker.waitForID(0);
    } catch (Exception e) {
    }
}

From source file:com.cubusmail.server.services.RetrieveImageServlet.java

/**
 * @param bufInputStream//from  w  w  w. jav a  2s .  co m
 * @param outputStream
 */
private void writeScaledImage(BufferedInputStream bufInputStream, OutputStream outputStream) {

    long millis = System.currentTimeMillis();
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = bufInputStream.read(buffer, 0, 8192)) != -1) {
            bos.write(buffer, 0, bytesRead);
        }
        bos.close();

        byte[] imageBytes = bos.toByteArray();

        Image image = Toolkit.getDefaultToolkit().createImage(imageBytes);
        MediaTracker mediaTracker = new MediaTracker(new Container());
        mediaTracker.addImage(image, 0);
        mediaTracker.waitForID(0);
        // determine thumbnail size from WIDTH and HEIGHT
        int thumbWidth = 300;
        int thumbHeight = 200;
        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {
            thumbWidth = (int) (thumbHeight * imageRatio);
        }
        // draw original image to thumbnail image object and
        // scale it to the new size on-the-fly
        BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
        int quality = 70;
        quality = Math.max(0, Math.min(quality, 100));
        param.setQuality((float) quality / 100.0f, false);
        encoder.setJPEGEncodeParam(param);
        encoder.encode(thumbImage);
    } catch (IOException ex) {
        log.error(ex.getMessage(), ex);
    } catch (InterruptedException ex) {
        log.error(ex.getMessage(), ex);
    } finally {
        log.debug("Time for thumbnail: " + (System.currentTimeMillis() - millis) + "ms");
    }
}

From source file:org.jfree.chart.demo.MultiShapesXYDemo.java

/**
 * A demonstration application showing a series with different shape attributes per item.
 *
 * @param title  the frame title.//w  w w .  j  a va2s  . c o m
 */
public MultiShapesXYDemo(final String title) {

    super(title);

    System.out.println("About to get images...");
    final URL url1 = getClass().getClassLoader().getResource("org/jfree/chart/demo/redball.png");
    final URL url2 = getClass().getClassLoader().getResource("org/jfree/chart/demo/arrow.png");
    if (url1 != null && url2 != null) {
        this.ballImage = new javax.swing.ImageIcon(url1).getImage();
        this.arrowImage = new javax.swing.ImageIcon(url2).getImage();
        final MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(this.ballImage, 0);
        tracker.addImage(this.arrowImage, 1);
        try {
            tracker.waitForID(0);
            tracker.waitForID(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Images loaded");
    } else {
        System.err.println("Can't find images");
    }
    System.out.println("Images done.");
    this.series = new XYSeries("Some Data");
    for (int i = 0; i < NUMBER_OF_POINTS; i++) {
        final double x = INCREMENT * i;
        final double y = Math.sin(x);
        this.series.add(x, y);
    }
    final XYSeriesCollection data = new XYSeriesCollection(this.series);
    final NumberAxis domainAxis = new NumberAxis("x");
    final NumberAxis rangeAxis = new NumberAxis("sin(x)");
    final DemoRenderer renderer = new DemoRenderer();
    final Plot plot = new XYPlot(data, domainAxis, rangeAxis, renderer);
    final JFreeChart chart = new JFreeChart(plot);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(600, 380));
    setContentPane(chartPanel);

}

From source file:de.cenote.jasperstarter.Report.java

private Map<String, Object> getCmdLineReportParams() {
    JRParameter[] jrParameterArray = jasperReport.getParameters();
    Map<String, JRParameter> jrParameters = new HashMap<String, JRParameter>();
    Map<String, Object> parameters = new HashMap<String, Object>();
    List<String> params;
    if (config.hasParams()) {
        params = config.getParams();/*from ww w.ja  v  a 2 s .  c o  m*/
        for (JRParameter rp : jrParameterArray) {
            jrParameters.put(rp.getName(), rp);
        }
        String paramName = null;
        //String paramType = null;
        String paramValue = null;

        for (String p : params) {
            try {
                paramName = p.split("=")[0];
                paramValue = p.split("=", 2)[1];
                if (config.isVerbose()) {
                    System.out.println("Using report parameter: " + paramName + " = " + paramValue);
                }
            } catch (Exception e) {
                throw new IllegalArgumentException("Wrong report param format! " + p, e);
            }
            if (!jrParameters.containsKey(paramName)) {
                throw new IllegalArgumentException("Parameter '" + paramName + "' does not exist in report!");
            }

            JRParameter reportParam = jrParameters.get(paramName);

            try {
                // special parameter handlers must also implemeted in
                // ParameterPanel.java
                if (Date.class.equals(reportParam.getValueClass())) {
                    // Date must be in ISO8601 format. Example 2012-12-31
                    DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd");
                    parameters.put(paramName, (Date) dateFormat.parse(paramValue));
                } else if (Image.class.equals(reportParam.getValueClass())) {
                    Image image = Toolkit.getDefaultToolkit()
                            .createImage(JRLoader.loadBytes(new File(paramValue)));
                    MediaTracker traker = new MediaTracker(new Panel());
                    traker.addImage(image, 0);
                    try {
                        traker.waitForID(0);
                    } catch (Exception e) {
                        throw new IllegalArgumentException("Image tracker error: " + e.getMessage(), e);
                    }
                    parameters.put(paramName, image);
                } else if (Locale.class.equals(reportParam.getValueClass())) {
                    parameters.put(paramName, LocaleUtils.toLocale(paramValue));
                } else {
                    // handle generic parameters with string constructor
                    try {
                        parameters.put(paramName, reportParam.getValueClass().getConstructor(String.class)
                                .newInstance(paramValue));
                    } catch (InstantiationException ex) {
                        throw new IllegalArgumentException("Parameter '" + paramName + "' of type '"
                                + reportParam.getValueClass().getName() + " caused " + ex.getClass().getName()
                                + " " + ex.getMessage(), ex);
                    } catch (IllegalAccessException ex) {
                        throw new IllegalArgumentException("Parameter '" + paramName + "' of type '"
                                + reportParam.getValueClass().getName() + " caused " + ex.getClass().getName()
                                + " " + ex.getMessage(), ex);
                    } catch (InvocationTargetException ex) {
                        Throwable cause = ex.getCause();
                        throw new IllegalArgumentException("Parameter '" + paramName + "' of type '"
                                + reportParam.getValueClass().getName() + " caused "
                                + cause.getClass().getName() + " " + cause.getMessage(), cause);
                    } catch (NoSuchMethodException ex) {
                        throw new IllegalArgumentException("Parameter '" + paramName + "' of type '"
                                + reportParam.getValueClass().getName() + " with value '" + paramValue
                                + "' is not supported by JasperStarter!", ex);
                    }
                }
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(
                        "NumberFormatException: " + e.getMessage() + "\" in \"" + p + "\"", e);
            } catch (java.text.ParseException e) {
                throw new IllegalArgumentException(e.getMessage() + "\" in \"" + p + "\"", e);
            } catch (JRException e) {
                throw new IllegalArgumentException("Unable to load image from: " + paramValue, e);
            }
        }
    }
    return parameters;
}

From source file:Jpeg.java

public JpegEncoder(Image image, int quality, OutputStream out) {
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(image, 0);/*from ww  w  .j a va  2 s.  c  o m*/
    try {
        tracker.waitForID(0);
    } catch (InterruptedException e) {
        // Got to do something?
    }
    /*
     * Quality of the image. 0 to 100 and from bad image quality, high
     * compression to good image quality low compression
     */
    Quality = quality;

    /*
     * Getting picture information It takes the Width, Height and RGB scans of
     * the image.
     */
    JpegObj = new JpegInfo(image);

    imageHeight = JpegObj.imageHeight;
    imageWidth = JpegObj.imageWidth;
    outStream = new BufferedOutputStream(out);
    dct = new DCT(Quality);
    Huf = new Huffman(imageWidth, imageHeight);
}