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:org.muse.mneme.impl.AttachmentServiceImpl.java

/**
 * Create a thumbnail image from the full image in the byte[], of the desired width and height and quality, preserving aspect ratio.
 * /* ww  w . j  a  v  a 2  s. c om*/
 * @param full
 *        The full image bytes.
 * @param width
 *        The desired max width (pixels).
 * @param height
 *        The desired max height (pixels).
 * @param quality
 *        The JPEG quality (0 - 1).
 * @return The thumbnail JPEG as a byte[].
 * @throws IOException
 * @throws InterruptedException
 */
protected byte[] makeThumb(byte[] full, int width, int height, float quality)
        throws IOException, InterruptedException {
    // read the image from the byte array, waiting till it's processed
    Image fullImage = Toolkit.getDefaultToolkit().createImage(full);
    MediaTracker tracker = new MediaTracker(new Container());
    tracker.addImage(fullImage, 0);
    tracker.waitForID(0);

    // get the full image dimensions
    int fullWidth = fullImage.getWidth(null);
    int fullHeight = fullImage.getHeight(null);

    // preserve the aspect of the full image, not exceeding the thumb dimensions
    if (fullWidth > fullHeight) {
        // full width will take the full desired width, set the appropriate height
        height = (int) ((((float) width) / ((float) fullWidth)) * ((float) fullHeight));
    } else {
        // full height will take the full desired height, set the appropriate width
        width = (int) ((((float) height) / ((float) fullHeight)) * ((float) fullWidth));
    }

    // draw the scaled thumb
    BufferedImage thumbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2D = thumbImage.createGraphics();
    g2D.drawImage(fullImage, 0, 0, width, height, null);

    // encode as jpeg to a byte array
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    BufferedOutputStream out = new BufferedOutputStream(byteStream);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
    param.setQuality(quality, false);
    encoder.setJPEGEncodeParam(param);
    encoder.encode(thumbImage);
    out.close();
    byte[] thumb = byteStream.toByteArray();

    return thumb;
}

From source file:org.ohdsi.whiteRabbit.WhiteRabbitMain.java

private Image loadIcon(String name, JFrame f) {
    Image icon = Toolkit.getDefaultToolkit().getImage(WhiteRabbitMain.class.getResource(name));
    MediaTracker mediaTracker = new MediaTracker(f);
    mediaTracker.addImage(icon, 0);/*from   www  .j  a  va2  s. c o  m*/
    try {
        mediaTracker.waitForID(0);
        return icon;
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return null;
}

From source file:org.openstreetmap.josm.gui.layer.geoimage.ThumbsLoader.java

@Override
public void run() {
    Logging.debug("Load Thumbnails");
    tracker = new MediaTracker(MainApplication.getMap().mapView);
    for (ImageEntry entry : data) {
        if (stop)
            return;

        // Do not load thumbnails that were loaded before.
        if (!entry.hasThumbnail()) {
            entry.setThumbnail(loadThumb(entry));

            if (layer != null && MainApplication.isDisplayingMapView()) {
                layer.updateBufferAndRepaint();
            }/* w  w w.j a  va2 s  . co  m*/
        }
    }
    if (layer != null) {
        layer.thumbsLoaded();
        layer.updateBufferAndRepaint();
    }
}

From source file:org.pegadi.client.LoginDialog.java

private void jbInit() {
    Locale.setDefault(new Locale("no", "NO"));
    this.setTitle(str.getString("title"));
    this.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowOpened(WindowEvent e) {
            this_windowOpened(e);
        }//w w  w .  j  a  v a 2s .c o  m
    });
    loginLabel.setText(str.getString("login"));

    userNameLabel.setText(str.getString("username"));
    okButton.setText("OK");
    okButton.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(ActionEvent e) {
            okButton_actionPerformed(e);
        }
    });
    quitButton.setText(str.getString("quit"));
    quitButton.addActionListener(new java.awt.event.ActionListener() {

        public void actionPerformed(ActionEvent e) {
            quitButton_actionPerformed(e);
        }
    });
    userNameField.setColumns(10);
    userNameField.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            userNameField_keyReleased(e);
        }
    });

    passwordLabel.setText(str.getString("password"));
    passwordField.setColumns(10);
    passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(KeyEvent e) {
            passwordField_keyReleased(e);
        }
    });

    serverLabel.setText(str.getString("server"));
    Set keyset = servers.keySet();
    for (Object aKeyset : keyset) {
        String serverKey = (String) aKeyset;
        serverChooser.addItem(serverKey);
    }
    serverChooser.setEnabled(false);

    // gui starts

    JPanel mainPanel = new JPanel();
    this.getContentPane().add(mainPanel);
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    JPanel labelPanel = new JPanel();
    loginLabel.setFont(new Font(null, Font.PLAIN, 20));

    // add icon
    URL iu = getClass().getResource("/images/pegadi_icon.png");
    Image icon = Toolkit.getDefaultToolkit().getImage(iu);
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(icon, 0);
    try {
        mt.waitForID(0);
    } catch (InterruptedException ie) {
        icon = null;
    }

    if (icon != null) {
        loginLabel.setIcon(new ImageIcon(icon));
        loginLabel.setVerticalTextPosition(JLabel.BOTTOM);
        loginLabel.setHorizontalTextPosition(JLabel.CENTER);
    }

    labelPanel.add(loginLabel);
    labelPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    mainPanel.add(labelPanel);

    JPanel fieldPanel = new JPanel(new SpringLayout());
    fieldPanel.add(userNameLabel);
    fieldPanel.add(userNameField);
    fieldPanel.add(passwordLabel);
    fieldPanel.add(passwordField);
    fieldPanel.add(serverLabel);
    fieldPanel.add(serverChooser);
    SpringUtilities.makeCompactGrid(fieldPanel, 3, 2, //rows, cols
            5, 5, //initialX, initialY
            10, 5);//xPad, yPad
    mainPanel.add(fieldPanel);

    JPanel buttonPanel = new JPanel();
    JPanel buttonWrapperPanel = new JPanel();
    buttonPanel.setLayout(new BorderLayout());
    buttonWrapperPanel.add(okButton);
    buttonWrapperPanel.add(quitButton);
    buttonPanel.add(buttonWrapperPanel, BorderLayout.CENTER);
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    mainPanel.add(buttonPanel);
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

private synchronized void waitForImage(final java.awt.Image image) {
    if (mediaTracker == null) {
        mediaTracker = new MediaTracker(new FakeComponent());
    }//w ww  . j  a  v a2s . c  om
    mediaTracker.addImage(image, 0);
    try {
        mediaTracker.waitForID(0);
    } catch (InterruptedException e) {
        // empty on purpose
    }
    mediaTracker.removeImage(image);
}

From source file:org.pmedv.core.app.SplashScreen.java

/**
 * Show the splash screen.//from w w w .  ja  va  2 s  .  co  m
 */
public void splash() {

    window = new JWindow();
    // TODO : What was this for?
    // AWTUtilities.setWindowOpaque(window, false);

    if (image == null) {
        image = loadImage(imageResourcePath);
        if (image == null) {
            return;
        }
    }

    MediaTracker mediaTracker = new MediaTracker(window);
    mediaTracker.addImage(image, 0);

    try {
        mediaTracker.waitForID(0);
    } catch (InterruptedException e) {
        log.error("Interrupted while waiting for splash image to load.");
    }

    int width = image.getWidth(null);
    int height = image.getHeight(null);

    BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = (Graphics2D) bimg.createGraphics();

    g2d.drawImage(image, 0, 0, null);
    g2d.setColor(Color.BLACK);
    g2d.setFont(new Font("Arial", Font.BOLD, 10));
    InputStream is = getClass().getClassLoader().getResourceAsStream("application.properties");
    Properties properties = new Properties();
    try {
        properties.load(is);
    } catch (IOException e1) {
        properties.setProperty("version", "not set");
    }

    String version = properties.getProperty("version");

    File f = new File("build.number");

    properties = new Properties();

    try {
        properties.load(new FileReader(f));
    } catch (IOException e1) {
        properties.setProperty("build.number", "00");
    }

    String buildNumber = properties.getProperty("build.number");

    g2d.drawString("Version " + version + "." + buildNumber, 400, 305);

    JLabel panelImage = new JLabel(new ImageIcon(bimg));

    window.getContentPane().add(panelImage);
    window.getContentPane().add(progressBar, BorderLayout.SOUTH);
    window.pack();

    WindowUtils.center(window);

    window.setVisible(true);
}

From source file:org.pmedv.core.gui.ApplicationWindowAdvisorImpl.java

@Override
public void preWindowCreate() {

    log.info("initializing.");

    String laf = (String) Preferences.values.get("org.pmedv.blackboard.BoardDesignerPerspective.lookAndFeel");

    try {//ww w  .  ja  v a 2s  .c  o  m
        if (laf.equals("Nimbus")) {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } else if (laf.equals("SkyBlue")) {
            Plastic3DLookAndFeel.setPlasticTheme(new SkyBluer());
            UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
            com.jgoodies.looks.Options.setPopupDropShadowEnabled(true);
        } else {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
    } catch (Exception e2) {
        log.info("failed to set look and feel.");
    }

    final Color blackboardLightBlue = new Color(225, 234, 242);
    final Color blackBoardDarkBlue = new Color(182, 191, 205);
    final Color blackboardLightGrey = new Color(220, 220, 222);

    UIManager.put("TaskPane.titleBackgroundGradientStart", Color.WHITE);
    UIManager.put("TaskPane.titleBackgroundGradientEnd", blackboardLightBlue);
    UIManager.put("TaksPane.specialTitleBackground", blackboardLightBlue);
    UIManager.put("TaskPane.titleBackground", blackboardLightBlue);
    UIManager.put("TaskPane.borderColor", blackboardLightBlue);
    UIManager.put("TaskPane.background", blackboardLightGrey);
    UIManager.put("TaskPaneContainer.backgroundPainter", new MattePainter(blackBoardDarkBlue));

    log.info("setting look and feel to: " + UIManager.getLookAndFeel());

    // construct app icon

    Image iconImage = resources.getIcon("icon.application").getImage();

    MediaTracker mt = new MediaTracker(win);
    mt.addImage(iconImage, 0);

    try {
        mt.waitForAll();
    } catch (InterruptedException e) {
        // Silently ignore
    }

    InputStream is = getClass().getClassLoader().getResourceAsStream("application.properties");
    Properties properties = new Properties();
    try {
        properties.load(is);
    } catch (IOException e1) {
        properties.setProperty("version", "not set");
    }

    win.setTitle(windowConfig.getConfig().getTitle() + " Version " + properties.get("version"));
    win.setIconImage(iconImage);
    win.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    win.addWindowListener(win);

    ToolTipManager.sharedInstance().setInitialDelay(100);
    ToolTipManager.sharedInstance().setDismissDelay(1000);

}

From source file:processing.app.Theme.java

/**
 * Return an Image object from inside the Processing lib folder.
 *//*from  www. jav a  2s .c  o  m*/
static public Image getLibImage(String filename, Component who, int width, int height) {
    Image image = null;

    // Use vector image when available
    Resource vectorFile = getThemeResource(filename + ".svg");
    if (vectorFile.exists()) {
        try {
            image = imageFromSVG(vectorFile.getUrl(), width, height);
        } catch (Exception e) {
            System.err.println("Failed to load " + vectorFile + ": " + e.getMessage());
        }
    }

    Resource bitmapFile = getThemeResource(filename + ".png");

    // Otherwise fall-back to PNG bitmaps, allowing user-defined bitmaps to
    // override built-in svgs
    if (image == null || bitmapFile.getPriority() > vectorFile.getPriority()) {
        Resource bitmap2xFile = getThemeResource(filename + "@2x.png");

        Resource imageFile;
        if (((getScale() > 125 && bitmap2xFile.exists()) || !bitmapFile.exists())
                && (bitmapFile.isUserDefined() && bitmap2xFile.isUserDefined())) {
            imageFile = bitmap2xFile;
        } else {
            imageFile = bitmapFile;
        }
        Toolkit tk = Toolkit.getDefaultToolkit();
        image = tk.getImage(imageFile.getUrl());
    }

    MediaTracker tracker = new MediaTracker(who);
    try {
        tracker.addImage(image, 0);
        tracker.waitForAll();
    } catch (InterruptedException e) {
    }

    if (image.getWidth(null) != width || image.getHeight(null) != height) {
        image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        try {
            tracker.addImage(image, 1);
            tracker.waitForAll();
        } catch (InterruptedException e) {
        }
    }

    return image;
}

From source file:sim.model.entity.Category.java

/**
 * Prepare the entity image/*w  ww.  ja  va 2 s .  c om*/
 */
protected void prepareEntityImage(JComponent c, String path) {
    // Prepare the entity image
    super.prepareEntityImage(c, path);
    // Adjust the image
    ImageFilter filter = new WhiteFilter();
    FilteredImageSource filteredImage = new FilteredImageSource(display.getImage().getSource(), filter);
    Image image = Toolkit.getDefaultToolkit().createImage(filteredImage);
    MediaTracker tracker = new MediaTracker(c);
    try {
        tracker.addImage(image, 0);
        tracker.waitForAll();
    } catch (InterruptedException e) {
    }
    display.setImage(image);
    display.setImagePath(path);
}

From source file:unikn.dbis.univis.explorer.VSplashScreen.java

/**
 * Erzeugt eine neue <code>VSplashScreen</code> und sobald das Splashable gesetzt wurde
 * beendet sich die Screen von selbst./*from  ww  w.  j a  v  a  2s  . com*/
 *
 * @param inputStream Der Dateiname incl. des relativen Pfades des anzuzeigenden Images.
 */
public VSplashScreen(InputStream inputStream) {

    // Splash screen have to be always on top until the explorer
    // isn't loaded.
    setAlwaysOnTop(true);

    try {
        image = ImageIO.read(inputStream);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    MediaTracker mt = new MediaTracker(this);

    mt.addImage(image, 0);

    try {
        mt.waitForAll();
    } catch (InterruptedException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(e.getMessage(), e);
        }
    }

    thread = new Thread(this);
    thread.setPriority(Thread.MAX_PRIORITY);
    thread.start();

    center();

    setVisible(true);
}