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:net.sf.firemox.ui.component.SplashScreen.java

/**
 * Create a new instance of this class./*w ww .  j  a  v a 2 s.  co  m*/
 * 
 * @param filename
 *          the picture filename.
 * @param parent
 *          the splash screen's parent.
 * @param waitTime
 *          the maximum time before the screen is hidden.
 */
public SplashScreen(String filename, Frame parent, int waitTime) {
    super(parent);
    getContentPane().setLayout(null);
    toFront();
    final JLabel l = new JLabel(new ImageIcon(filename));
    final Dimension labelSize = l.getPreferredSize();
    l.setLocation(0, 0);
    l.setSize(labelSize);
    setSize(labelSize);

    final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation(screenSize.width / 2 - labelSize.width / 2, screenSize.height / 2 - labelSize.height / 2);

    final JLabel mp = new JLabel(IdConst.PROJECT_DISPLAY_NAME);
    mp.setLocation(30, 305);
    mp.setSize(new Dimension(300, 30));

    final JLabel version = new JLabel(IdConst.VERSION);
    version.setLocation(235, 418);
    version.setSize(new Dimension(300, 30));

    final JTextArea disclaimer = new JTextArea();
    disclaimer.setEditable(false);
    disclaimer.setLineWrap(true);
    disclaimer.setWrapStyleWord(true);
    disclaimer.setAutoscrolls(true);
    disclaimer.setFont(MToolKit.defaultFont);
    disclaimer.setTabSize(2);

    // Then try and read it locally
    Reader inGPL = null;
    try {
        inGPL = new BufferedReader(new InputStreamReader(MToolKit.getResourceAsStream(IdConst.FILE_LICENSE)));
        disclaimer.read(inGPL, "");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(inGPL);
    }

    final JScrollPane disclaimerSPanel = new JScrollPane();
    disclaimerSPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    disclaimerSPanel.setViewportView(disclaimer);
    disclaimerSPanel.setLocation(27, 340);
    disclaimerSPanel.setPreferredSize(new Dimension(283, 80));
    disclaimerSPanel.setSize(disclaimerSPanel.getPreferredSize());

    getContentPane().add(disclaimerSPanel);
    getContentPane().add(version);
    getContentPane().add(mp);
    getContentPane().add(l);

    final int pause = waitTime;
    final Runnable waitRunner = new Runnable() {
        public void run() {
            try {
                Thread.sleep(pause);
                while (!bKilled) {
                    Thread.sleep(200);
                }
            } catch (InterruptedException e) {
                // Ignore this error
            }
            setVisible(false);
            dispose();
            MagicUIComponents.magicForm.toFront();
        }
    };

    // setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            setVisible(false);
            dispose();
            if (MagicUIComponents.magicForm != null)
                MagicUIComponents.magicForm.toFront();
        }
    });
    addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                setVisible(false);
                dispose();
                MagicUIComponents.magicForm.toFront();
            }
        }
    });
    setVisible(true);
    start(waitRunner);
}

From source file:Charts2D.java

public Charts2D() {
    super("2D Charts");
    setSize(720, 280);// w w  w. j a  v  a  2s.c  o m
    getContentPane().setLayout(new GridLayout(1, 3, 10, 0));
    getContentPane().setBackground(Color.white);

    int[] xData = new int[8];
    int[] yData = new int[8];
    for (int i = 0; i < xData.length; i++) {
        xData[i] = i;
        yData[i] = (int) (Math.random() * 100);
        if (i > 0)
            yData[i] = (yData[i - 1] + yData[i]) / 2;
    }

    JChart2D chart = new JChart2D(JChart2D.LineChart, xData.length, xData, yData, "Line Chart");
    chart.setStroke(new BasicStroke(5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
    chart.setLineColor(new Color(0, 28, 28));
    getContentPane().add(chart);

    chart = new JChart2D(JChart2D.ColumnChart, xData.length, xData, yData, "Column Chart");
    GradientPaint gp = new GradientPaint(0, 100, Color.white, 0, 300, Color.blue, true);
    chart.setGradient(gp);
    chart.setEffectIndex(JChart2D.Gradientffect);
    chart.setDrawShadow(true);
    getContentPane().add(chart);

    chart = new JChart2D(JChart2D.PieChart, xData.length, xData, yData, "Pie Chart");
    ImageIcon icon = new ImageIcon("largeJava2slogo.GIF");
    chart.setForegroundImage(icon.getImage());
    chart.setEffectIndex(JChart2D.ImageEffect);
    chart.setDrawShadow(true);
    getContentPane().add(chart);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

From source file:de.mprengemann.intellij.plugin.androidicons.images.ImageUtils.java

public static void updateImage(JLabel imageContainer, File imageFile) {
    if (!imageFile.exists()) {
        return;/*from   w  ww  .java 2  s . c om*/
    }
    BufferedImage img = null;
    try {
        img = ImageIO.read(imageFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (img == null) {
        return;
    }
    int imageWidth = img.getWidth();
    int imageHeight = img.getHeight();
    int imageViewWidth = imageContainer.getWidth();
    int imageViewHeight = imageContainer.getHeight();
    double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight),
            new Dimension(imageViewWidth, imageViewHeight));
    factor = Math.min(factor, 1f);
    imageWidth = (int) (factor * imageWidth);
    imageHeight = (int) (factor * imageHeight);
    if (imageWidth <= 0 || imageHeight <= 0 || imageViewWidth <= 0 || imageViewHeight <= 0) {
        return;
    }
    BufferedImage tmp = UIUtil.createImage(imageViewWidth, imageViewHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    int x = (imageViewWidth - imageWidth) / 2;
    int y = (imageViewHeight - imageHeight) / 2;
    g2.drawImage(img, x, y, imageWidth, imageHeight, null);
    g2.dispose();
    imageContainer.setIcon(new ImageIcon(tmp));
}

From source file:fr.duminy.jbackup.swing.ProgressPanel.java

public void setTask(final Future<?> task) {
    this.task = task;
    if ((task != null) && (cancelButton == null)) {
        Utils.runInEventDispatchThread(new Runnable() {
            @Override/*  w w  w .  j  a va2s  .  c  om*/
            public void run() {
                ImageIcon icon = new ImageIcon(ProgressPanel.class.getResource("cancel.png"));
                cancelButton = new JButton(icon);
                cancelButton.setMargin(new Insets(0, 0, 0, 0));
                cancelButton.setToolTipText(Bundle.getBundle(Messages.class).cancelTaskTooltip());
                add(cancelButton, BorderLayout.EAST);
                revalidate();
                cancelButton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (task.cancel(false)) {
                            removeFromParent();
                        }
                    }
                });
            }
        });
    }
}

From source file:net.sf.keystore_explorer.gui.actions.ExportKeyPairPublicKeyAction.java

/**
 * Construct action./* w  w w .ja v  a 2 s  . c  o m*/
 *
 * @param kseFrame
 *            KeyStore Explorer frame
 */
public ExportKeyPairPublicKeyAction(KseFrame kseFrame) {
    super(kseFrame);

    putValue(LONG_DESCRIPTION, res.getString("ExportKeyPairPublicKeyAction.statusbar"));
    putValue(NAME, res.getString("ExportKeyPairPublicKeyAction.text"));
    putValue(SHORT_DESCRIPTION, res.getString("ExportKeyPairPublicKeyAction.tooltip"));
    putValue(SMALL_ICON, new ImageIcon(Toolkit.getDefaultToolkit()
            .createImage(getClass().getResource(res.getString("ExportKeyPairPublicKeyAction.image")))));
}

From source file:com.igormaznitsa.sciareto.ui.tree.TreeCellRenderer.java

private void ensureIcons(@Nonnull final JTree tree) {
    if (PROJECT_CLOSED == null) {
        PROJECT_CLOSED = new ImageIcon(UiUtils.makeBadgedRightBottom(
                UiUtils.iconToImage(tree, UIManager.getIcon("Tree.closedIcon")), PROJECT_BADGE));
        PROJECT_CLOSED_RO = new ImageIcon(
                UiUtils.makeBadgedRightTop(((ImageIcon) PROJECT_CLOSED).getImage(), READONLY_BADGE));
    }/*from  w w w . ja  v  a 2 s. co m*/

    if (PROJECT_OPENED == null) {
        PROJECT_OPENED = new ImageIcon(UiUtils.makeBadgedRightBottom(
                UiUtils.iconToImage(tree, UIManager.getIcon("Tree.openIcon")), PROJECT_BADGE));
        PROJECT_OPENED_RO = new ImageIcon(
                UiUtils.makeBadgedRightTop(((ImageIcon) PROJECT_OPENED).getImage(), READONLY_BADGE));
    }

    if (FOLDER_CLOSED == null) {
        FOLDER_CLOSED = new ImageIcon(UiUtils.iconToImage(tree, UIManager.getIcon("Tree.closedIcon")));
        FOLDER_CLOSED_RO = new ImageIcon(
                UiUtils.makeBadgedRightTop(((ImageIcon) FOLDER_CLOSED).getImage(), READONLY_BADGE));
    }

    if (FOLDER_OPENED == null) {
        FOLDER_OPENED = new ImageIcon(UiUtils.iconToImage(tree, UIManager.getIcon("Tree.openIcon")));
        FOLDER_OPENED_RO = new ImageIcon(
                UiUtils.makeBadgedRightTop(((ImageIcon) FOLDER_OPENED).getImage(), READONLY_BADGE));
    }

    if (LEAF == null) {
        LEAF = new ImageIcon(UiUtils.iconToImage(tree, UIManager.getIcon("Tree.leafIcon")));
        LEAF_RO = new ImageIcon(
                UiUtils.makeBadgedRightTop(((ImageIcon) PROJECT_OPENED).getImage(), READONLY_BADGE));
    }

    if (LEAF_MINDMAP == null) {
        LEAF_MINDMAP = Icons.DOCUMENT.getIcon();
        LEAF_MINDMAP_RO = new ImageIcon(
                UiUtils.makeBadgedRightTop(Icons.DOCUMENT.getIcon().getImage(), READONLY_BADGE));
    }
}

From source file:components.ImagePreview.java

public void loadImage() {
    if (file == null) {
        thumbnail = null;/*from  w ww . j a v  a  2  s. c o m*/
        return;
    }

    //Don't use createImageIcon (which is a wrapper for getResource)
    //because the image we're trying to load is probably not one
    //of this program's own resources.
    ImageIcon tmpIcon = new ImageIcon(file.getPath());
    if (tmpIcon != null) {
        if (tmpIcon.getIconWidth() > 90) {
            thumbnail = new ImageIcon(tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
        } else { //no need to miniaturize
            thumbnail = tmpIcon;
        }
    }
}

From source file:Tree1.java

public Tree1() {
    super("Sample Tree [OID]");
    setSize(400, 300);/*w ww  . j  a  v a  2 s . c om*/

    Object[] nodes = new Object[5];
    DefaultMutableTreeNode top = new DefaultMutableTreeNode(new OidNode(1, "ISO"));
    DefaultMutableTreeNode parent = top;
    nodes[0] = top;

    DefaultMutableTreeNode node = new DefaultMutableTreeNode(new OidNode(0, "standard"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(2, "member-body"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(3, "org"));
    parent.add(node);
    parent = node;
    nodes[1] = parent;

    node = new DefaultMutableTreeNode(new OidNode(6, "dod"));
    parent.add(node);
    parent = node;
    nodes[2] = parent;

    node = new DefaultMutableTreeNode(new OidNode(1, "internet"));
    parent.add(node);
    parent = node;
    nodes[3] = parent;

    node = new DefaultMutableTreeNode(new OidNode(1, "directory"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(2, "mgmt"));
    parent.add(node);
    nodes[4] = node;
    node.add(new DefaultMutableTreeNode(new OidNode(1, "mib-2")));
    node = new DefaultMutableTreeNode(new OidNode(3, "experimental"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(4, "private"));
    node.add(new DefaultMutableTreeNode(new OidNode(1, "enterprises")));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(5, "security"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(6, "snmpV2"));
    parent.add(node);
    node = new DefaultMutableTreeNode(new OidNode(7, "mail"));
    parent.add(node);

    m_model = new DefaultTreeModel(top);
    m_tree = new JTree(m_model);

    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
    renderer.setOpenIcon(new ImageIcon("opened.gif"));
    renderer.setClosedIcon(new ImageIcon("closed.gif"));
    renderer.setLeafIcon(new ImageIcon("leaf.gif"));
    m_tree.setCellRenderer(renderer);

    m_tree.setShowsRootHandles(true);
    m_tree.setEditable(false);
    TreePath path = new TreePath(nodes);
    m_tree.setSelectionPath(path);

    m_tree.addTreeSelectionListener(new OidSelectionListener());

    JScrollPane s = new JScrollPane();
    s.getViewport().add(m_tree);
    getContentPane().add(s, BorderLayout.CENTER);

    m_display = new JTextField();
    m_display.setEditable(false);
    getContentPane().add(m_display, BorderLayout.SOUTH);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
}

From source file:components.SplitPaneDividerDemo.java

/** Returns an ImageIcon, or null if the path was invalid. */
private static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = SplitPaneDividerDemo.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {//  ww w.j a  v a2  s  .  c  o m
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

From source file:de.mprengemann.intellij.plugin.androidicons.util.ImageUtils.java

public static void updateImage(JLabel imageContainer, File imageFile, Format format) {
    if (imageFile == null || !imageFile.exists()) {
        return;/*from   ww  w .j a v  a2s  .  c om*/
    }
    BufferedImage img = null;
    try {
        img = ImageIO.read(imageFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (img == null) {
        return;
    }
    int imageWidth = img.getWidth();
    int imageHeight = img.getHeight();
    int imageViewWidth = (int) imageContainer.getPreferredSize().getWidth();
    int imageViewHeight = (int) imageContainer.getPreferredSize().getHeight();
    double factor = getScaleFactorToFit(new Dimension(imageWidth, imageHeight),
            new Dimension(imageViewWidth, imageViewHeight));
    imageWidth = (int) (factor * imageWidth);
    imageHeight = (int) (factor * imageHeight);
    if (imageWidth <= 0 || imageHeight <= 0 || imageViewWidth <= 0 || imageViewHeight <= 0) {
        return;
    }
    BufferedImage tmp = UIUtil.createImage(imageViewWidth, imageViewHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    int x = (imageViewWidth - imageWidth) / 2;
    int y = (imageViewHeight - imageHeight) / 2;
    if (format == Format.PNG || format == Format.XML) {
        g2.drawImage(img, x, y, imageWidth, imageHeight, null);
    } else {
        g2.drawImage(img, x, y, imageWidth, imageHeight, Color.WHITE, null);
    }
    g2.dispose();
    imageContainer.setIcon(new ImageIcon(tmp));
}