Example usage for java.awt MediaTracker addImage

List of usage examples for java.awt MediaTracker addImage

Introduction

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

Prototype

public void addImage(Image image, int id) 

Source Link

Document

Adds an image to the list of images being tracked by this media tracker.

Usage

From source file:Java2DExample.java

public ImagePanel(URL imageURL) {
    image = Toolkit.getDefaultToolkit().createImage(imageURL);
    MediaTracker mediaTracker = new MediaTracker(this);
    mediaTracker.addImage(image, 0);

    try {/*from   ww w.  j  av  a  2  s .co  m*/
        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:ColorApp.java

public void loadImage() {
    displayImage = Toolkit.getDefaultToolkit().getImage("largeJava2sLogo.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(displayImage, 1);
    try {/*from   w w w .  ja va  2 s . c  o  m*/
        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);
    try {/*from ww  w  .j  a  v a  2s  . co  m*/
        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:ClipImage.java

public void init() {
    img = getImage(getURL("largeJava2sLogo.GIF"));
    try {/*from  w  w  w  . ja  va 2  s .co m*/
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(img, 0);
        tracker.waitForID(0);
    } catch (Exception e) {
    }
}

From source file:SlidePuzzle.java

public void run() {
    if (!m_fAllLoaded) {
        m_Graphics = getGraphics();
        MediaTracker tracker = new MediaTracker(this);
        logo = getImage(getDocumentBase(), "vjlogo.gif");
        tracker.addImage(logo, 0);

        try {/*from www .j  ava  2  s  .co  m*/
            tracker.waitForAll();
            m_fAllLoaded = !tracker.isErrorAny();
        } catch (InterruptedException e) {
        }

        if (!m_fAllLoaded) {
            stop();
            m_Graphics.drawString("Error loading images!", 10, 40);
            return;
        }
    }

    //Clear the screen and draw first image
    repaint();

    //Move the squares
    while (true) {
        for (x = 1; x < 9; x++) {
            //Slow down the animation
            try {
                Thread.sleep(350);
            } catch (InterruptedException e) {
            }

            //Move square to next location

            m_Graphics.copyArea(squares[order[x]].x, squares[order[x]].y, 60, 60,
                    squares[order[x - 1]].x - squares[order[x]].x,
                    squares[order[x - 1]].y - squares[order[x]].y);

            //Clear most recently copied square

            m_Graphics.clearRect(squares[order[x]].x, squares[order[x]].y, 60, 60);
        }

        //Repaint original graphic after two cycles
        y++;
        if (y == 2) {
            repaint();
            y = 0;
        }
    }
}

From source file:ClipImage.java

public void init() {
    setBackground(Color.white);//w ww. j av  a2  s  .  c o  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 .  j  av 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./*from ww  w.  j  a va 2s .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:net.sf.firemox.tools.MToolKit.java

/**
 * Return the loaded picture from a local place.
 * /*from   ww w .java  2 s.  c o  m*/
 * @param localFile
 *          the local file name.
 * @return the local picture.
 * @throws InterruptedException
 */
public static Image getLocalPicture(String localFile) throws InterruptedException {
    final Image result = Toolkit.getDefaultToolkit().getImage(getFile(localFile, true).getAbsolutePath());
    if (result == null) {
        throw new InterruptedException("Picture " + localFile + " has not been found");
    }
    final MediaTracker tracker = new MediaTracker(MagicUIComponents.magicForm);
    tracker.addImage(result, 0);
    tracker.waitForAll();
    if (tracker.isErrorAny()) {
        tracker.removeImage(result, 0);
        tracker.waitForAll();
        result.flush();
        throw new InterruptedException("Malformed picture " + localFile);
    }
    return result;
}