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:Main.java

public Main() {
    setSize(300, 300);//from   w  w w. ja v a  2 s. co  m
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JCheckBox checkBox = new JCheckBox("Check me!");
    checkBox.setSelected(true);

    // Set default icon for checkbox
    checkBox.setIcon(new ImageIcon("icon.png"));
    // Set selected icon when checkbox state is selected
    checkBox.setSelectedIcon(new ImageIcon("selectedIcon.png"));
    // Set disabled icon for checkbox
    checkBox.setDisabledIcon(new ImageIcon("disabledIcon.png"));
    // Set disabled-selected icon for checkbox
    checkBox.setDisabledSelectedIcon(new ImageIcon("disabledSelectedIcon.png"));
    // Set checkbox icon when checkbox is pressed
    checkBox.setPressedIcon(new ImageIcon("pressedIcon.png"));
    // Set icon when a mouse is over the checkbox
    checkBox.setRolloverIcon(new ImageIcon("rolloverIcon.png"));
    // Set icon when a mouse is over a selected checkbox
    checkBox.setRolloverSelectedIcon(new ImageIcon("rolloverSelectedIcon.png"));

    getContentPane().add(checkBox);
}

From source file:AnimatedLabel.java

public AnimatedLabel(String gifName, int numGifs) {
    icons = new Icon[numGifs];
    for (int i = 0; i < numGifs; i++)
        icons[i] = new ImageIcon(gifName + i + ".gif");
    setIcon(icons[0]);/*  w ww. ja  va  2 s.  co m*/

    Thread tr = new Thread(this);
    tr.setPriority(Thread.MAX_PRIORITY);
    tr.start();
}

From source file:Main.java

public static void draw(BufferedImage imageBG, BufferedImage imageFG) {
    Ellipse2D.Double ellipse1 = new Ellipse2D.Double(20, 20, 30, 30);
    Ellipse2D.Double ellipse2 = new Ellipse2D.Double(25, 25, 30, 30);
    Area circle = new Area(ellipse1);
    circle.subtract(new Area(ellipse2));

    Graphics2D g = imageBG.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g.setClip(circle);/*from www. j a  va  2s.c o  m*/
    g.drawImage(imageFG, 0, 0, null);
    g.setClip(null);
    Stroke s = new BasicStroke(2);
    g.setStroke(s);
    g.setColor(Color.BLACK);
    g.draw(circle);
    g.dispose();

    JLabel l = new JLabel(new ImageIcon(imageBG));
    JOptionPane.showMessageDialog(null, l);
}

From source file:SplashScreen.java

public SplashScreen(String image) {
    JLabel lbl = new JLabel(new ImageIcon(image));
    getContentPane().add(lbl, BorderLayout.CENTER);
    pack();//ww w . java2 s  .  com
    setLocationRelativeTo(null);
    setVisible(true);
}

From source file:Main.java

public static ImageIcon createImageIcon(Class<?> bezugsklasse, String path) {
    final URL imgURL = bezugsklasse.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    }//  w w  w .  j  a  va 2  s  .c  o  m
    System.err.println("Couldn't find file: " + path);
    return null;
}

From source file:ImageUtil.java

/**
 * get image thumbnail/*from ww  w. ja  v a 2  s .c o m*/
 * @param imageFile
 */
public static ImageIcon getImageThumbnail(File imageFile, int width, int height) {
    ImageIcon thumbnail = null;
    if (imageFile != null && imageFile.isFile()) {
        ImageIcon tmpIcon = new ImageIcon(imageFile.getPath());
        if (tmpIcon != null) {
            if (tmpIcon.getIconWidth() > width) {
                int targetHeight = width / tmpIcon.getIconWidth() * tmpIcon.getIconHeight();
                if (targetHeight > height) {
                    targetHeight = height;
                } else {
                    targetHeight = -1;
                }
                thumbnail = new ImageIcon(
                        tmpIcon.getImage().getScaledInstance(width, targetHeight, Image.SCALE_AREA_AVERAGING));
            } else {
                thumbnail = tmpIcon;
            }
        }
    }
    return thumbnail;
}

From source file:JCheckBoxCustomIcon.java

public JCheckBoxCustomIcon() {
    setSize(300, 300);//from  w  ww .j  av  a2  s. co  m
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    JCheckBox checkBox = new JCheckBox("Check me!");
    checkBox.setSelected(true);

    // Set default icon for checkbox
    checkBox.setIcon(new ImageIcon("icon.png"));
    // Set selected icon when checkbox state is selected
    checkBox.setSelectedIcon(new ImageIcon("selectedIcon.png"));
    // Set disabled icon for checkbox
    checkBox.setDisabledIcon(new ImageIcon("disabledIcon.png"));
    // Set disabled-selected icon for checkbox
    checkBox.setDisabledSelectedIcon(new ImageIcon("disabledSelectedIcon.png"));
    // Set checkbox icon when checkbox is pressed
    checkBox.setPressedIcon(new ImageIcon("pressedIcon.png"));
    // Set icon when a mouse is over the checkbox
    checkBox.setRolloverIcon(new ImageIcon("rolloverIcon.png"));
    // Set icon when a mouse is over a selected checkbox
    checkBox.setRolloverSelectedIcon(new ImageIcon("rolloverSelectedIcon.png"));

    getContentPane().add(checkBox);
}

From source file:MainClass.java

public MainClass() {
    super("Tree Test Example");
    setSize(200, 150);//from   w  ww  .j ava  2s.  co m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    UIManager.put("Tree.leafIcon", new ImageIcon("leafIcon.gif"));
    UIManager.put("Tree.openIcon", new ImageIcon("openIcon.gif"));
    UIManager.put("Tree.closedIcon", new ImageIcon("closedIcon.gif"));
    UIManager.put("Tree.expandedIcon", new ImageIcon("expandedIcon.gif"));
    UIManager.put("Tree.collapsedIcon", new ImageIcon("collapsedIcon"));
}

From source file:CascadeDemo.java

public CascadeDemo() {
    super("CascadeDemo");
    EARTH = new ImageIcon("earth.jpg");
    m_count = m_tencount = 0;// www  .java  2s.c om

    m_desktop = new JDesktopPane();
    m_desktop.putClientProperty("JDesktopPane.dragMode", "outline");
    m_newFrame = new JButton("New Frame");
    m_newFrame.addActionListener(this);

    m_infos = UIManager.getInstalledLookAndFeels();
    String[] LAFNames = new String[m_infos.length];
    for (int i = 0; i < m_infos.length; i++) {
        LAFNames[i] = m_infos[i].getName();
    }
    m_UIBox = new JComboBox(LAFNames);
    m_UIBox.addActionListener(this);

    JPanel topPanel = new JPanel(true);
    topPanel.add(m_newFrame);
    topPanel.add(new JLabel("Look & Feel:", SwingConstants.RIGHT));
    topPanel.add(m_UIBox);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add("North", topPanel);
    getContentPane().add("Center", m_desktop);

    setSize(570, 400);
    Dimension dim = getToolkit().getScreenSize();
    setLocation(dim.width / 2 - getWidth() / 2, dim.height / 2 - getHeight() / 2);
    setVisible(true);
    WindowListener l = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(l);
}

From source file:ch.fork.AdHocRailway.ui.utils.ImageTools.java

public static ImageIcon createImageIcon(final String icon) {
    if (!cache.containsKey(icon)) {
        final ImageIcon imageIcon = new ImageIcon(ClassLoader.getSystemResource(icon));
        cache.put(icon, imageIcon);/*from   w w  w.  j  av a 2s.co  m*/
        LOGGER.info("cache-miss: put icon for " + icon + " in cache");
        return imageIcon;
    }
    return cache.get(icon);
}