Example usage for java.awt MediaTracker MediaTracker

List of usage examples for java.awt MediaTracker MediaTracker

Introduction

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

Prototype

public MediaTracker(Component comp) 

Source Link

Document

Creates a media tracker to track images for a given component.

Usage

From source file:Java2DExample.java

public ImagePanel(URL imageURL) {
    image = Toolkit.getDefaultToolkit().createImage(imageURL);
    MediaTracker mediaTracker = new MediaTracker(this);
    mediaTracker.addImage(image, 0);/*ww  w  . j a  v  a  2 s.  com*/

    try {
        mediaTracker.waitForAll();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    originalImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
    displayImage = originalImage;
    Graphics2D graphics = displayImage.createGraphics();
    graphics.drawImage(image, null, null);

}

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 ww . j ava2s . c  om
 */
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:ColorApp.java

public void loadImage() {
    displayImage = Toolkit.getDefaultToolkit().getImage("largeJava2sLogo.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(displayImage, 1);// www  .ja v a2  s.  c  o m
    try {
        mt.waitForAll();
    } catch (Exception e) {
        System.out.println("Exception while loading.");
    }

    if (displayImage.getWidth(this) == -1) {
        System.out.println("No jpg file");
        System.exit(0);
    }
}

From source file:com.tomtom.speedtools.json.ImageSerializer.java

@Nonnull
private static BufferedImage convertToBufferedImage(@Nonnull final Image image) throws IOException {
    assert image != null;

    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from   w  w  w .  jav a 2 s  .  c om*/

    /**
     * Load the image in the background and wait
     * until is is downloaded.
     */
    final MediaTracker tracker = new MediaTracker(new Component() {
        // Empty.
    });
    tracker.addImage(image, 0);
    try {
        tracker.waitForAll();
    } catch (final InterruptedException e) {
        throw new IOException(e.getMessage(), e);
    }

    /**
     * Create a buffered image with the right dimensions.
     */
    final BufferedImage bufImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    /**
     * Draw the image in the buffer and return it as base64 data.
     */
    final Graphics g = bufImage.createGraphics();
    g.drawImage(image, 0, 0, null);
    return bufImage;
}

From source file:AffineTransformApp.java

public void loadImage() {
    displayImage = Toolkit.getDefaultToolkit().getImage("largeJava2sLogo.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(displayImage, 1);//from   www  . j a v a2 s  .c  o m
    try {
        mt.waitForAll();
    } catch (Exception e) {
        System.out.println("Exception while loading.");
    }

    if (displayImage.getWidth(this) == -1) {
        System.out.println(" Missing .jpg file");
        System.exit(0);
    }
}

From source file:CombineApp.java

public void loadImage() {
    displayImage = Toolkit.getDefaultToolkit().getImage("largeJava2sLogo.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(displayImage, 1);//from w  w  w .  java  2 s . c  o m
    try {
        mt.waitForAll();
    } catch (Exception e) {
        System.out.println("Exception while loading.");
    }
    if (displayImage.getWidth(this) == -1) {
        System.out.println("No jpg) file");
        System.exit(0);
    }
}

From source file:ColorApp.java

public void loadImage() {
    displayImage = Toolkit.getDefaultToolkit().getImage("a.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(displayImage, 1);/* w w w.j a  va  2s. c o  m*/
    try {
        mt.waitForAll();
    } catch (Exception e) {
        System.out.println("Exception while loading.");
    }

    if (displayImage.getWidth(this) == -1) {
        System.out.println("No jpg file");
        System.exit(0);
    }
}

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

/**
 * @param bufInputStream//from  w  ww.  j  a v  a  2s  .  c  o 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");
    }
}