Example usage for javax.swing ImageIcon ImageIcon

List of usage examples for javax.swing ImageIcon ImageIcon

Introduction

In this page you can find the example usage for javax.swing ImageIcon ImageIcon.

Prototype

public ImageIcon(byte[] imageData) 

Source Link

Document

Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG.

Usage

From source file:TabSample.java

static void add(JTabbedPane tabbedPane, String label) {
    int count = tabbedPane.getTabCount();
    JButton button = new JButton(label);
    button.setBackground(colors[count]);
    tabbedPane.addTab(label, new ImageIcon("yourFile.gif"), button, label);
}

From source file:Main.java

public static ImageIcon iconFromStream(final InputStream in) throws IOException {
    if (in == null) {
        throw new IllegalArgumentException("Stream must not be null");
    }/*from w  ww . j a  va  2  s. co m*/
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) > 0) {
        out.write(buffer, 0, read);
    }
    in.close();
    return new ImageIcon(out.toByteArray());
}

From source file:Main.java

public static void setTheme() {
    final Color color = new Color(85, 142, 119);
    final Color colorBackground = new Color(247, 247, 247);
    Image error_dialog_icon = new ImageIcon("/images/error_dialog.png").getImage();

    UIManager.getLookAndFeelDefaults().put("nimbusOrange", color);
    UIManager.getLookAndFeelDefaults().put("control", colorBackground);

    UIManager.getLookAndFeelDefaults().put("OptionPane.errorIcon", error_dialog_icon);
    UIManager.getLookAndFeelDefaults().put("OptionPane.background", colorBackground);

    UIManager.getLookAndFeelDefaults().put("Panel.background", new Color(245, 245, 245));
    UIManager.put("Table.background", new Color(250, 250, 250));
    //        UIManager.put("Table.alternateRowColor", new Color(159,203,64));
}

From source file:Main.java

public static ImageIcon getImageIcon(String name) {
    Image image = getImage(name);
    if (image == null) {
        return null;
    }//from   w  w  w. j  a va  2s. c om
    return new ImageIcon(image);
}

From source file:Main.java

public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }/*from w  ww .jav  a2 s .com*/

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels; for this method's
    // implementation, see Determining If an Image Has Transparent Pixels
    boolean hasAlpha = true;

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
        // The system does not have a screen
    }

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:Main.java

/**
 * Gets an {@code Icon} given the url {@code resource}.
 * /*  w w  w  .  j  a va  2s.c  om*/
 * @param resource
 *            the url path to a valid icon
 * @return An {@code Icon}
 * @throws RuntimeException if the {@link ClassLoader} could not locate the resource.
 */
@Deprecated
public static Icon getIcon(String resource) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    URL url = cl.getResource(resource);
    if (null == url) {
        throw new RuntimeException("ClassLoader could not locate " + resource);
    }
    return new ImageIcon(url);
}

From source file:ImageUtil.java

/**
 * Posted by alpha02 at http://www.dreamincode.net/code/snippet1076.htm
 *//*from  ww w .  j  ava  2  s.c o m*/
public static BufferedImage toBufferedImage(Image image) {
    if (image instanceof BufferedImage)
        return (BufferedImage) image;

    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();

    // Determine if the image has transparent pixels
    boolean hasAlpha = hasAlpha(image);

    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;

        if (hasAlpha == true)
            transparency = Transparency.BITMASK;

        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();

        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
    } //No screen

    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;

        if (hasAlpha == true) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }

    // Copy image to buffered image
    Graphics g = bimage.createGraphics();

    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
}

From source file:Main.java

public static Image genImageResource(Class<?> cls, String icon) {
    Image img = new ImageIcon(cls.getResource(icon)).getImage();
    return img;// w  w  w  .j  a v  a 2 s  .c om
}

From source file:Main.java

/**
 * Loads a icon from either the file system or from a jar.
 *
 * @param clas A class used to resolve the icon's relative path name
 * @param iconPath the path relative to the clas in which to find the icon.
 * @return an image icon for the icon, or null if not found
 *
 * <pre>/*  ww w.  j av  a 2  s  .c  om*/
 * for a directory structure like
 *  edu/
 *       mypackage/
 *          MyClass.class
 *          images/
 *             myicon.gif
 *
 * call loadImageIcon(MyClass.class, "images/myicon.gif");
 *
 * </pre>
 */
public static ImageIcon loadImageIcon(Class clas, String iconPath) {
    ImageIcon icon = null;
    URL url = clas.getResource(iconPath);
    if (url != null) {
        icon = new ImageIcon(url);
    }
    return icon;
}

From source file:ImageUtils.java

/**
 * Creates a transparent icon. The Icon can be used for aligning menu items.
 *
 * @param width  the width of the new icon
 * @param height the height of the new icon
 * @return the created transparent icon.
 *//*from   w  w w.j a v  a  2 s  . c o  m*/
public static Icon createTransparentIcon(final int width, final int height) {
    return new ImageIcon(createTransparentImage(width, height));
}