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

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform tx = new AffineTransform();

    double x = 50;
    double y = 50;
    tx.translate(x, y);//  w ww.  j a v a2s.com

    g2d.setTransform(tx);
    g2d.drawImage(new ImageIcon("a.png").getImage(), tx, this);
}

From source file:Main.java

/**
 * This method returns a buffered image with the contents of an image
 * This snippet was taken from: http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html
 * @param image// www.j a  v  a2  s.  co  m
 * @return The buffered image
 */
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; for this method's
    // implementation, see e661 Determining If an 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) {
            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:ImageUtil.java

/**
 * get fixing preview image rsize <br/>
 * Exif not apply: calculate by formula: iHeight/iWidth*rsize < (lbHeight-10)
 * Exif apply: calculate by formula: iHeight/iWidth*rsize < (lbHeight-80-10)
 * @param imageFile/*from w w  w.  ja  v  a 2s  . c  o  m*/
 * @param lbWidth
 * @param lbHeight
 * @param applyExif
 * @return
 */
public static int getFixedPreviewSize(File imageFile, int lbHeight, boolean applyExif) {
    int rsize = 0;
    ImageIcon image = new ImageIcon(imageFile.getPath());
    int iHeight = image.getIconHeight();
    int iWidth = image.getIconWidth();
    image = null;
    if (iHeight > iWidth) {
        if (!applyExif) {
            rsize = lbHeight - 10;
        } else {
            rsize = lbHeight - 90;
        }
    } else {
        if (!applyExif) {
            rsize = (lbHeight - 10) * iWidth / iHeight;
        } else {
            rsize = (lbHeight - 90) * iWidth / iHeight;
        }
    }
    return rsize;
}

From source file:ThumbnailTools.java

public ThumbnailTools(Image pImage) {
    image = new ImageIcon(pImage);
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Shape shape = new java.awt.geom.Ellipse2D.Float(20, 20, 200, 200);

    g2d.setClip(shape);/*w  w w .j  a va2s.co  m*/

    int x = 0;
    int y = 0;
    g2d.drawImage(new ImageIcon("a.png").getImage(), x, y, this);
}

From source file:Main.java

public static ImageIcon createImage() {
    BufferedImage image = new BufferedImage(28, 28, BufferedImage.TYPE_INT_ARGB);
    Graphics g = image.getGraphics();
    g.setColor(Color.RED);//from  w w  w .j a va  2 s.c  o m
    g.fillRect(0, 0, 32, 32);
    g.dispose();
    return new ImageIcon(image);
}

From source file:SampleButton.java

public SampleButton() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    JButton b = new JButton("Test");
    b.setHorizontalTextPosition(SwingConstants.LEFT);
    b.setIcon(new ImageIcon("r.gif"));
    b.setRolloverIcon(new ImageIcon("b.gif"));
    b.setRolloverEnabled(true);/*w  ww  .j a va  2s.  c  om*/

    b.setMnemonic('t');
    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Button pressed");
        }
    });
    p.add(b);
    getContentPane().add(p);
    pack();
}

From source file:MainClass.java

public MainClass() {
    menuBar = new JMenuBar();
    JMenu formatMenu = new JMenu("Justify");
    formatMenu.setMnemonic('J');

    MenuAction leftJustifyAction = new MenuAction("Left", new ImageIcon("left.gif"));
    MenuAction rightJustifyAction = new MenuAction("Right", new ImageIcon("right.gif"));
    MenuAction centerJustifyAction = new MenuAction("Center", new ImageIcon("center.gif"));
    MenuAction fullJustifyAction = new MenuAction("Full", new ImageIcon("full.gif"));

    JMenuItem item;//  w w  w  . j  a v a2s.c om
    item = formatMenu.add(leftJustifyAction);
    item.setMnemonic('L');
    item = formatMenu.add(rightJustifyAction);
    item.setMnemonic('R');
    item = formatMenu.add(centerJustifyAction);
    item.setMnemonic('C');
    item = formatMenu.add(fullJustifyAction);
    item.setMnemonic('F');

    menuBar.add(formatMenu);
    menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

}

From source file:Main.java

private JPanel getPanel() {
    JPanel panel = new JPanel();
    JLabel label = new JLabel("Java Technology Dive Log");
    ImageIcon image = null;/*from www  .  j  av  a2 s .  com*/
    try {
        image = new ImageIcon(ImageIO.read(new URL("http://www.java2s.com/style/download.png")));
    } catch (Exception mue) {
        mue.printStackTrace();
    }
    label.setIcon(image);

    JButton button = new JButton("EXIT");
    button.addActionListener(e -> dialog.dispose());

    panel.add(label);
    panel.add(button);

    return panel;
}

From source file:BasicDraw.java

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform tx = new AffineTransform();

    double shiftx = .1;
    double shifty = .3;
    tx.shear(shiftx, shifty);//from w  w w  . ja v a 2 s.c  o  m

    g2d.setTransform(tx);
    g2d.drawImage(new ImageIcon("a.png").getImage(), tx, this);
}