Java by API/java.awt/MediaTracker

Материал из Java эксперт
Перейти к: навигация, поиск

MediaTracker: addImage(Image image, int id)

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void load() throws MalformedURLException {
    URL url = new URL("image address");
    Image im = Toolkit.getDefaultToolkit().getImage(url);
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(im, 0);
    try {
      mt.waitForID(0);
    } catch (InterruptedException e) {
      System.err.println("Unexpected interrupt in waitForID!");
      return;
    }
    if (mt.isErrorID(0)) {
      System.err.println("Couldn"t load image file " + url);
      return;
    }
  }
  public static void main(String[] args) {
  }
}





MediaTracker.isErrorID(int id)

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void load() throws MalformedURLException {
    URL url = new URL("image address");
    Image im = Toolkit.getDefaultToolkit().getImage(url);
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(im, 0);
    try {
      mt.waitForID(0);
    } catch (InterruptedException e) {
      System.err.println("Unexpected interrupt in waitForID!");
      return;
    }
    if (mt.isErrorID(0)) {
      System.err.println("Couldn"t load image file " + url);
      return;
    }
  }
  public static void main(String[] args) {
  }
}





MediaTracker: waitForID(int id)

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void load() throws MalformedURLException {
    URL url = new URL("image address");
    Image im = Toolkit.getDefaultToolkit().getImage(url);
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(im, 0);
    try {
      mt.waitForID(0);
    } catch (InterruptedException e) {
      System.err.println("Unexpected interrupt in waitForID!");
      return;
    }
    if (mt.isErrorID(0)) {
      System.err.println("Couldn"t load image file " + url);
      return;
    }
  }
  public static void main(String[] args) {
  }
}





new MediaTracker(Component comp)

import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public void load() throws MalformedURLException {
    URL url = new URL("image address");
    Image im = Toolkit.getDefaultToolkit().getImage(url);
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(im, 0);
    try {
      mt.waitForID(0);
    } catch (InterruptedException e) {
      System.err.println("Unexpected interrupt in waitForID!");
      return;
    }
    if (mt.isErrorID(0)) {
      System.err.println("Couldn"t load image file " + url);
      return;
    }
  }
  public static void main(String[] args) {
  }
}