List of usage examples for javax.swing ImageIcon getIconHeight
public int getIconHeight()
From source file:jshm.gui.GuiUtil.java
public final static javax.swing.ImageIcon resizeIcon(ImageIcon icon, float scale) { return resizeIcon(icon, (int) (scale * icon.getIconWidth()), (int) (scale * icon.getIconHeight())); }
From source file:br.com.diegosilva.jsfcomponents.util.Utils.java
public static byte[] resizeImage(byte[] imageData, String contentType, int width) { ImageIcon icon = new ImageIcon(imageData); double ratio = (double) width / icon.getIconWidth(); int resizedHeight = (int) (icon.getIconHeight() * ratio); int imageType = "image/png".equals(contentType) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; BufferedImage bImg = new BufferedImage(width, resizedHeight, imageType); Graphics2D g2d = bImg.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.drawImage(icon.getImage(), 0, 0, width, resizedHeight, null); g2d.dispose();/*from w w w . j av a 2 s . c om*/ String formatName = ""; if ("image/png".equals(contentType)) formatName = "png"; else if ("image/jpeg".equals(contentType) || "image/jpg".equals(contentType)) formatName = "jpeg"; ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { ImageIO.write(bImg, formatName, baos); return baos.toByteArray(); } catch (IOException ex) { throw new RuntimeException(ex); } }
From source file:edu.ku.brc.ui.IconManager.java
/** * Creates a Black and White image from the color * @param img the image to be converted/* w w w .j a va 2 s . co m*/ * @return new B&W image */ public static ImageIcon createBWImage(final ImageIcon img) { BufferedImage bi = new BufferedImage(img.getIconWidth(), img.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = bi.createGraphics(); g.drawImage(img.getImage(), 0, 0, null); ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null); colorConvert.filter(bi, bi); ImageIcon icon = new ImageIcon(bi); g.dispose(); return icon; }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Gets a scaled icon and if it doesn't exist it creates one and scales it * @param icon image to be scaled//from w w w . ja va 2 s. c o m * @param iconSize the icon size (Std) * @param scaledIconSize the new scaled size in pixels * @return the scaled icon */ public static BufferedImage getBufferedImage(final ImageIcon icon) { Image imgMemory = icon.getImage(); //make sure all pixels in the image were loaded imgMemory = new ImageIcon(imgMemory).getImage(); int w = icon.getIconWidth(); int h = icon.getIconHeight(); BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = bufferedImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(imgMemory, 0, 0, w, h, 0, 0, w, h, null); graphics2D.dispose(); return bufferedImage; }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Gets a scaled icon and if it doesn't exist it creates one and scales it * @param icon image to be scaled//from ww w . j a v a2 s. c om * @param iconSize the icon size (Std) * @param scaledIconSize the new scaled size in pixels * @return the scaled icon */ public static Image getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio) { if (icon != null) { int dstWidth = newMaxWidth; int dstHeight = newMaxHeight; int srcWidth = icon.getIconWidth(); int srcHeight = icon.getIconHeight(); if ((dstWidth < 0) || (dstHeight < 0)) { //image is nonstd, revert to original size dstWidth = icon.getIconWidth(); dstHeight = icon.getIconHeight(); } if (maintainRatio) { double longSideForSource = Math.max(srcWidth, srcHeight); double longSideForDest = Math.max(dstWidth, dstHeight); double multiplier = longSideForDest / longSideForSource; dstWidth = (int) (srcWidth * multiplier); dstHeight = (int) (srcHeight * multiplier); } Image imgMemory = icon.getImage(); //make sure all pixels in the image were loaded imgMemory = new ImageIcon(imgMemory).getImage(); BufferedImage thumbImage = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = thumbImage.createGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2D.drawImage(imgMemory, 0, 0, dstWidth, dstHeight, 0, 0, srcWidth, srcHeight, null); graphics2D.dispose(); return thumbImage; } return null; }
From source file:net.cbtltd.server.UploadFileService.java
private static byte[] getImageBlob(String fn, int fullsizepixels) { try {//from w w w . j ava 2s . c o m ImageIcon image = new ImageIcon(fn); ImageIcon logoImage = new ImageIcon( image.getImage().getScaledInstance(fullsizepixels, -1, Image.SCALE_SMOOTH)); BufferedImage bufferedImage = new BufferedImage(logoImage.getIconWidth(), logoImage.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics graphics = bufferedImage.getGraphics(); graphics.drawImage(logoImage.getImage(), 0, 0, null); fn = fn.substring(0, fn.lastIndexOf('.')) + ".Blob.jpg"; File file = new File(fn); file.delete(); ImageIO.write(bufferedImage, FULLSIZE_JPEG, file); InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { throw new IOException("File is too large for logo - maximum size = " + Integer.MAX_VALUE); } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; } catch (IOException x) { throw new RuntimeException("Error creating BLOB image " + x.getMessage()); } }
From source file:net.cbtltd.server.UploadFileService.java
private static boolean getImage(String fn, int fullsizepixels, int thumbnailpixels) { ImageIcon image = new ImageIcon(fn); // if(image.getIconHeight() > 0 && image.getIconWidth() > 0) { if (image.getImageLoadStatus() == MediaTracker.COMPLETE) { ImageIcon fullsizeImage = new ImageIcon( image.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH)); LOG.debug("\n UploadFileService setImage image= " + image + " width=" + fullsizeImage.getIconWidth() + " height=" + fullsizeImage.getIconHeight()); BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(), fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics(); fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null); File fullsizeFile = new File(fn.substring(0, fn.lastIndexOf('.')) + ".jpg"); fullsizeFile.delete();/*from w w w.j a v a 2s. c om*/ try { ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, fullsizeFile); } catch (IOException x) { throw new RuntimeException("Error saving full sized image " + x.getMessage()); } ImageIcon thumbnailImage = new ImageIcon( image.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH)); File thumbnailFile = new File(fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg"); thumbnailFile.delete(); BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(), thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics(); thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null); try { ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, thumbnailFile); } catch (IOException x) { throw new RuntimeException("Error saving thumbnail image " + x.getMessage()); } return true; } else { LOG.error("\n UploadFileService setImage image= " + image + " width=" + image.getIconWidth() + " height=" + image.getIconHeight()); return false; } }
From source file:net.cbtltd.server.UploadFileService.java
private static boolean getImage(String fn, BufferedImage image, Map<String, byte[]> images) { int fullsizepixels = Text.FULLSIZE_PIXELS_VALUE; int thumbnailpixels = Text.THUMBNAIL_PIXELS_VALUE; // ByteBuffer byteArray = new ByteBuffer(); ByteArrayOutputStream bOutputReg = new ByteArrayOutputStream(); ByteArrayOutputStream bOutputThumb = new ByteArrayOutputStream(); ImageIcon imageIcon = new ImageIcon(image); // if(image.getIconHeight() > 0 && image.getIconWidth() > 0) { if (imageIcon.getImageLoadStatus() == MediaTracker.COMPLETE) { ImageIcon fullsizeImage = new ImageIcon( imageIcon.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH)); LOG.debug("\n UploadFileService setImage image= " + imageIcon + " width=" + fullsizeImage.getIconWidth() + " height=" + fullsizeImage.getIconHeight()); BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(), fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics(); fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null); String fullsizeFile = fn.substring(0, fn.lastIndexOf('.')) + ".jpg"; try {//from w w w. j ava 2s. com ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, bOutputReg); bOutputReg.flush(); images.put(fullsizeFile, bOutputReg.toByteArray()); bOutputReg.close(); } catch (IOException x) { throw new RuntimeException("Error saving full sized image " + x.getMessage()); } catch (Exception e) { LOG.error(e.getMessage() + " Error saving full sized image: " + fullsizeFile); } ImageIcon thumbnailImage = new ImageIcon( imageIcon.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH)); String thumbnailFile = fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg"; BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(), thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics(); thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null); try { ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, bOutputThumb); bOutputThumb.flush(); images.put(thumbnailFile, bOutputThumb.toByteArray()); bOutputThumb.close(); } catch (IOException x) { throw new RuntimeException("Error saving thumbnail image " + x.getMessage()); } catch (Exception e) { LOG.error(e.getMessage() + " Error saving thumbnail image: " + thumbnailFile); } return true; } else { LOG.error("\n UploadFileService setImage image= " + imageIcon + " width=" + imageIcon.getIconWidth() + " height=" + imageIcon.getIconHeight()); return false; } }
From source file:net.cbtltd.server.UploadFileService.java
private static boolean getCompressedImage(String fn, BufferedImage image, Map<String, byte[]> images) { int fullsizepixels = Text.FULLSIZE_PIXELS_VALUE; int thumbnailpixels = Text.THUMBNAIL_PIXELS_VALUE; // ByteBuffer byteArray = new ByteBuffer(); ByteArrayOutputStream bOutputReg = new ByteArrayOutputStream(); ByteArrayOutputStream bOutputThumb = new ByteArrayOutputStream(); ImageIcon imageIcon = new ImageIcon(image); try {/*from w w w . j ava 2 s. c o m*/ GZIPOutputStream zipStreamReg = new GZIPOutputStream(bOutputReg); GZIPOutputStream zipStreamThumb = new GZIPOutputStream(bOutputThumb); if (imageIcon.getImageLoadStatus() == MediaTracker.COMPLETE) { ImageIcon fullsizeImage = new ImageIcon( imageIcon.getImage().getScaledInstance(-1, fullsizepixels, Image.SCALE_SMOOTH)); LOG.debug("\n UploadFileService setImage image= " + imageIcon + " width=" + fullsizeImage.getIconWidth() + " height=" + fullsizeImage.getIconHeight()); BufferedImage fullsizeBufferedImage = new BufferedImage(fullsizeImage.getIconWidth(), fullsizeImage.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics fullsizeGraphics = fullsizeBufferedImage.getGraphics(); fullsizeGraphics.drawImage(fullsizeImage.getImage(), 0, 0, null); String fullsizeFile = fn.substring(0, fn.lastIndexOf('.')) + ".jpg"; try { ImageIO.write(fullsizeBufferedImage, FULLSIZE_JPEG, zipStreamReg); zipStreamReg.close(); bOutputReg.close(); images.put(fullsizeFile, bOutputReg.toByteArray()); } catch (IOException x) { throw new RuntimeException("Error saving full sized image " + x.getMessage()); } ImageIcon thumbnailImage = new ImageIcon( imageIcon.getImage().getScaledInstance(-1, thumbnailpixels, Image.SCALE_SMOOTH)); String thumbnailFile = fn.substring(0, fn.lastIndexOf('.')) + "Thumb.jpg"; BufferedImage thumbnailBufferedImage = new BufferedImage(thumbnailImage.getIconWidth(), thumbnailImage.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics thumbnailGraphics = thumbnailBufferedImage.getGraphics(); thumbnailGraphics.drawImage(thumbnailImage.getImage(), 0, 0, null); try { ImageIO.write(thumbnailBufferedImage, FULLSIZE_JPEG, zipStreamThumb); zipStreamThumb.close(); bOutputThumb.close(); images.put(thumbnailFile, bOutputThumb.toByteArray()); } catch (IOException x) { throw new RuntimeException("Error saving thumbnail image " + x.getMessage()); } return true; } else { LOG.error("\n UploadFileService setImage image= " + imageIcon + " width=" + imageIcon.getIconWidth() + " height=" + imageIcon.getIconHeight()); return false; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; }
From source file:Main.java
public Main() { super("JLayeredPane Demo"); setSize(256, 256);//from ww w .j a v a 2s . c om JPanel content = new JPanel(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); content.setOpaque(false); JLabel label1 = new JLabel("Username:"); label1.setForeground(Color.white); content.add(label1); JTextField field = new JTextField(15); content.add(field); JLabel label2 = new JLabel("Password:"); label2.setForeground(Color.white); content.add(label2); JPasswordField fieldPass = new JPasswordField(15); content.add(fieldPass); setLayout(new FlowLayout()); add(content); ((JPanel) getContentPane()).setOpaque(false); ImageIcon earth = new ImageIcon("largeJava2sLogo.png"); JLabel backlabel = new JLabel(earth); getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE)); backlabel.setBounds(0, 0, earth.getIconWidth(), earth.getIconHeight()); super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); }